This forum is now read-only. Please use our new forums at discuss.codecademy.com.
What is the difference between =,==, and === in JS?
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!