Skip to main content

Command Palette

Search for a command to run...

JavaScript Operators Explained

Simple explanations of the most important JavaScript operators.

Updated
•7 min read•View as Markdown
JavaScript Operators Explained
D
Web dev learner documenting the journey —from “why doesn’t this work?” to “ohhh… that’s why.” Sharing notes, mistakes, and small wins

In this article, we are going to explore and cover up all the operator used in javascript.

Operator

Operators are one of the core building blocks of JavaScript. They allow developers to perform calculations, compare values, assign variables, and control program logic. In this article, we’ll explore the most commonly used operators in JavaScript with simple examples.


Assignment Operator

Assignment operator (=) used to assign value to the variable

for eg

// Javascript keyword (let, const, var)
// let name_of_variable = value 

let num = 2026
let name = "Js programming"  

the left side is a name of the variable and the value which is on the right side is value is assigned into the variable.

Addition Assignment Operator

this operator is a shorthand notation for writing the addition operator along with assignment operator

// Method 1
var result = 10

result = result + 10

console.log(result) // 20

// Method 2

var result_1 = 10

// name_of_declared_variable += value (which you want to add)

result += 10
console.log(result) // 20

Subtraction Assignment Operator

this operator is a shorthand notation for writing the subtraction operator along with assignment operator

// Method 1
var result = 10

result = result - 10

console.log(result) // 20

// Method 2

var result_1 = 10

// name_of_declared_variable -= value (which you want to add)

result -= 10
console.log(result) // 20

Arithmetic Operators

Arithmetic operators is used to perform operation like addition (+), subtraction (-), multiplication (*), division (/), Modular (%), increment (++) and decrement (--).

Addition (+)
The addition operator (+) adds two or more operands.
Here are some example.

// Example 1
var result = 10 + 7
console.log(result) // 17

// Example 2
let num1 = 10
let num2 = 20

var result_1 = num1 + num2
console.log(result_1) // 30

Subtraction (-)

subtraction is used to subtract one and more operand together. Here are some of the example of the Subtraction.

 // Example 1
var result = 10 - 7
console.log(result) // 3

// Example 2
let num1 = 20
let num2 = 10

var result_1 = num1 - num2
console.log(result_1) // 10

Multiplication (*)

Multiplication is used to multiply one and more operand together. Here are some of the example of the Multiplication.

// Example 1
var result = 10 * 7
console.log(result) // 70

// Example 2
let num1 = 10
let num2 = 20

var result_1 = num1 * num2
console.log(result_1) // 200

Division (/)

Division is used to divided one and more operand together. Here are some of the example of the Division.

// Example 1
var result = 10 / 7
console.log(result) // 1.4285714285714286

// Example 2
let num1 = 10
let num2 = 20

var result_1 = num1 / num2
console.log(result_1) // 0.5

console.log(10/0) // Infinity

Increment (++)

We can use increment operator in two ways

  1. Post Increment operator

  2. Pre Increment operator

Post Increment operator

In this operator the value is increase after the operation is execute.

let result = 10

console.log(result ++) // 10

// After this operation the value of the result is 11

// this result ++ is a short hand for the result = result + 1
// And 
// result += 1

The current value is returned first, then the variable is incremented.

Pre Increment operator

In this operator, the value is first increment before the operation has taken place

 let result = 10

console.log(++result) // 11

// this ++ result is a short hand for the result = result + 1
// And 
// result += 1

Decrement (--)

We can use decrement operator in two ways

  1. Post decrement operator

  2. Pre decrement operator

Post Decrement operator

In this operator the value is increase after the operation is execute.

let result = 10

console.log(result --) // 10


// this result -- is a short hand for the result = result - 1
// And 
// result -= 1

Pre Decrement operator

In this operator, the value is first decrement before the operation has taken place

 let result = 10

console.log(--result) // 9

// this --result is a short hand for the result = result - 1
// And 
// result -= 1

Modular Operator

Modular operator is a division remainder based operator used to get the remainder of two values.

// Example 1
let result = 16 % 2
console.log(result) // 0

// Example 2
let result_2 = 17 % 2
console.log(result_2) // 1

Comparison Operator

comparison operator is used for making the comparison between the variable are as follow.

operator Description Comparison Return
== equal to "8" == 8 true
=== equal value "5" === 5 false
!= not equal 5 != 8 true
!== not equal value "8" !== 8 true
> greater than 8 > 5 true
< less than 5 < 8 true
>= greater than and equal 5 >= 8 false
<= less than and equal 5 <= 8 true

Equal to (==) vs Equal value (===)

the equal to operator performs type coercion before comparison. JavaScript converts operands to a common type, then compares values. This can lead to unexpected results.

it give true at 5 == "5"

but equal value operator compares both value and type without coercion. Returns true only if both operands are of the same type and have the same value.

it give true only at "5" === "5" otherwise it result in false


Logical Operator

Logical operator are used to combine or modify boolean values to control program flow based on conditions.

AND (&&) → true if both conditions are true
OR (||) → true if at least one condition is true
NOT (!) → reverses a boolean value

Example:

let age = 20

console.log(age > 18 && age < 30) // true

Final word

JavaScript operators are fundamental for performing operations on values and variables. They include arithmetic (e.g., +, -, *, /, %), assignment (e.g., =, +=, -=), comparison (e.g., ==, ===, >, <), logical (e.g., &&, ||, !), bitwise, ternary, and nullish coalescing (??) operators.

Understanding operator precedence is crucial: operations like multiplication (*) and division (/) are evaluated before addition (+) and subtraction (-). For example, 3 + 5 * 2 evaluates to 13, not 16, due to precedence rules. Use parentheses to override default precedence and control evaluation order.

Short-circuit evaluation applies to logical operators (&&, ||, ??): if the result can be determined from the first operand, the second is not evaluated. For instance, false || alert("test") will execute the alert, but true || alert("test") will NOT evaluate the second part at all.

The nullish coalescing operator (??) is preferred over || when assigning defaults, as it only returns the right-hand value if the left is null or undefined, preserving valid falsy values like 0, '', or false.

Finally, strict equality (===) avoids type coercion, making it safer than loose equality (==). Always prefer === to prevent unexpected results when comparing values of different types.

Reference

W3Schools :- https://www.w3schools.com/js/default.asp