«Back to Home

Learn JavaScript

Topics

Operators in JavaScript

Operators allow you to perform actions on values. You can use them for calculations, comparisons, assigning values, and even logical decisions. Operators are an essential part of every programming language, and learning them properly will help you write stronger conditions, loops, and functions.

As a college student or fresher, mastering operators early will make your logic building much easier.

JavaScript has several types of operators. In this chapter, we will learn the most commonly used ones:

  1. Arithmetic Operators

  2. Assignment Operators

  3. Comparison Operators

  4. Logical Operators

  5. String Operators

  6. Unary Operators

Let’s understand each type step-by-step.

1. Arithmetic Operators

Arithmetic operators are used to perform mathematical operations.

OperatorMeaning
+Addition
-Subtraction
*Multiplication
/Division
%Modulus (remainder)
**Exponent (power)

Example:

let a = 10;
let b = 3;

console.log(a + b);  
console.log(a - b);  
console.log(a * b);  
console.log(a / b);  
console.log(a % b);  
console.log(a ** b); 

Output:

137303.333333333333333511000

2. Assignment Operators

These operators assign values to variables.

OperatorMeaning
=Assign value
+=Add and assign
-=Subtract and assign
*=Multiply and assign
/=Divide and assign

Example:

let x = 10;

x += 5;  
x -= 3;
x *= 2;
x /= 4;

console.log(x);

Output:

6

Explanation:
10 + 5 = 15
15 - 3 = 12
12 × 2 = 24
24 ÷ 4 = 6

3. Comparison Operators

These operators compare two values and return true or false.

OperatorMeaning
==Equal (with coercion)
===Strict equal
!=Not equal
!==Strict not equal
>Greater than
<Less than
>=Greater or equal
<=Less or equal

Example:

console.log(5 == "5");  
console.log(5 === "5");  
console.log(10 > 7);
console.log(3 <= 3);

Output:

truefalsetruetrue

Important:

  • == compares values but not types

  • === compares both value and type

Always prefer === for accurate comparison.

4. Logical Operators

Logical operators facilitate decision-making within conditional statements and loops.

OperatorMeaning
&&AND
||OR
!NOT

AND (&&)

Returns true only if both conditions are true.

console.log(5 > 2 && 10 > 5);

Output:

true

OR (||)

Returns true if any one condition is true.

console.log(5 > 10 || 10 > 5);

Output:

true

NOT (!)

Flips the value.

console.log(!true);
console.log(!false);

Output:

falsetrue

5. String Operator (+)

The + operator joins two strings.

let first = "Hello";
let second = "World";

console.log(first + " " + second);

Output:

Hello World

If even one value is a string, JavaScript converts the other into a string automatically.

Example:

console.log("Age: " + 20);

Output:

Age: 20

6. Unary Operators

Unary operators work on a single value.

Increment (++)

let a = 5;
a++;

console.log(a);

Output:

6

Decrement (--)

let b = 10;
b--;

console.log(b);

Output:

9

Example Program Using Multiple Operators

let math = 40;
let science = 45;

let total = math + science;
let average = total / 2;

console.log("Total:", total);
console.log("Average:", average);

let passed = average >= 35;
console.log("Passed:", passed);

Output:

Total: 85Average: 42.5Passed: true

This example illustrates how arithmetic, comparison, and logical operators interact.

Why Operators Are Important

You will use operators in:

  • Calculations

  • Conditions (if-else)

  • Loops

  • Functions

  • Form validation

  • API responses

  • Logic building

Operators make your program smart and interactive.

Practice Tasks (Do It Yourself)

  1. Calculate the total and average of three marks.

  2. Check if a number is even or odd using %.

  3. Compare two ages and print who is older.

  4. Use && and || to check if a student passed.

  5. Try "10" + 5 and "10" - 5 and observe the difference.

Author