This forum is now read-only. Please use our new forums! Go to forums

0 points
Submitted by swali1
over 7 years

What is the difference between =,==, and === in JS?

The = (assigment operator) , == and === (relational operator) please explain the diffenerce between these 3 operators in javascript;along with relevant examples.And what do we mean by type type conversion in===,please explain with example.And why does

`````` 3==='3'//false
3==="3"//false
"3"==3//true
3===3//true``````

also why does

``````3==3//true
"3"==3//true
3=='3'//true
1==true//true``````

By using = you assign a value to something.

``````x = 1 //x now equals 1
x = 2 //x now equals 2``````

By using == you check if something is equal to something else. This is not strict

``````x == 1 //is x equal to 1? (False)
x == 2 //is x equal to 2? (True)
true == 1 //does the boolean value of true equal 1? (True)``````

By using === you check if something is equal to something else. This is also strict.

``````x === 1 //is x equal to 1? (False)
x === 2 //is x equal to 2? (True)
true === 1 //does the boolean value of true equal 1? (False)``````

What strict does, in case it wasn’t clear there, is that it checks not only the equality of the two values, it compares the types of the two values too. Using == will try and convert one side of the expression to be the same type as the other. For example, boolean and integer. The boolean value for true is 1, therefore does 1 equal 1? Yes, true. When using strict however, it does not try and convert before doing the comparison, it checks if true equals 1, which is doesn’t as they are two different data types, and returns false.

Hope this helps!

points
Submitted by Nathan Audsley
over 7 years