Just learning to code JavaScript, trying to learn if statements but my code isn't working:
var car = 8;
if (car = 9) {
document.write("your code is not working")
}
This executes the write command and I have no idea why. I'm using the tab button for indents, is that not allowed?
= is called assignment operator in JavaScript, it assigns the value of the right hand side expression to the variable on the left hand side.
You have to use comparison operator instead of assignment operator like this
if (car === 9)
We have two comparison operators in JavaScript, == and ===. The difference between them is that,
== checks if the values are the same, but === checks if the type and the value is also the same.
Go through the wonderful answers, to know more about == and ===
This line assigns car to the value of 9 and check if it is truthy (which 9 is).
if (car=9)
I think you want to use a comparison operator, like this:
if(car == 9)
use this code
var car = 8;
if (car==9)
{
document.write("your code is not working")
}
you need to understand about operators '=' is an assignment operator whereas '==' is a comparision operator.
See Tutorial
If you want
to compare if car is equal to 9, then you have to use code
if(car === 9){
/*Your code goes here*/
}
Related
I have written some code and in certain places == is required and in others = is required. Can someone explain the differences or point me in the direction of the resource that can?
Example:
if($("#block").css.display == "none"){
$("#block").css.display = "block";
}
The only thing I can come up with is that in one I’m changing and in the other I’m checking. But in both I am referring to equality.
= is the assignment operator. It sets a variable (the left-hand side) to a value (the right-hand side). The result is the value on the right-hand side.
== is the comparison operator. It will only return true if both values are equivalent after coercing their types to the same type.
=== is a more strict comparison operator often called the identity operator. It will only return true if both the type and value of the operands are the same.
I would check out CodeCademy for a quick intro to JavaScript.
If you prefer to read more, MDN is a great intro as well.
For those concerned about the source of the term "identity operator" jbabey pointed out that JavaScript: The Definitive Guide seems to mention it.
= assigns a value to a variable
== checks if the two parameter are equal to each other
=== checks if the two parameters are equal to each other and if their type is the same
! not operator
!= checks if the two parameters are not equal to each other
!== checks if the two parameters are not equal to each other or the type is not the same
one more
> checks if one parameter is greater than the other
>= checks if one parameter is greater than or equal to the other
>== DOESN'T EXIST
etcetera...
== is used to test if the value on the left is equal to the value on the right.
= is used to assign the value on the right to the variable on the left.
In javascript you have also the ===.
= This is for set the value to the variable.
== This is for compare if the value is the same.
=== This is for compare if the value is the same and also the type is the same.
The = operator is an assignment operator. You are assigning an object to a value. The == operator is a conditional equality operation. You are confirming whether two things have equal values. There is also a === operator. This compares not only value, but also type.
Assignment Operators
Comparison Operators
I have written some code and in certain places == is required and in others = is required. Can someone explain the differences or point me in the direction of the resource that can?
Example:
if($("#block").css.display == "none"){
$("#block").css.display = "block";
}
The only thing I can come up with is that in one I’m changing and in the other I’m checking. But in both I am referring to equality.
= is the assignment operator. It sets a variable (the left-hand side) to a value (the right-hand side). The result is the value on the right-hand side.
== is the comparison operator. It will only return true if both values are equivalent after coercing their types to the same type.
=== is a more strict comparison operator often called the identity operator. It will only return true if both the type and value of the operands are the same.
I would check out CodeCademy for a quick intro to JavaScript.
If you prefer to read more, MDN is a great intro as well.
For those concerned about the source of the term "identity operator" jbabey pointed out that JavaScript: The Definitive Guide seems to mention it.
= assigns a value to a variable
== checks if the two parameter are equal to each other
=== checks if the two parameters are equal to each other and if their type is the same
! not operator
!= checks if the two parameters are not equal to each other
!== checks if the two parameters are not equal to each other or the type is not the same
one more
> checks if one parameter is greater than the other
>= checks if one parameter is greater than or equal to the other
>== DOESN'T EXIST
etcetera...
== is used to test if the value on the left is equal to the value on the right.
= is used to assign the value on the right to the variable on the left.
In javascript you have also the ===.
= This is for set the value to the variable.
== This is for compare if the value is the same.
=== This is for compare if the value is the same and also the type is the same.
The = operator is an assignment operator. You are assigning an object to a value. The == operator is a conditional equality operation. You are confirming whether two things have equal values. There is also a === operator. This compares not only value, but also type.
Assignment Operators
Comparison Operators
I have written some code and in certain places == is required and in others = is required. Can someone explain the differences or point me in the direction of the resource that can?
Example:
if($("#block").css.display == "none"){
$("#block").css.display = "block";
}
The only thing I can come up with is that in one I’m changing and in the other I’m checking. But in both I am referring to equality.
= is the assignment operator. It sets a variable (the left-hand side) to a value (the right-hand side). The result is the value on the right-hand side.
== is the comparison operator. It will only return true if both values are equivalent after coercing their types to the same type.
=== is a more strict comparison operator often called the identity operator. It will only return true if both the type and value of the operands are the same.
I would check out CodeCademy for a quick intro to JavaScript.
If you prefer to read more, MDN is a great intro as well.
For those concerned about the source of the term "identity operator" jbabey pointed out that JavaScript: The Definitive Guide seems to mention it.
= assigns a value to a variable
== checks if the two parameter are equal to each other
=== checks if the two parameters are equal to each other and if their type is the same
! not operator
!= checks if the two parameters are not equal to each other
!== checks if the two parameters are not equal to each other or the type is not the same
one more
> checks if one parameter is greater than the other
>= checks if one parameter is greater than or equal to the other
>== DOESN'T EXIST
etcetera...
== is used to test if the value on the left is equal to the value on the right.
= is used to assign the value on the right to the variable on the left.
In javascript you have also the ===.
= This is for set the value to the variable.
== This is for compare if the value is the same.
=== This is for compare if the value is the same and also the type is the same.
The = operator is an assignment operator. You are assigning an object to a value. The == operator is a conditional equality operation. You are confirming whether two things have equal values. There is also a === operator. This compares not only value, but also type.
Assignment Operators
Comparison Operators
I have written some code and in certain places == is required and in others = is required. Can someone explain the differences or point me in the direction of the resource that can?
Example:
if($("#block").css.display == "none"){
$("#block").css.display = "block";
}
The only thing I can come up with is that in one I’m changing and in the other I’m checking. But in both I am referring to equality.
= is the assignment operator. It sets a variable (the left-hand side) to a value (the right-hand side). The result is the value on the right-hand side.
== is the comparison operator. It will only return true if both values are equivalent after coercing their types to the same type.
=== is a more strict comparison operator often called the identity operator. It will only return true if both the type and value of the operands are the same.
I would check out CodeCademy for a quick intro to JavaScript.
If you prefer to read more, MDN is a great intro as well.
For those concerned about the source of the term "identity operator" jbabey pointed out that JavaScript: The Definitive Guide seems to mention it.
= assigns a value to a variable
== checks if the two parameter are equal to each other
=== checks if the two parameters are equal to each other and if their type is the same
! not operator
!= checks if the two parameters are not equal to each other
!== checks if the two parameters are not equal to each other or the type is not the same
one more
> checks if one parameter is greater than the other
>= checks if one parameter is greater than or equal to the other
>== DOESN'T EXIST
etcetera...
== is used to test if the value on the left is equal to the value on the right.
= is used to assign the value on the right to the variable on the left.
In javascript you have also the ===.
= This is for set the value to the variable.
== This is for compare if the value is the same.
=== This is for compare if the value is the same and also the type is the same.
The = operator is an assignment operator. You are assigning an object to a value. The == operator is a conditional equality operation. You are confirming whether two things have equal values. There is also a === operator. This compares not only value, but also type.
Assignment Operators
Comparison Operators
I had read in some articles that in some languages, like in JavaScript, assignment operators can be used in conditional statements. I want to know what is the logic behind that operation? As far as I know, only comparison operators are allowed in condition checking statements.
Any expression is allowed in a condition checking statement. If the value of the expression isn't boolean, then it will be converted to boolean to determine what the statement should do.
You can for example use a number in an if statement:
if (1) { ... }
Any non-zero number will be converted to true.
In Javascript an assignment is also an expression, i.e. it has a value. The value of the expression is the same value that was assigned to the variable.
So, you can use an assignment expression in a condition checking statement, and the value of the expression is converted to boolean if needed:
if (x = 1) { ... }
Using an assignment in an condition checking statement can be useful, if the value that you assign should be used to control what happens. If you for example have a function that returns different values for the first calls, then a null when there are no more values, you can use that in a loop:
while (line = getLine()) {
document.write(line);
}
You can of couse do that with the assignment separated from the logic, but then the code gets more complicated:
while (true) {
line = getLine();
if (line == null) break;
document.write(line);
}
In JavaScript (and many other languages), when a value is assigned to a variable, the value "returned" is the value that was assigned to a variable. As such, such a statement can be used in a condition with any assigned value being evaluated in the standard way.
For example:
var y = 0;
if(x = y){
alert("Y(and thus X) is Truthy");
}
else{
alert("Y(and thus X) is Falsy");
}
There are two factors that combine to give this effect:
in many languages, including JavaScript, an expression of the form left = right evaluates to the new left. For example, a = b = c = 0 sets all of a, b, and c to zero.
in many languages, including JavaScript, a wide variety of values can be used as conditional expressions. if(7) is equivalent to if(true); so if(a = 7) is equivalent to a = 7; if(true) rather than to the presumably-intended if(a == 7).
Assigning a value with = returns that value. You can use it to make an assignment while testing if the outcome is truthy or falsey (null, 0, "" undefined, NaN, false)
if (myVar = myArgument) ...
//same as:
// myVar=myArgument;
// if (myArgument) ...
This assigns myArgument to myVar while testing myArgument. Another more specific example:
If (myVar = 3+2) ...
// same as:
// myVar=3+2;
// if (5) ...
The benefit is more compact, terse code, sometimes at the expense of clarity.
This could be used to make a condition check and also use the value after it, without writing more lines.
if (value = someFunction()) {
...
}
This is valid syntax, though highly discouraged. In quite a few languages this is explicitely forbidden, but some languages also does not make this rule (e.g. C).