Comparison

There's another set of operators that all return a Boolean value as a result of the operation. These are the comparison operators. The following table lists them together with example uses:

Operator symbol

Description

Example

==

Equality comparison: Returns true when both operands are equal. The operands are converted to the same type before being compared. Also called loose comparison.

> 1 == 1;
true

> 1 == 2;
false

> 1 == '1';
true

===

Equality and type comparison: Returns true if both operands are equal and of the same type. It's better and safer to compare this way because there's no behind-the-scenes type conversions. It is also called strict comparison.

> 1 === '1';
false

> 1 === 1;
true

!=

Non-equality comparison: Returns true if the operands are not equal to each other (after a type conversion).

> 1 != 1;
false

> 1 != '1';
false

> 1 != '2';
true

!==

Non-equality comparison without type conversion: Returns true if the operands are not equal or if they are of different types.

> 1 !== 1;
false

> 1 !== '1';
true

>

Returns true if the left operand is greater than the right one.

> 1 > 1;
false

> 33 > 22;
true

>=

Returns true if the left operand is greater than or equal to the right one.

> 1 >= 1;
true

<

Returns true if the left operand is less than the right one.

> 1 < 1;
false

> 1 < 2;
true

<=

Returns true if the left operand is less than or equal to the right one.

> 1 <= 1;
true

> 1 <= 2;
true

Note that NaN is not equal to anything, not even itself:

> NaN == NaN;
false

Undefined and null

If you try to use a non-existing variable, you'll get an error:

> foo;
ReferenceError: foo is not defined

Using the typeof operator on a non-existing variable is not an error. You get the string "undefined" back:

> typeof foo;
"undefined"

If you declare a variable without giving it a value, this is, of course, not an error. But, the typeof still returns "undefined":

> var somevar;
> somevar;
> typeof somevar;
"undefined"

This is because when you declare a variable without initializing it, JavaScript automatically initializes it with the value undefined:

> var somevar;
> somevar === undefined;
true

The null value, on the other hand, is not assigned by JavaScript behind the scenes; it's assigned by your code:

> var somevar = null;
null
> somevar;
null
> typeof somevar;
"object"

Although the difference between null and undefined is small, it could be critical at times. For example, if you attempt an arithmetic operation, you get different results:

> var i = 1 + undefined;
> i;
NaN
> var i = 1 + null;
> i;
1

This is because of the different ways null and undefined are converted to the other primitive types. The following examples show the possible conversions:

  • Conversion to a number:
    > 1 * undefined;
    NaN
    > 1 * null;
    0
    
  • Conversion to a Boolean:
    > !!undefined;
    false
    > !!null;
    false
    
  • Conversion to a string:
    > "value: " + null;
    "value: null"
    > "value: " + undefined;
    "value: undefined"
    
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.227.114.125