Comparison with the Equality Operator
function myTest(val) {
if (val == 12) {
return “Equal”;
}
return “Not Equal”;
}
Comparison with the Strict Equality Operator
“Strict equality (===
) is the counterpart to the equality operator (==
). Unlike the equality operator, strict equality tests both the data type
and value of the compared elements.”
Examples
3 === 3 // true
3 === ‘3’ // false
In the second example, 3
is a Number
type and '3'
is a String
type.
function myTest(val) {
if (val === 7) {
return “Equal”;
}
return “Not Equal”;
}
Comparison with the Inequality Operator
The inequality operator (!=
) is the opposite of the equality operator. It means “Not Equal” and returns false
where equality would return true
and vice versa.
function myTest(val) {
if (val != 99) {
return “Not Equal”;
}
return “Equal”;
}
Comparison with the Strict Inequality Operator
The strict inequality operator (!==
) is the opposite of the strict equality operator. It means “Strictly Not Equal” and returns false
where strict equality would return true
and vice versa. Strict inequality will not convert data types.
function myTest(val) {
if (val !== 17) {
return “Not Equal”;
}
return “Equal”;
}
Comparison with the Greater Than Operator
The greater than operator (>
) compares the values of two numbers. If the number to the left is greater than the number to the right, it returns true
. Otherwise, it returns false
.
Like the equality operator, greater than operator will convert data types of values while comparing.
function myTest(val) {
if (val > 100) {
return “Over 100”;
}
if (val > 10) {
return “Over 10”;
}
return “10 or Under”;
}
Comparison with the Greater Than Equal To Operator
The greater than equal to operator (>=
) compares the values of two numbers. If the number to the left is greater than or equal to the number to the right, it returns true
. Otherwise, it returns false
.
Like the equality operator, greater than equal to operator will convert data types while comparing.
function myTest(val) {
if (val >= 20) {
return “20 or Over”;
}
if (val >= 10) { // Change this line
return “10 or Over”;
}
return “9 or Under”;
}
Comparison with the Less Than Operator
The less than operator (<
) compares the values of two numbers. If the number to the left is less than the number to the right, it returns true
. Otherwise, it returns false
. Like the equality operator, less than operator converts data types while comparing.
function myTest(val) {
if (val < 25) {
return “Under 25”;
}
if (val < 55) {
return “Under 55”;
}
return “55 or Over”;
}
Comparison with the Less Than Equal To Operator
The less than equal to
operator (<=
) compares the values of two numbers. If the number to the left is less than or equal the number to the right, it returns true
. If the number on the left is greater than the number on the right, it returns false
. Like the equality operator, less than equal to
converts data types.
function myTest(val) {
if (val <= 12) {
return “Smaller Than or Equal to 12”;
}
if (val <= 24) {
return “Smaller Than or Equal to 24”;
}
return “25 or More”;
}
Comparisons with the Logical And Operator
The logical and operator (&&
) returns true
if and only if the operands to the left and right of it are true.
function myTest(val) {
if (val <= 50 && val >= 25) {
return “Yes”;
}
return “No”;
}
Comparisons with the Logical Or Operator
The logical or operator (||
) returns true
if either of the operands is true
. Otherwise, it returns false
.
function myTest(val) {
if (val < 10 || val > 20) {
return “Outside”;
}
return “Inside”;
}
Thanks to Free Code Camp for the lessons on JavaScript Comparison Operators!