Operations & Logic

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 shorthand

Arithmetic Operators

Arithmetic operators perform mathematical calculations on numeric values. They follow standard mathematical precedence rules.

Basic Operations

+Addition
-Subtraction
*Multiplication
/Division
10 + 5 // 15

Advanced Operations

%Modulus (remainder)
**Exponentiation
++Increment
--Decrement
10 % 3 // 1

String Operations

+ can concatenate strings
When one operand is a string, + performs concatenation instead of addition
"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 equality

Performs type coercion

5 == "5" // true
===Strict equality

No type coercion (recommended)

5 === "5" // false

Relational Operators

<Less than
>Greater than
<=Less/equal
>=Greater/equal
!=Not equal
!==Strict not equal

Best 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

true && true → true
true && false → false
false && true → false

OR (||)

Returns true if at least one operand is true

true || true → true
true || false → true
false || false → false

NOT (!)

Inverts the boolean value

!true → false
!false → true
!"text" → false

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 assign
x += 5 // x = x + 5
-=Subtract and assign
x -= 3 // x = x - 3
*=, /=, %=Math and assign
x *= 2 // x = x * 2

Special Operators

Ternary Operator (? :)
age >= 18 ? "Adult" : "Minor"
Nullish Coalescing (??)
value ?? "default"
typeof Operator
typeof variable

Operators — step by step

1 / 4
1

== 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).

1 of 4
⚠️The typeof check for undefined
typeof variable === 'undefined' is safe even if the variable has never been declared. Direct comparison variable === undefined throws a ReferenceError if the variable is undeclared. Use typeof for checking if a global or optional property exists without knowing if it was declared.

🎯 Quick Check

What does '5' + 3 evaluate to in JavaScript?

JavaScript Operators: summary

Key points

  • + concatenates if either side is a string: "2" + 3 is "23", while "2" * 3 is 6.
  • || returns the first truthy value; ?? returns the first value that isn't null or undefined, so 0 ?? 10 is 0.
  • Falsy values: false, 0, "", null, undefined, NaN. Everything else is truthy, including "0" and [].
  • % is the remainder operator: n % 2 === 0 checks 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.