JavaScript Operators

Operators are used to perform operations on variables and values. JavaScript has arithmetic, assignment, comparison, and logical operators.

Arithmetic Operators

OperatorNameExampleResult
+Addition5 + 38
-Subtraction5 - 32
*Multiplication5 * 315
**Exponentiation5 ** 225
/Division10 / 25
%Modulus (remainder)10 % 31
++Incrementx++x + 1
--Decrementx--x - 1
Arithmetic Operators
let a = 10, b = 3;
console.log(a + b);    // 13
console.log(a - b);    // 7
console.log(a * b);    // 30
console.log(a ** b);   // 1000 (10 to the power of 3)
console.log(a / b);    // 3.333...
console.log(a % b);    // 1 (remainder)

Assignment Operators

OperatorExampleEquivalent
=x = 5x = 5
+=x += 3x = x + 3
-=x -= 3x = x - 3
*=x *= 3x = x * 3
/=x /= 3x = x / 3
%=x %= 3x = x % 3
**=x **= 3x = x ** 3
Assignment Operators
let x = 10;
x += 5;   console.log(x);   // 15
x -= 3;   console.log(x);   // 12
x *= 2;   console.log(x);   // 24
x /= 4;   console.log(x);   // 6
x **= 2;  console.log(x);   // 36

Comparison Operators

OperatorNameExampleResult
==Equal (loose)5 == "5"true
===Equal (strict)5 === "5"false
!=Not equal (loose)5 != 3true
!==Not equal (strict)5 !== "5"true
>Greater than5 > 3true
<Less than5 < 3false
>=Greater than or equal5 >= 5true
<=Less than or equal5 <= 3false
== vs === (Very Important!)
console.log(5 == "5");    // true  — loose: converts types first
console.log(5 === "5");   // false — strict: types must match
console.log(null == undefined);   // true
console.log(null === undefined);  // false

Logical Operators

OperatorNameExampleResult
&&ANDtrue && falsefalse
||ORtrue || falsetrue
!NOT!truefalse
Logical Operators
let age = 25;
let hasId = true;

console.log(age >= 18 && hasId);   // true  (both must be true)
console.log(age < 18 || hasId);    // true  (at least one true)
console.log(!hasId);               // false (inverts boolean)

String + Operator

String Concatenation
console.log("John" + " " + "Doe");  // "John Doe"

// Mixed types: number becomes string
console.log("5" + 5);    // "55"  (not 10!)
console.log(5 + 5 + "5"); // "105" (left-to-right: 10, then "105")
📝 Note: Always use === (strict equality) instead of == to avoid unexpected type coercion bugs.
Exercise:
What does the === operator check?
Try it YourselfCtrl+Enter to run
Click Run to see the output here.