Comparison Operators

Comparison operators, as the name suggests, compare two values and return a value of either true or false. They are usually used with conditional statements and loops to perform actions only certain conditions are met.

OperatorSymbolFunction
Is equal to==Returns true if the values on both sides of the operator are equal to each other.
Is not equal to!=Returns true if the values on both sides of the operator are not equal to each other.
Is greater than>Returns true if the value on the left side of the operator is greater than the value on the right side.
Is less than<Returns true if the value on the left side of the operator is less than the value on the right side.
Is greater than or equal to>=Returns true if the value on the left side of the operator is greater than or equal to the value on the right side.
Is less than or equal to<=Returns true if the value on the left side of the operator is less than or equal to the value on the right side.
Strict is equal to===Returns true if the values on both sides of the operator are equal and of the same type.
Strict is not equal to!==Returns true if the values on both sides of the operator are not equal and not of the same type.

Is-Equal-To Operator (==)

If the values or statements on each side of the Is-Equal-To operator are equal then it returns true, otherwise it returns false.

Examples:

ComparisonReturn valueReason
5==5trueTwo equal numbers.
(5+2)==(6+1)trueThe result on both sides is 7.
"My Honda"
==
"My Honda"
trueTwo identical strings.
("My "+"Honda")
==
("My "+"Honda")
trueThe result of the addition of the two strings is indentic on both sides.
5==6falseNot equal numbers.
(5+2)
==
(4+1)
falseThe result on left is 7 and the result on right is 5.
"My Honda"
==
"my honda"
falseStrings are not the same (caps).
("My" + "Honda")
==
("My "+"Honda")
falseNo space character on the left after "My"; strings are unequal.

You can also use variables with comparison operators. The comparison returns true if the values of both variables are equal otherwise is returns false.


var NumberOne = 5;
var NumberTwo = 7;
var NumberThree = 7;

//The following comparison returns true
NumberTwo == NumberThree

//The following comparison return false
NumberOne == NumberThree

Is-Not-Equal-To Operator (!=)

The Is-Not-Equal-To operator (!=) is the opposite of Is-Equal-To operator (==). It returns true if the values on both sides of it are not equal and false if the values on its sides are equal.

Examples:

ComparisonReturn valueReason
5!=6trueTwo unequal numbers.
"My Honda"!="my honda"trueUnequal strings (caps).
5!=5falseTwo identical numbers.
"My Honda"!="My Honda"falseTwo equal numbers.

Is-Greater-Than Operator (>)

The Is-Greater-Than operator returns true if the value on its left side is greater than the value on its right side. The compared values can be strings or numbers. When comparing strings the Is-Greater-Than operator first looks at the first letter of the string. A lowercase letter is greater than an uppercase later and an uppercase letter is greater than a number.

Examples:

ComparisonReturn valueReason
5>4true5 is greater than 4.
0>-1true0 is greater than a negative number.
"a">"A"trueIn strings, lowercase letters are greater than uppercase letters.
"A">"1"trueLetters in strings are greater than numbers in strings.
5>6false5 is not greater than 6.
-1>0falseA negative number is less than 0, not greater.
"B">"b"falseIn strings, lowercase letters are greater than uppercase letters.
"3">"B"falseLetters in strings are greater than numbers in strings.
5>5falseTwo equal numbers.

Is-Less-Than Operator (<)

The Is-Less-Than operator returns true when the value on its side is less than the value on its right side. Of course, it returns false when the value on its left side is greater than the value on its right side.

Examples:

ComparisonReturn valueReason
4<5true4 is less than 5.
"A"<"a"trueIn strings uppercase letters are less than lowercase letters.
5<4false5 is greater than 4.
"a"<"A"falseIn strings lowercase letters are greater than uppercase letters.
5<5falseThe values are equal.

Is-Greater-Than-or-Equal-To Operator (>=)

The Is-Greater-Than-Or-Equal-To operator functions as the Is-Greater-Than operator but it adds one more option for comparison to return true. It returns true if the value on the left side of the operator is greater or equal to the value on the right side of the operator. It returns false if the value on the left side of the operator is less than the value on the right side of the operator.

Examples:

ComparisonReturn valueReason
5>=4true5 is greater than 4.
5>=5true5 is equal to 5.
"a">="A"trueIn strings lowercase letters are greater than uppercase letters.
"A">="A"trueThe strings are equal.
4>=5false4 is less than 5.
"A">="a"falseIn strings uppercase letters are less than lowercase letters, not greater than or equal to.

Is-Less-Than-or-Equal-To Operator (<=)

The Is-Less-Than-Or-Equal-To operator returns true if the value on the left side of the operator is less than or equal to the value on the right side of the operator. It returns false if the value on the left side of the operator is greater than the value on the right side of the operator.

Examples:

ComparisonReturn valueReason
4<=5true4 is less than 5.
5<=5true5 is equal to 5.
"A"<="a"trueIn strings upper case letters are less than lowercase letters.
"A"<="A"trueThe strings are equal.
5<=4false5 is greater than 4, not less than or equal to.
"a"<="A"falseIn strings lowercase letters are greater than upper casecase letters, not less than or equal to.

The Strict Is-Equal-To Operator (===)

The Strict Is-Equal-To Operator returns true if the values on both sides of the operator are equal and of the same type. For example 5 === "5" will return false because on the left side the value is a number and on the right side the value is a string.

Examples:

ComparisonReturn valueReason
5===5trueTwo equal numbers.
(5+2)
===
(6+1)
trueThe same result on both sides.
"My Honda"
===
"My Honda"
trueThe strings on both sides are the same.
("My " + "Honda")
===
("My"+" Honda")
trueThe result of strings addition is the same.
4===5false4 and 5 are not equal numbers.
(5+2)==="7"falseThe 7 on the right is a string value while the 7 on the left is a numeric value.
"My Honda"
===
"my honda"
falseThe strings are not the same (caps).

The Strict Is-Not-Equal-To Operator (!==)

The Strict Is-Not-Equal-To Operator returns true if the values on both sides of the operator are not equal or are not of the same type otherwise it will return false.

ComparisonReturn valueReason
5!==4trueTwo unequal numbers.
5!=="5"trueDifferent types of values.
5!==5falseTwo equal numbers
"My Honda"!==
"My Honda"
falseThe strings are the same.

Leave a Reply