Skip to main content

Command Palette

Search for a command to run...

JavaScript Operators

Updated
•4 min read•View as Markdown
JavaScript Operators

What are the operators

Operators are allow us to perform multiple multiple operation on the data or variables.

Such as mathematical calculations, comparisons, and logical evaluations.

They are total 4 types of Operators:

1.Arithmetic operators (+, -, "*", /, %)

2.Comparison operators (==, ===, !=, >, <)

3.Logical operators (&&, ||, !)

4.Assignment operators (=, +=, -=)


1.Arithmetic Operators

Arithmetic Operators allow us to perform common mathematical operations.

These called Operators (+, -, "*", /, %).Most of them we have seen in our Mathbook.

1."+"(Addition)

The addition operator (+) adds numbers

let a = 10 let b = 20 let c = a + b

console.log(c) // 30

2."-"(Subtraction)

The subtraction operator (-) subtracts numbers.

it's like minus the value

let x = 5; 
let y = 2; 
let z = x - y;
console.log(z) // 3

3."*"(Multiplying)

The multiplication operator (*) multiplies numbers.

let x = 5; 
let y = 2;
let z = x * y; 
console.log(z) // 10

4."/"(Division)

The division operator (/) divides numbers.

let x = 5;
let y = 2;
let z = x / y;
console.log(z) // 2

5."%"(Remainder)

The modulus operator (%) returns the remainder of operation.

let x = 5;
let y = 2; 
let z = x % y; 
console.log(z) // 1

2.Comparison operators

As name it compare the value between 2 variables. It 's always returns True or False value

1."=="(equal to)

it compare the values and If value is same then return True Else False.It does not check the type of the value like it's string or numbers.

console.log(5 == "5"); // true

2."==="(Strict equal to)

It's same as equal to(==) operator but difference is Strict equal to also check the data type

console.log(5  "5"); // true ()
console.log(5 === "5"); // false (strict equal to) (===)

3."!="(Not equal to)

Returns true if values or types differ.

console.log(5 != "5"); // false

console.log(5 !== "5"); // true (strict)

4.">"(Greater than)

Returns True if value is Greater than other value

console.log(9 > 8) // True

console.log(9 >= 9) (Greater than or equal to) // True

5."<"(Less Than)

Returns True if value is Less than other value

console.log(5 < 10); // true

console.log(10 < 5); // false

3.Logical operators (&&, ||, !)

1."&&"(And Operator)

Returns true only if both sides are true.

let age = 25;
let hasLicense = true;
// Logical AND (&&) - Both must be true

if (age >= 18 && hasLicense) { console.log("You can drive!"); }

2."||"(Or operator)

Returns true only if anyone side is true.

// Logical OR (||) - Only one needs to be true

let isWeekend = true; let isHoliday = false;

if (isWeekend || isHoliday) { console.log("You can sleep in."); }

3."!"(Logical NOT)

It invert the result of condition.

let isWeekend = true;

let isHoliday = false;

console.log(!isWeekend); // false

console.log(!isHoliday); // true

5.Assignment operators (=, +=, -=)

Assignment operators are used to assign the value to the variable.

1."="(Basic Assignment operator)

It simply takes the value on the right and puts it into the variable on the left.

let x = 10;

2."+="(Addition Assignment operator)

It adds the value on the right to the variable on the left and then updates that variable with the new result.

let score = 10;

score += 5; // Same as: score = 10 + 5

console.log(score); // 15

3."-="(Subtraction Assignment operator)

It's same as "+=" but differ is it subtract the value

let score = 10;

score -= 5; // Same as: score = 10 - 5

console.log(score); // 5

Thank You