JavaScript Comparison and Logical Operators
JavaScript Comparison and Logical Operators
Comparison and Logical operators are used to test for true or false .
Comparison Operators
Comparison operators are used in logical statements to determine equality or difference
between variables or values.
== equal to x == 8 false
Try it »
x == 5 true
Try it »
x == "5" true
Try it »
x !== 8 true
Try it »
You will learn more about the use of conditional statements in the next chapter of this
tutorial.
https://www.w3schools.com/js/js_comparisons.asp 2/10
1/16/2021 JavaScript Comparison and Logical Operators
Given that x = 6 and y = 3 , the table below explains the logical operators:
|| or (x == 5 || y == 5) is false
Try it »
Syntax
Example
Try it Yourself »
If the variable age is a value below 18, the value of the variable voteable will be "Too
young", otherwise the value of voteable will be "Old enough".
https://www.w3schools.com/js/js_comparisons.asp 3/10
1/16/2021 JavaScript Comparison and Logical Operators
Comparing
HTML Different
CSS MORE Types EXERCISES
When comparing a string with a number, JavaScript will convert the string to a number
when doing the comparison. An empty string converts to 0. A non-numeric string
converts to NaN which is always false .
2 < 12 true
Try it »
2 == "John" false
Try it »
When comparing two strings, "2" will be greater than "12", because (alphabetically) 1 is
less than 2.
To secure a proper result, variables should be converted to the proper type before
comparison:
age = Number(age);
if (isNaN(age)) {
voteable = "Input is not a number";
} else {
https://www.w3schools.com/js/js_comparisons.asp 4/10