JavaScript Operators and Expressions

ADVERTISEMENT

What Are Operators and Expressions?

In JavaScript, an expression is any valid combination of variables, values, and operators that results in a value.

An operator is a symbol that performs a specific operation on one or more values (called operands).

Example:

let sum = 10 + 5; // '+' is an operator; '10 + 5' is an expression

Arithmetic Operators

These are used to perform basic mathematical operations.

OperatorDescriptionExampleResult
+Addition10 + 515
-Subtraction10 - 55
*Multiplication10 * 550
/Division10 / 52
%Modulus (remainder)10 % 31
**Exponentiation2 ** 38
++Incrementa++a = a + 1
--Decrementa--a = a - 1

Example:

let x = 10;
x++;     // 11
x *= 2;  // 22

Assignment Operators

These assign values to variables.

OperatorDescriptionExampleEquivalent To
=Assignx = 10
+=Add and assignx += 5x = x + 5
-=Subtract and assignx -= 3x = x - 3
*=Multiply and assignx *= 2x = x * 2
/=Divide and assignx /= 2x = x / 2
%=Modulus and assignx %= 3x = x % 3

Example:

let a = 10;
a += 5; // a is now 15

Comparison Operators

These are used to compare two values and return a Boolean result (true or false).

OperatorDescriptionExampleResult
==Equal (loose)5 == '5'true
===Equal (strict)5 === '5'false
!=Not equal (loose)5 != '5'false
!==Not equal (strict)5 !== '5'true
>Greater than10 > 5true
<Less than5 < 10true
>=Greater than or equal to10 >= 10true
<=Less than or equal to10 <= 5false

Tip:
Use === and !== instead of == and != to avoid unexpected type coercion.

Logical Operators

Used to combine multiple boolean expressions.

OperatorNameExampleResult
&&Logical ANDtrue && falsefalse
``Logical OR
!Logical NOT!truefalse

Example:

let age = 25;
let isAdult = age >= 18 && age < 60; // true

Bitwise Operators (Advanced, Use Rarely)

These work on numbers at the binary level.

OperatorDescriptionExample
&AND5 & 1 // 1
``OR
^XOR5 ^ 1 // 4
~NOT~5 // -6
<<Left shift5 << 1 // 10
>>Right shift5 >> 1 // 2

Note: Bitwise operators are rarely used in beginner-level JavaScript but useful in performance-critical or system-level programming.

Ternary Operator

A shorthand for if...else.

Syntax:

condition ? expressionIfTrue : expressionIfFalse;

Example:

let age = 18;
let status = age >= 18 ? "Adult" : "Minor"; // "Adult"

This is very useful for quick value decisions.

Operator Precedence

When multiple operators appear in one expression, JavaScript follows a specific order of operations (just like math).

For example:

let result = 10 + 5 * 2; // 20 (not 30)

Explanation:
Multiplication (*) has higher precedence than addition (+), so 5 * 2 happens first.

Common Precedence Order:

  1. () — Parentheses
  2. ** — Exponentiation
  3. * / % — Multiplication, Division, Modulus
  4. + - — Addition, Subtraction
  5. Comparison (<, >, ==, etc.)
  6. Logical (&&, ||)
  7. Assignment (=, +=, etc.)

Use parentheses to control and clarify complex expressions:

let result = (10 + 5) * 2; // 30

Summary

  • Use arithmetic operators for math (+, -, *, /).
  • Use assignment operators (=, +=, -=) to set or update values.
  • Use comparison operators (===, >, <) to compare values.
  • Use logical operators (&&, ||, !) in conditional expressions.
  • Use the ternary operator for quick if...else alternatives.
  • Know operator precedence to avoid bugs in complex expressions.

ADVERTISEMENT