JavaScript has two boolean values: true and false.
let isLoggedIn = true;
let hasAccess = false;Comparison operators are used to compare two values and return a boolean result (true or false). They include equality (==,===), inequality (!=, !==), and relational operators like >, <, >=, and <=.
== performs loose equality, meaning it compares values after performing type coercion. This can lead to unexpected results because different types can be considered equal if they convert to the same value.
=== performs strict equality, meaning it compares both value and type without coercion. It is generally recommended to use === and!== to avoid type-related bugs.
5 == "5" // true (loose equality)
5 === "5" // false (strict equality)
10 > 5 // true
10 <= 5 // falsetrue && false // false
true || false // true
!true // falseUse these to build complex conditional logic.
Arithmetic operators are used to perform mathematical calculations on numbers. Common ones include:
+ — Addition operator. Adds two numbers together.
% — Modulus (remainder) operator. Returns the remainder after division.
++ — Increment operator. Increases a number by 1. When used as a++, it’s a post-increment (returns the value before incrementing). When used as ++a, it’s a pre-increment (increments first, then returns the value).
-- — Decrement operator. Decreases a number by 1. Works similarly to the increment operator, with pre- and post-decrement forms.
let a = 10;
let b = 3;
a + b // 13 (addition)
a % b // 1 (remainder after division)
a++ // increments a to 11 (post-increment)
--b // decrements b to 2 (pre-decrement)let x = 5;
x += 3; // x = 8
x *= 2; // x = 16A quick way to write a conditional expression.
It evaluates a condition and returns one value if true and another if false, all in a single line.
let grade = 89;
let result = (grade >= 50) ? "Pass" : "Fail";=== and !== over == and !=() to group complex logicAsk the AI if you need help understanding or want to dive deeper in any topic