1. When comparing a number and a string, the string is converted to a number value. JavaScript attempts to convert the string numeric literal to a Number type value. First, a mathematical value is derived from the string numeric literal. Next, this value is rounded to nearest Number type value.
2. If one of the operands is Boolean, the Boolean operand is converted to 1 if it is true and +0 if it is false.
3. If an object is compared with a number or string, JavaScript attempts to return the default value for the object. Operators attempt to convert the object to a primitive value, a String or Number value, using the valueOf and toString methods of the objects. If this attempt to convert the object fails, a run-time error is generated.
4. If both operands are objects, they're compared as objects and the equality test is true only if both refer the same object.[1]
var n = 0;
while(true) {
n++;
if(n==100000) break;
}
var n = 0;
while(true) {
n++;
if(n===100000) break;
}
alert('' == '0'); // false
alert(0 == ''); // true
alert(0 == '0'); // true
alert(false == 'false'); // false
alert(false == '0'); // true
alert(false == undefined); // false
alert(false == null); // false
alert(null == undefined); // true
alert(' \t\r\n ' == 0); // true
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (0)