CS Engineering Gyan

JavaScript Operators

JavaScript operators are special symbols used to perform operations on variables, values, and expressions. They allow developers to calculate values, compare data, assign information, and make logical decisions within a program. Every JavaScript application, from a simple calculator to a complex web application, relies on operators to process data efficiently.

Whenever you add two numbers, compare two values, assign a value to a variable, or check whether a condition is true or false, you are using JavaScript operators. Understanding operators is one of the most important steps in learning JavaScript because they are used in almost every program.

For example, when a shopping website calculates the total bill, when a login page checks a password, or when a game increases a player's score, JavaScript operators perform these operations behind the scenes.


What are JavaScript Operators?

A JavaScript operator is a symbol or keyword that performs a specific operation on one or more values called operands. The result of the operation is returned as a new value.

In the following example, the plus (+) symbol is an arithmetic operator that adds two numbers.

Syntax

operand operator operand

Example

let a = 15;

let b = 5;

let result = a + b;

console.log(result);

Output

20

Here, a and b are operands, while + is the operator.


Why are Operators Important?

Operators help JavaScript perform calculations, manipulate data, compare values, and execute logical decisions. Without operators, writing useful programs would not be possible.

Importance of Operators


Types of JavaScript Operators

JavaScript provides several categories of operators, each designed for a different purpose.

Operator Type Purpose
Arithmetic Operators Perform mathematical calculations.
Assignment Operators Assign values to variables.
Comparison Operators Compare two values.
Logical Operators Combine multiple conditions.
Bitwise Operators Perform operations on binary values.
String Operators Combine string values.
Ternary Operator Write short conditional expressions.
Type Operators Determine or check data types.

Arithmetic Operators

Arithmetic operators are used to perform mathematical calculations such as addition, subtraction, multiplication, division, and finding remainders. These are among the most frequently used operators in JavaScript.

Operator Name Example
+ Addition a + b
- Subtraction a - b
* Multiplication a * b
/ Division a / b
% Modulus a % b
** Exponentiation a ** b

Addition Operator (+)

The addition operator adds two numeric values and returns their sum.

Example

let x = 20;

let y = 15;

console.log(x + y);

Output

35

Subtraction Operator (-)

The subtraction operator subtracts one value from another.

Example

let marks = 90;

let deduction = 8;

console.log(marks - deduction);

Output

82

Multiplication Operator (*)

The multiplication operator multiplies two numbers.

Example

let price = 150;

let quantity = 4;

console.log(price * quantity);

Output

600

Division Operator (/)

The division operator divides one value by another and returns the quotient.

Example

let total = 100;

let students = 5;

console.log(total / students);

Output

20

Modulus Operator (%)

The modulus operator returns the remainder after division. It is commonly used to determine whether a number is even or odd.

Example

let number = 17;

console.log(number % 2);

Output

1

Since the remainder is 1, the number is odd.


Exponentiation Operator (**)

The exponentiation operator raises a number to the power of another number.

Example

let result = 3 ** 4;

console.log(result);

Output

81

Increment Operator (++)

The increment operator increases the value of a variable by one. It is commonly used inside loops and counters.

Example

let count = 10;

count++;

console.log(count);

Output

11

Decrement Operator (--)

The decrement operator decreases the value of a variable by one.

Example

let score = 25;

score--;

console.log(score);

Output

24

Complete Arithmetic Example

let a = 12;

let b = 4;

console.log("Addition:", a + b);

console.log("Subtraction:", a - b);

console.log("Multiplication:", a * b);

console.log("Division:", a / b);

console.log("Remainder:", a % b);

console.log("Power:", a ** b);

Output

Addition: 16

Subtraction: 8

Multiplication: 48

Division: 3

Remainder: 0

Power: 20736

Key Points to Remember


In the next section, you will learn about Assignment Operators, Comparison Operators, Logical Operators, and their practical applications with real-world examples.


Assignment Operators

Assignment operators are used to assign values to variables. They are commonly used to store data and update existing variable values. Besides the simple assignment operator, JavaScript provides several compound assignment operators that perform a calculation and assignment in a single statement.

Using assignment operators makes code shorter, cleaner, and easier to maintain. They are frequently used in loops, counters, calculations, and real-world web applications.

Common Assignment Operators

Operator Description Example
= Assigns a value x = 10
+= Add and assign x += 5
-= Subtract and assign x -= 2
*= Multiply and assign x *= 4
/= Divide and assign x /= 2
%= Assign remainder x %= 3
**= Assign exponentiation x **= 2

Simple Assignment Operator (=)

The assignment operator stores a value inside a variable. It is the most frequently used operator in JavaScript programming.

Example

let age = 20;

console.log(age);

Output

20

Add and Assign (+=)

The += operator adds a value to the current variable and stores the updated result in the same variable.

Example

let marks = 80;

marks += 10;

console.log(marks);

Output

90

Subtract and Assign (-=)

The -= operator subtracts a value from a variable and updates it immediately.

Example

let balance = 500;

balance -= 120;

console.log(balance);

Output

380

Multiply and Assign (*=)

This operator multiplies the existing value by another number and saves the result.

Example

let salary = 25000;

salary *= 2;

console.log(salary);

Output

50000

Divide and Assign (/=)

The /= operator divides the current value by another number and stores the result.

Example

let total = 100;

total /= 5;

console.log(total);

Output

20

Comparison Operators

Comparison operators compare two values and always return either true or false. These operators are mainly used in conditional statements such as if, else, loops, and decision-making programs.

Operator Description Example
== Equal to 10 == 10
=== Strict equal 10 === "10"
!= Not equal 10 != 5
!== Strict not equal 10 !== "10"
> Greater than 20 > 15
< Less than 5 < 10
>= Greater than or equal 18 >= 18
<= Less than or equal 12 <= 20

Equality Operator (==)

The equality operator compares only the values after automatic type conversion.

Example

console.log(20 == "20");

Output

true

Although one value is a number and the other is a string, JavaScript converts the string into a number before comparison.


Strict Equality Operator (===)

The strict equality operator compares both the value and the data type. It does not perform automatic type conversion.

Example

console.log(20 === "20");

Output

false

Since the data types are different, the result is false.


Greater Than and Less Than Operators

Example

let age = 22;

console.log(age > 18);

console.log(age < 18);

Output

true

false

Logical Operators

Logical operators combine multiple conditions and return a Boolean result. They are commonly used in login systems, validation forms, search filters, and decision-making applications.

Operator Name Description
&& Logical AND Returns true only if all conditions are true.
|| Logical OR Returns true if at least one condition is true.
! Logical NOT Reverses the Boolean value.

Logical AND (&&)

Example

let age = 20;

let citizen = true;

console.log(age >= 18 && citizen);

Output

true

Both conditions are true, so the final result is true.


Logical OR (||)

Example

let marks = 45;

let sportsQuota = true;

console.log(marks >= 50 || sportsQuota);

Output

true

Only one condition needs to be true when using the OR operator.


Logical NOT (!)

Example

let isLoggedIn = false;

console.log(!isLoggedIn);

Output

true

Real-Life Example

let username = "admin";

let password = "12345";

if(username == "admin" && password == "12345")
{
    console.log("Login Successful");
}
else
{
    console.log("Invalid Credentials");
}

Output

Login Successful

Summary

In the next section, you will learn about Bitwise Operators, String Operators, Type Operators, Ternary Operator, Operator Precedence, and JavaScript operator best practices with practical examples.


Bitwise Operators

Bitwise operators perform operations directly on the binary (0 and 1) representation of numbers. Although they are not used as frequently as arithmetic or logical operators, they are useful in system programming, graphics, encryption, data compression, and performance optimization.

Before applying a bitwise operator, JavaScript converts decimal numbers into binary form, performs the operation, and converts the result back into a decimal number.

Common Bitwise Operators

Operator Name Description
& Bitwise AND Returns 1 only if both bits are 1.
| Bitwise OR Returns 1 if either bit is 1.
^ Bitwise XOR Returns 1 when bits are different.
~ Bitwise NOT Inverts every bit.
<< Left Shift Moves bits to the left.
>> Right Shift Moves bits to the right.

Example

let x = 6;

let y = 3;

console.log(x & y);

Output

2

String Operator

The plus (+) operator can also combine two or more strings. This process is called string concatenation.

Example

let firstName = "CSE";

let lastName = "Gyan";

let fullName = firstName + " " + lastName;

console.log(fullName);

Output

CSE Gyan

Type Operators

Type operators help determine the type of a variable or check whether an object belongs to a particular class.

typeof Operator

The typeof operator returns the data type of a variable or value.

Example

let city = "Delhi";

console.log(typeof city);

console.log(typeof 50);

console.log(typeof true);

Output

string

number

boolean

instanceof Operator

The instanceof operator checks whether an object is created from a specific constructor.

Example

let numbers = [10,20,30];

console.log(numbers instanceof Array);

Output

true

Ternary Operator

The ternary operator is a short and efficient way to write simple conditional statements. It uses three parts: a condition, the value returned if the condition is true, and the value returned if the condition is false.

Syntax

condition ? value1 : value2;

Example

let age = 19;

let result = age >= 18 ? "Eligible" : "Not Eligible";

console.log(result);

Output

Eligible

Operator Precedence

When multiple operators appear in the same expression, JavaScript follows operator precedence rules to determine which operation should be performed first.

Example

let result = 10 + 5 * 2;

console.log(result);

Output

20

Multiplication has a higher precedence than addition, so 5 × 2 is calculated first, followed by adding 10.

Using Parentheses

let result = (10 + 5) * 2;

console.log(result);

Output

30

Common Mistakes While Using Operators


Best Practices


Real-World Example

let itemPrice = 800;

let quantity = 3;

let discount = 200;

let total = itemPrice * quantity;

let finalAmount = total - discount;

console.log("Total Price:", total);

console.log("Final Amount:", finalAmount);

console.log(finalAmount > 2000 ? "Free Delivery" : "Delivery Charges Apply");

Output

Total Price: 2400

Final Amount: 2200

Free Delivery

Interview Questions

  1. What are JavaScript operators?
  2. What is the difference between == and ===?
  3. Explain arithmetic operators with examples.
  4. What are logical operators in JavaScript?
  5. What is the purpose of the ternary operator?
  6. What is operator precedence?
  7. How does the typeof operator work?
  8. What is the difference between = and ==?
  9. What are assignment operators?
  10. Where are bitwise operators commonly used?

Summary

JavaScript operators are essential tools for performing calculations, assigning values, comparing data, and making decisions in programs. They help developers write efficient and readable code while solving real-world problems. Understanding arithmetic, assignment, comparison, logical, bitwise, string, type, and ternary operators provides a strong foundation for learning advanced JavaScript concepts.

As you continue learning JavaScript, you will use these operators in loops, functions, DOM manipulation, form validation, event handling, and nearly every interactive web application you build.


← Previous: JavaScript Data Types Next: JavaScript Input & Output →
Home Visit Our YouTube Channel