Skip to main content

Command Palette

Search for a command to run...

JavaScript Operators: The Basics You Need to Know

JavaScript Operators: The Basics You Need to Know

Updated
4 min read
JavaScript Operators: The Basics You Need to Know

Have you ever wondered how a simple '+' symbol can add numbers, join strings, and even alter JavaScript data types in different contexts? This versatility is one of the powers of operators in JavaScript. JavaScript Operators seamlessly perform numerous tasks behind the scenes, enabling Developers to manipulate data, create dynamic web pages, and build interactive applications.

In this blog, we will understand JavaScript Operators and their precedence, which is crucial for any aspiring Developer. Gaining knowledge of these operators not only streamlines your code but also increases its readability, making it ready for diverse applications.

1) Introduction to the JavaScript Operator

JavaScript operators are special symbols or keywords used to perform operations on values and variables. They can be categorised into several types, including arithmetic (e.g., +, -, *, /, %), comparison (e.g., ==, !=, >, <), logical (e.g., &&, ||, !), and assignment (e.g., =, +=, -=) operators.

These operators help manipulate data, carry out calculations, and make comparisons or decisions within JavaScript code, which are essential for writing efficient and error-free code. Hands-on practice and studying real-world examples are effective ways to become proficient in using these operators.

2) Types of JavaScript Operators

     a) Arithmetic Operators

JavaScript provides several arithmetic operators to perform mathematical operations on numerical values. These operators can be used with literals, variables, or expressions and return a single numerical value.

Example :

let a = 50;
let b = 10:

let sum = a + b; //Addition
let difference = a - b; //Subtraction
let product = a * b; //Multiplication
let quotient = a / b //Divison
let remainder = a % b //Modulus

//Exponentiation 
let power = 5 ** 3;
//(This raises a base number to a given power.)
// Result: 125 (which is 5 * 5 * 5)let count = 0;

//Increment (++) and Decrement (--)
let count = 0;
//These operators add or subtract 1 from a variable.
count++; // count is now 1
count--; // count is now 0

//Printing Outputs:
console.log(`sum: ${sum}`)
console.log(`difference: ${difference}`)
console.log(`product: ${product}`)
console.log(`quotient: ${quotient}`)
console.log(`remainder: ${remainder}`)
//Output:
sum: 60
difference: 40
product: 500
quotient: 5
remainder: 0

     b) Assignment Operators

JavaScript assignment operators are used to assign values to variables, with the basic operator being the equal sign (=). They also include shorthand operations for performing arithmetic and assignment in a single step.

Example :

// declare a variable

// Assignment operator
let x = 5; //5

//compound assignment operator
x += 5;//10 

x -= 2//8

x *= 2//16

x %= 4//0


     c) Comparison Operators

Comparison operators compare two values and provide a Boolean result. and are essential for making decisions in code based on different conditions.

let x = 9;
let y = 6;
let z = "9";

console.log(x > y); //true
console.log(x < y); //false
console.log(x == z); //true
console.log(x === z); //false
console.log(x != y); //true
console.log(x !== y); //true
console.log(x >= y); //true
console.log(x <= y); //false

     d) Logical Operators

Logical operators are used to combine or manipulate Boolean (logical) values and are essential for controlling program flow based on multiple conditions.

let x = true;
let y = false;

console.log(x && y) //false
console.log(x || y) //true
console.log(!x) //false
console.log(!y) //true
let age = 20;
let hasValidID = true; 
let isAdmin = false;

// The condition combines comparison and logical operators 
if ((age >= 18 && hasValidID) || isAdmin){
 console.log("Access granted!"); // This line will execute 
}
 else {
 console.log("Access denied.");
 }
  • && (AND): The condition (age >= 18 && hasValidID) returns true only if both age >= 18 is true and hasValidID is true.

  • || (OR): The overall if statement returns true if at least one of the main conditions is true: either the person meets the age/ID criteria, or they are an administrator.

  • ! (NOT): This operator can be used to invert a boolean value. For example, !isAdmin would return true if isAdmin is false.

Conclusion

In conclusion, JavaScript operators are fundamental symbols that allow developers to manipulate data, perform calculations, compare values, and control program flow

Chai aur JavaScript

Part 1 of 1

In this series, I will explain and understand the core concepts of JavaScript throughout the Web Development Cohort 2026.