JavaScript Operators Explained

JavaScript Operators Overview

JavaScript provides a variety of operators, similar to those found in other programming languages. An operator is a special symbol used to perform operations on one or more operands (data values), producing a result.

For example, in the expression 1 + 2, the + symbol is an operator, 1 is the left operand, and 2 is the right operand. The + operator performs addition on the two numeric values and returns the result.

JavaScript operators are grouped into the following categories:

  • Arithmetic Operators – Perform mathematical operations like addition, subtraction, multiplication, etc.

  • Comparison Operators – Compare two values and return a Boolean result.

  • Logical Operators – Combine or invert Boolean values.

  • Assignment Operators – Assign values to variables.

  • Conditional Operators – Perform operations based on a condition.

  • Ternary Operator – A shorthand for the conditional statement (condition ? trueResult : falseResult).

Arithmetic Operators

Arithmetic operators do math on numbers.

OperatorDescriptionExampleResult
+Adds two numbers5 + 27
-Subtracts right number from left10 - 37
*Multiplies two numbers4 * 28
/Divides left number by right10 / 25
%Returns the remainder7 % 21
++Increases a value by 1x++ or ++xx = x + 1
--Decreases a value by 1x-- or --xx = x - 1










Example

let x = 5, y = 10;
    console.log(x + y);  // 15
    console.log(y - x);  // 5
    console.log(x * y);  // 50
    console.log(y / x);  // 2
    console.log(x % 2);  // 1

 Pre-Increment vs Post-Increment

 let x = 5;
    x++;  // post-increment: x is 5 now, becomes 6 in next line
    ++x;  // pre-increment: x becomes 7 immediately
    x--;  // post-decrement: x is 7 now, becomes 6 in next line
    --x;  // pre-decrement: x becomes 5 immediately

String Concatenation with + Operator

If you use + with a string, it joins (concatenates) values instead of adding them.

Example

 let a = 5, b = "Hello ", c = "World!", d = 10;
    console.log(a + b);       // "5Hello "
    console.log(b + c);       // "Hello World!"
    console.log(a + d);       // 15
    console.log(b + true);    // "Hello true"
    console.log(c - b);       // NaN (Not a Number)

Comparison Operators

These operators compare two values and return true or false.

OperatorDescription
==Equal (ignores type)
===Equal (checks type too)
!=Not equal
>Greater than
<Less than
>=Greater than or equal
<=Less than or equal









 


Example

let a = 5, b = 10, c = "5", x = a;
    console.log(a == c);   // true
    console.log(a === c);  // false
    console.log(a != b);   // true
    console.log(a > b);    // false
    console.log(a < b);    // true

Logical Operators

Used to check multiple conditions.

OperatorNameDescription
&&ANDReturns true if both conditions are true
``
!NOTReverses the condition

 Example

let a = 5, b = 10;
    console.log((a != b) && (a < b));  // true
    console.log((a > b) || (a == b));  // false
    console.log(!(a < b));             // false

Assignment Operators

These assign or update variable values.

OperatorDescription
=Assign value
+=Add and assign
-=Subtract and assign
*=Multiply and assign
/=Divide and assign
%=Modulus and assign








 


Example

  let x = 5, y = 10;
    x = y;     // x = 10
    x += 1;    // x = 11
    x -= 2;    // x = 9
    x *= 2;    // x = 18
    x /= 3;    // x = 6
    x %= 4;    // x = 2

Ternary Operator (?:)

A shortcut for if...else statements.

Syntax

condition ? valueIfTrue : valueIfFalse;

Example



    let a = 10, b = 5;

    let c = a > b ? a : b;  // c = 10
    let d = a < b ? a : b;  // d = 5