Saturday, September 6, 2014

JavaScript Operators Equal Sign(s): =, ==, and ===

Assign values (=)
You can assign a value to a variable (for example, x) to a value.
var x = 10;

JavaScript has strict and type–converting comparisons.

Equality (==)
The equality operator converts the operands if they are not of the same type, then tries to compare them (this is known as an abstract comparison). If the operand is a number or a boolean, then JS tries to convert them to numbers to compare to another number. String operands are also converted to numbers if possible. If both operands are objects, then JS can compares internal references of the object stored in memory.
0 == false // can convert false to 0 
'0' == false // can convert false to 0, and string '0' to 0
1 == "1" // can convert string '1' to int 1
null == undefined // can convert undefined to null 
Strict comparison (===)
A strict comparison can only be used if operands are of the same type. A string must match a string, an int to an int.. etc.
a === b
1 === 1 // both int
Inequality, abstract comparison (!=)
This inequality operator returns true if the operands are not equal.

Strict not equal (!==)
The non-identity operator will return true if the operands are not equal and/or not of the same type.