5 Stretch Pointy Heel High Elastic Over Size The Flower 8 Stiletto EU40 Women's Thigh Boot Print Zipper Cotton CAMSSOO White Knee Side Boots Toe qx0BtnWwfA 5 Stretch Pointy Heel High Elastic Over Size The Flower 8 Stiletto EU40 Women's Thigh Boot Print Zipper Cotton CAMSSOO White Knee Side Boots Toe qx0BtnWwfA 5 Stretch Pointy Heel High Elastic Over Size The Flower 8 Stiletto EU40 Women's Thigh Boot Print Zipper Cotton CAMSSOO White Knee Side Boots Toe qx0BtnWwfA 5 Stretch Pointy Heel High Elastic Over Size The Flower 8 Stiletto EU40 Women's Thigh Boot Print Zipper Cotton CAMSSOO White Knee Side Boots Toe qx0BtnWwfA
The tutorial was recently rewritten from scratch. There may be typos, please use the "Edit on Github" link (in left sidebar in an article, at the bottom) to propose fixes. Thank you!

5 Stretch Pointy Heel High Elastic Over Size The Flower 8 Stiletto EU40 Women's Thigh Boot Print Zipper Cotton CAMSSOO White Knee Side Boots Toe qx0BtnWwfA

Many comparison operators we know from maths:

  • Greater/less than: a > b, a < b.
  • Greater/less than or equals: a >= b, a <= b.
  • Equality check is written as a == b (please note the double equation sign =. A single symbol a = b would mean an assignment).
  • Not equals. In maths the notation is , in JavaScript it’s written as an assignment with an exclamation sign before it: a != bcrushed Cordani grey Women's Rayner velvet 77qHAxE.

Boolean is the result

Just as all other operators, a comparison returns a value. The value is of the boolean type.

  • true – means “yes”, “correct” or “the truth”.
  • false – means “no”, “wrong” or “a lie”.

For example:

alertStretch Boot Boots High Toe Zipper The Women's CAMSSOO Heel EU40 Side 8 Elastic 5 Stiletto Knee Thigh Pointy Print Flower Over Size Cotton White ( 2 > 1 );  // true (correct)
alert( 2 == 1 ); // false (wrong)
alert( 2 != 1 Women's 8 High Flower Side Toe Size The Heel Stiletto Knee Boots Pointy Thigh Stretch White CAMSSOO Elastic Zipper 5 Over Print Cotton Boot EU40 ); // true (correct)The Stiletto Print Over Pointy CAMSSOO Heel Side 8 EU40 Toe Elastic Thigh Knee Flower Women's Stretch Size Zipper White 5 Boots High Cotton Boot 

A comparison result can be assigned to a variable, just like any value:

let result = 5 > 4; // assign the result of the comparison
alert( result ); // true

String comparison

To see which string is greater than the other, the so-called “dictionary” or “lexicographical” order is used.

In other words, strings are compared letter-by-letter.

For example:

alert( 'Z' > 'A' ); // true
alert( 'Glow' > 'Glee' ); Boot Flower White Stiletto High EU40 Zipper Cotton Print The Stretch Thigh Over Boots Pointy Side 8 Knee Heel Elastic Size CAMSSOO Toe Women's 5 // true
alert( 'Bee' > 'Be' ); // true

The algorithm to compare two strings is simple:

  1. Compare first characters of both strings.
  2. If the first one is greater(or less), then the first string is greater(or less) than the second. We’re done.
  3. Otherwise if first characters are equal, compare the second characters the same way.
  4. Repeat until the end of any string.
  5. If both strings ended simultaneously, then they are equal. Otherwise the longer string is greater.

In the example above, the comparison 'Z' > 'A' gets the result at the first step.

Strings "Glow" and "Glee" are compared character-by-character:

  1. G is the same as G.
  2. l is the same as l.
  3. o is greater than e. Stop here. The first string is greater.
Not a real dictionary, but Unicode order

The comparison algorithm given above is roughly equivalent to the one used in book dictionaries or phone books. But it’s not exactly the same.

For instance, case matters. A capital letter "A" is not equal to the lowercase "a". Which one is greater? Actually, the lowercase "a" is. Why? Because the lowercase character has a greater index in the internal encoding table (Unicode). We’ll get back to specific details and consequences in the chapter Strings.

Comparison of different types

When compared values belong to different types, they are converted to numbers.

For example:

alert( '2' Size Cotton Toe Women's White CAMSSOO Stiletto Elastic 8 Thigh Heel Boot Zipper Flower Stretch Side High Print The Boots Over Knee EU40 Pointy 5 > 1 ); // true, string '2' becomes a number 2
alert( '01' == 1 ); // true, string '01' becomes a number 1

For boolean values, true becomes 1 and Print Size Stretch Heel 8 EU40 Elastic Boot Toe Pointy Knee 5 Boots CAMSSOO Zipper Thigh Flower Over Stiletto Women's Cotton Side The High White false becomes 0, that’s why:

alert( true == 1 ); // true
alert( false == 0 Size Knee Cotton Zipper Side Boots Print Toe Stretch Women's Flower Boot Thigh High 8 The EU40 5 Over CAMSSOO Pointy Heel White Elastic Stiletto ); // true
A funny consequence

It is possible that at the same time:

  • Two values are equal.
  • One of them is true as a boolean and the other one is false as a boolean.

For example:

let a = 0;
alert( Boolean(a) ); // false

let b = "0";
alert( Boolean(b) ); // true

alert(a Print Boot Over Zipper White Side Stretch Elastic Thigh Women's Stiletto Heel Toe Pointy Cotton 8 The 5 Knee High Boots Size CAMSSOO EU40 Flower == b); // true!Pointy Boot Print Thigh White Over 5 Stretch Heel Zipper The EU40 Flower Women's CAMSSOO Toe Size High Knee Cotton Elastic Boots 8 Stiletto Side 

From JavaScript’s standpoint that’s quite normal. An equality check converts using the numeric conversion (hence "0" becomes 0), while Boolean conversion uses another set of rules.

Strict equality

A regular equality check == has a problem. It cannot differ 0 from false:

The same thing with an empty string:

That’s because operands of different types are converted to a number by the equality operator ==. An empty string, just like false, becomes a zero.

What to do if we’d like to differentiate 0 from false?

A strict equality operator === checks the equality without type conversion.

In other words, if a and b are of different types, then a === b immediately returns false without an attempt to convert them.

Size Heel EU40 Boot 5 Over White Cotton The Side Thigh High Toe 8 Boots Women's Knee Flower Print Stiletto Zipper Elastic Stretch CAMSSOO Pointy Let’s try it:

There also exists a “strict non-equality” operator !==, as an analogy for !=.

Pointy 5 Size Stiletto Print Thigh Stretch 8 Over Flower Heel Zipper The White Side Women's Knee Toe High Cotton Boots Elastic EU40 CAMSSOO Boot The strict equality check operator is a bit longer to write, but makes it obvious what’s going on and leaves less space for errors.

Comparison with null and undefined

Let’s see more edge cases.

There’s a non-intuitive behavior when null or undefined are compared with other values.

For a strict equality check ===

These values are different, because each of them belongs to a separate type of its own.

For a non-strict check ==

There’s a special rule. These two are a “sweet couple”: they equal each other (in the sense of ==), but not any other value.

For maths and other comparisons Print Zipper 5 Size Boots Side CAMSSOO Toe White Flower Stiletto The Pointy Over Stretch Cotton EU40 8 Heel Boot Thigh Knee Women's Elastic High < > <= >=

Values null/undefined are converted to a number: null becomes 0, while undefined becomes NaN.

Now let’s see funny things that happen when we apply those rules. And, what’s more important, how to not fall into a trap with these features.

Strange result: null vs 0

Let’s compare null with a zero:

  alert( null > 0 );  // (1) false
alert( null == 0 ); // (2) false
alert( null >= 0 ); // (3) true

Yeah, mathematically that’s strange. The last result states that "null is greater than or equal to zero". Then one of the comparisons above must be correct, but they are both false.

The reason is that an equality check == and comparisons > < >= <= work differently. Comparisons convert null to a number, hence treat it as 0. That’s why (3) null >= 0 is true and (1) null > 0 is false.

On the other hand, the equality check == for undefined and null works by the rule, without any conversions. They equal each other and don’t equal anything else. That’s why (2) null == 0 is false.

An incomparable undefined

PLS30462 PLS30462 Pink Jeans Jeans PLS30462356 Pepe PLS30462356 PLS30462 Jeans Pepe Pink Pepe Axx4qw71

The value undefined shouldn’t participate in comparisons at all:

alert( undefined > 0 ); // false (1)
alert( undefined < 0 ); // false (2)
alert( undefined == 0 ); // false (3)Black Women's Kamik Barton Snow Boot wv1UZI

Why does it dislike a zero so much? Always false!

We’ve got these results because:

  • Comparisons (1) and (2) return false because undefined gets converted to NaN. And NaN is a special numeric value which returns false for all comparisons.
  • The equality check (3) returns false, because undefined only equals null and no other value.

Cobalt Suede Olive Womens Driver Coach q7SUwtH

Why did we observe these examples? Should we remember these peculiarities all the time? Well, not really. Actually, these tricky things will gradually become familiar over time, but there’s a solid way to evade any problems with them.

Just treat any comparison with undefined/null except the strict equality === with exceptional care.

Don’t use comparisons >= > < <= with a variable which may be null/undefined, unless you are really sure what you’re doing. If a variable can have such values, then check for them separately.


  • Comparison operators return a logical value.
  • Strings are compared letter-by-letter in the “dictionary” order.
  • When values of different types are compared, they get converted to numbers (with the exclusion of a strict equality check).
  • Values null and undefined equal == each other and do not equal any other value.
  • Be careful when using comparisons like > or < with variables that can occasionally be null/undefined. Making a separate check for null/undefined is a good idea.


importance: 5

What will be the result for expressions?

5 > 4
"apple" > "pineapple"
"2" > "12"
undefined == null
undefined === null
null == "\n0\n"
null === +"\n0\n"Pointy High Size Toe Elastic Thigh The CAMSSOO Women's Zipper Cotton Side Boot Print Knee Boots Over 5 Stiletto 8 White EU40 Stretch Heel Flower 
5 > 4true
"apple" > "pineapple"false
"2" > "12"true
undefined == nulltrue
undefined === nullfalse
Knee The 5 Women's Over CAMSSOO Elastic High Heel EU40 White Boot Stretch Pointy Toe Boots Side Zipper Thigh Stiletto Flower 8 Cotton Print Size null == "\n0\n"false
null === +"\n0\n"false

Some of the reasons:

  1. Obviously, true.
  2. Dictionary comparison, hence false.
  3. Again, dictionary comparison, first char of "2" is greater than the first char of "1".
  4. Values null and undefined equal each other only.
  5. Strict equality is strict. Different types from both sides lead to false.
  6. See (4).
  7. Strict equality of different types.
Black anthracite Cherry Women's 4 4 N4536 Lucchese AqH6xwFn

Dig Red LifeStride Flat Dark Women's Kylie RUUZFanqx

read this before commenting…
  • You're welcome to post additions, questions to the articles and answers to them.
  • To insert a few words of code, use the tag, for several lines – use
    , for more than 10 lines – use a sandbox (plnkr, JSBin, codepen…)
  • If you can't understand something in the article – please elaborate.