</> Code Corner

JavaScript · JS Tutorial · Lesson 27

JS Comparisons

Comparison and logical operators.

Comparison and logical operators are used to test for true or false.

Example

<p id="demo"></p>
<script>
let age = 20;
let out = "";
out += (age >= 18 ? "Adult" : "Minor") + "<br>";
out += "age > 18 && age < 30: " + (age > 18 && age < 30) + "<br>";
out += "age === 20 || age === 21: " + (age === 20 || age === 21) + "<br>";
out += "!(age > 18): " + !(age > 18) + "<br>";
let user = null;
out += "Nullish: " + (user ?? "Guest") + "<br>";
out += "Optional chaining: " + (user?.name);
document.getElementById("demo").innerHTML = out;
</script>
Operator Meaning
== / === Equal value / equal value and type
!= / !== Not equal / not equal value or type
> < >= <= Greater / less than (or equal)
condition ? a : b Conditional (ternary) operator
?? Nullish coalescing: right side if left is null/undefined
?. Optional chaining: undefined instead of an error

Keep reading “JS Comparisons”

Sign up to read the full lesson, try every example in the live editor, and take the 5-question quiz.

Chat with us 👋