JavaScript Operators
Operators perform operations on variables and values. Master arithmetic, comparison, logical, and assignment operators to manipulate data effectively.
What is the difference between == and === in JavaScript?
=== (strict equality) checks that the value and the type are the same, with no conversion. == (loose equality) converts types first, so "5" == 5 is true but "5" === 5 is false. Always use === and !==, except for the value == null shortcut, which matches both null and undefined.
5 + 2 // 7 arithmetic: + - * / % **
"5" === 5 // false strict equality
"5" == 5 // true loose (converts types)
a && b // logical AND
a || b // logical OR (first truthy)
a ?? b // nullish: b only if a is null/undefined
x += 5 // assignment shorthandArithmetic Operators
Arithmetic operators perform mathematical calculations on numeric values. They follow standard mathematical precedence rules.
Basic Operations
+Addition-Subtraction*Multiplication/Division10 + 5 // 15Advanced Operations
%Modulus (remainder)**Exponentiation++Increment--Decrement10 % 3 // 1String Operations
"Hello" + " World" // "Hello World"Type Coercion Warning
"5" + 3 results in "53" (string), while "5" - 3 results in 2 (number). The + operator behaves differently with strings.
Comparison Operators
Comparison operators compare two values and return a boolean result. Understanding the difference between loose and strict equality is crucial.
Equality Operators
==Loose equalityPerforms type coercion
5 == "5" // true===Strict equalityNo type coercion (recommended)
5 === "5" // falseRelational Operators
<Less than>Greater than<=Less/equal>=Greater/equal!=Not equal!==Strict not equalBest Practice
Always use === and !== for equality comparisons to avoid unexpected type coercion issues.
Logical Operators
Logical operators combine boolean expressions and return boolean results. They're essential for complex conditional logic.
AND (&&)
Returns true only if both operands are true
OR (||)
Returns true if at least one operand is true
NOT (!)
Inverts the boolean value
Short-Circuit Evaluation
AND (&&):
If first is false, second is not evaluated
OR (||):
If first is true, second is not evaluated
Interactive Operators Example
Code Editor
<pre id="out"></pre>Preview
Key Points in This Example:
- • All arithmetic operators with practical examples
- • Strict vs loose equality comparison
- • Logical operators for complex conditions
- • Assignment operators for efficient coding
Assignment & Special Operators
Assignment Operators
+=Add and assignx += 5 // x = x + 5-=Subtract and assignx -= 3 // x = x - 3*=, /=, %=Math and assignx *= 2 // x = x * 2Special Operators
age >= 18 ? "Adult" : "Minor"value ?? "default"typeof variableOperators — step by step
== vs === — loose vs strict equality
=== (strict) checks both value and type — no coercion. == (loose) coerces types before comparing: '5' == 5 is true, null == undefined is true, 0 == false is true. These surprises cause real bugs. Always use === unless you specifically want the null/undefined equivalence check (if (val == null) catches both null and undefined).
🎯 Quick Check
What does '5' + 3 evaluate to in JavaScript?
JavaScript Operators: summary
Key points
+concatenates if either side is a string:"2" + 3is"23", while"2" * 3is6.||returns the first truthy value;??returns the first value that isn'tnullorundefined, so0 ?? 10is0.- Falsy values:
false,0,"",null,undefined,NaN. Everything else is truthy, including"0"and[]. %is the remainder operator:n % 2 === 0checks for an even number.
Frequently asked questions
›When should I use ?? instead of ||?
Use ?? when 0, "" or false are valid values that you want to keep. count || 10 replaces 0 with 10; count ?? 10 keeps the 0.
›Why does "2" + 2 give "22"?
When one side of + is a string, JavaScript converts the other side to a string and joins them. Convert first with Number("2") + 2.