11.3 Comparison Operators

Comparison operators are often used in the if statements (Section 15.6): two operands are compared and the flow of the program proceeds according to the result of the comparison.

11.3.1 Equal (==)

The Equal operator returns true if its operands' values are equal; otherwise returns false:

4 == 4

true

"foo" == "bar"

false

After defining the following variables:

var1 = 5;
var2 = "foo";
var1 == 5

true

var2 == "bar"

false

Important: Numbers and strings are always considered different:

"5" == 5

false

The same holds for a null and an undefined value; suppose that no variable called never_defined has ever been defined in your script:

never_defined == 0

0 (false)

never_defined == ""

0 (false)

Two undefined variables are equal:

never_defined == never_defined_2

1 (true)

11.3.2 Not Equal (!=)

The Not Equal operator returns true if its operands' values are different; otherwise returns false:

4 != 4

false

"foo" != "bar"

true

After defining the following variables:

var1 = 5;
var2 = "foo";
var1 != 5

false

var2 != "bar"

true

Important: Numbers and strings are always considered different:

"5" != 5

true

The same holds for a null and an undefined value; suppose that non variable called never_defined has ever been defined in your script:

never_defined != 0

1 (true)

never_defined != ""

1 (true)

Two undefined variables are equal:

never_defined != never_defined_2

0 (false)

11.3.3 Weak Equal (~==)

The Weak Equal operator behaves like Equal with two exceptions:

  1. When applied to operands of different type (a number and a string), it tries to convert the string in an integer, and then performs the comparison.

  2. Undefined and null values are considered equal.

"5" ~== 5

true

never_defined ~== 0

1 (true)

never_defined ~== ""

1 (true)

11.3.4 Weak Not Equal (~!=)

The Weak Not Equal operator behaves like Not Equal with two exceptions:

  1. When applied to operands of different type (a number and a string), it tries to convert the string in an integer, and then performs the comparison.

  2. Undefined and null values are considered equal.

"5" ~!= 5

0 (false)

never_defined ~!= 0

0 (false)

never_defined ~!= ""

0 (false)

11.3.5 Greater Then (>)

Applied to integers, this operator returns true if the first operand is greater than the second. Otherwise, it returns false. Applied to strings, returns true if the first operand follows the second in alphabetical order. Otherwise, it returns false.

5 > 5

0 (false)

5 > 3

1 (true)

"foo" > "bar"

1 (true)

11.3.6 Greater Then or Equal (>=)

Applied to integers, this operator returns true if the first operand is greater than or equal to the second. Otherwise, it returns false. Applied to strings, returns true if the first operand follows the second in alphabetical order or if they are equal. Otherwise, it returns false.

5 >= 5

1 (true)

"foo" >= "foo"

1 (true)

11.3.7 Lower Then (<)

Applied to integers, this operator returns true if the first operand is lower than the second. Otherwise, it returns false. Applied to strings, returns true if the first operand precedes the second in alphabetical order. Otherwise, it returns false.

5 < 5

0 (false)

5 < 3

0 (false)

"foo" < "bar"

0 (false)

11.3.8 Lower Then or Equal (<=)

Applied to integers, this operator returns true if the first operand is lower than or equal to the second. Otherwise, it returns false. Applied to strings, returns true if the first operand precedes the second in alphabetical order or if they are equal. Otherwise, it returns false.

5 <= 5

1 (true)

"foo" <= "foo"

1 (true)

This manual can be downloaded from http://www.g-cows.org/.