JavaScript Operators


JavaScript is a powerful and versatile programming language, and operators are an essential part of any JavaScript program. They help you perform operations on variables, values, and expressions. In this guide, we’ll take a detailed look at JavaScript operators, types, and provide practical examples for each one.


What Are JavaScript Operators?

JavaScript operators are special symbols or keywords used to perform operations on values or variables. They can be categorized into several types, each serving a different purpose in JavaScript. The most common types of operators include:

  • Arithmetic Operators
  • Assignment Operators
  • Comparison Operators
  • Logical Operators
  • Bitwise Operators
  • Ternary Operator
  • Unary Operators

1. Arithmetic Operators

Arithmetic operators perform mathematical calculations like addition, subtraction, multiplication, etc.

Examples of Arithmetic Operators

Operator Description Example
+ Addition 5 + 38
- Subtraction 5 - 32
* Multiplication 5 * 315
/ Division 6 / 32
% Modulus (Remainder) 7 % 31
** Exponentiation 2 ** 38

Sample Code:

let num1 = 10;
let num2 = 5;

console.log(num1 + num2); // Output: 15
console.log(num1 - num2); // Output: 5
console.log(num1 * num2); // Output: 50
console.log(num1 / num2); // Output: 2
console.log(num1 % num2); // Output: 0
console.log(num1 ** num2); // Output: 100000

2. Assignment Operators

Assignment operators are used to assign values to variables.

Examples of Assignment Operators

Operator Description Example
= Simple Assignment x = 10
+= Add and Assign x += 5x = x + 5
-= Subtract and Assign x -= 5x = x - 5
*= Multiply and Assign x *= 5x = x * 5
/= Divide and Assign x /= 5x = x / 5
%= Modulo and Assign x %= 5x = x % 5

Sample Code:

let x = 10;
x += 5;  // x = x + 5
console.log(x); // Output: 15

x *= 2;  // x = x * 2
console.log(x); // Output: 30

3. Comparison Operators

Comparison operators are used to compare two values. They return true or false based on the condition.

Examples of Comparison Operators

Operator Description Example
== Equal to 5 == 5true
=== Strict Equal to 5 === "5"false
!= Not equal to 5 != 3true
!== Strict Not Equal to 5 !== "5"true
> Greater than 5 > 3true
< Less than 3 < 5true
>= Greater than or equal 5 >= 3true
<= Less than or equal 3 <= 5true

Sample Code:

let a = 10;
let b = 20;

console.log(a == b);  // Output: false
console.log(a !== b); // Output: true
console.log(a > b);   // Output: false
console.log(a <= b);  // Output: true

4. Logical Operators

Logical operators are used to combine conditional statements. They are essential for controlling program flow.

Examples of Logical Operators

Operator Description Example
&& Logical AND true && falsefalse
`   `
! Logical NOT !truefalse

Sample Code:

let x = true;
let y = false;

console.log(x && y); // Output: false
console.log(x || y); // Output: true
console.log(!x);     // Output: false

5. Bitwise Operators

Bitwise operators work with the binary representation of numbers.

Examples of Bitwise Operators

Operator Description Example
& Bitwise AND 5 & 31
` ` Bitwise OR
^ Bitwise XOR 5 ^ 36
~ Bitwise NOT ~5-6
<< Left Shift 5 << 110
>> Right Shift 5 >> 12

Sample Code:

let num1 = 5;  // 0101 in binary
let num2 = 3;  // 0011 in binary

console.log(num1 & num2); // Output: 1 (0001 in binary)
console.log(num1 | num2); // Output: 7 (0111 in binary)
console.log(num1 ^ num2); // Output: 6 (0110 in binary)

6. Ternary Operator

The ternary operator is a shorthand for if-else statements. It has three parts: a condition, a true value, and a false value.

Syntax:

condition ? expression1 : expression2;

Example:

let age = 18;
let canVote = (age >= 18) ? "Yes, you can vote." : "No, you cannot vote.";
console.log(canVote); // Output: Yes, you can vote.

7. Unary Operators

Unary operators perform operations on a single operand.

Examples of Unary Operators

Operator Description Example
++ Increment x++
-- Decrement x--
+ Unary Plus +x
- Unary Negation -x
! Logical NOT !x

Sample Code:

let num = 5;
console.log(++num); // Output: 6
console.log(num--); // Output: 6 (then num becomes 5)