Learn All About JavaScript Operators: A Complete Guide


A Complete Guide to Operators in JavaScript
In JavaScript, operators are used to perform operations on variables and values. They act as building blocks for mathematical calculations, logical decisions, and various tasks in programming. This blog will explore different types of operators, their syntax, and examples to demonstrate their usage.
What Are Operators?
Operators perform operations such as addition, subtraction, assignment, comparison, and logical evaluations. Think of them as tools for manipulating and evaluating data.
Example of Operators in Action:
Types of Operators in JavaScript
Arithmetic Operators
Unary Operators
Assignment Operators
Comparison Operators
Logical Operators
1. Arithmetic Operators
Arithmetic operators handle basic mathematical calculations.
Common Arithmetic Operators:
+
(Addition)-
(Subtraction)*
(Multiplication)/
(Division)%
(Modulus)**
(Exponentiation)
Example Code:
let a = 10;
let b = 5;
let addition = `Addition of a & b: ${a + b}`;
let subtraction = `Subtraction of a & b: ${a - b}`;
let multiplication = `Multiplication of a & b: ${a * b}`;
let division = `Division of a & b: ${a / b}`;
let modulus = `Modulus of a & b: ${a % b}`;
let exponentiation = `Exponentiation of a & b: ${a ** b}`;
console.log(addition);
console.log(subtraction);
console.log(multiplication);
console.log(division);
console.log(modulus);
console.log(exponentiation);
Output:
2. Unary Operators
Unary operators operate on a single operand.
Types of Unary Operators:
++
(Increment)--
(Decrement)
Example Code:
let a = 10;
console.log(a++); // Output: 10, then a becomes 11
console.log(++a); // Output: 12
console.log(a--); // Output: 12, then a becomes 11
console.log(--a); // Output: 10
Output:
3. Assignment Operators
Assignment operators assign values to variables. They can also perform operations while assigning.
Common Assignment Operators:
=
(Assignment)+=
(Addition Assignment)-=
(Subtraction Assignment)*=
(Multiplication Assignment)/=
(Division Assignment)%=
(Modulus Assignment)
Example Code:
let a = 10;
let b = 5;
b = a; // Assigns the value of a to b
console.log(b); // Output: 10
console.log(a += 1); // Adds 1 to a: Output 11
console.log(a -= 1); // Subtracts 1 from a: Output 10
console.log(a *= 2); // Multiplies a by 2: Output 20
console.log(a /= 2); // Divides a by 2: Output 10
console.log(a %= 3); // Finds the modulus of a by 3: Output 1
Output:
4. Comparison Operators
Comparison operators compare two values and return a boolean (true
or false
).
Common Comparison Operators:
==
(Equal to)===
(Strict equal to)!=
(Not equal to)!==
(Strict not equal to)<
(Less than)>
(Greater than)<=
(Less than or equal to)>=
(Greater than or equal to)
Example Code:
let a = 10;
let b = 5;
console.log(a > b); // Output: true
console.log(a < b); // Output: false
console.log(a == 10); // Output: true
console.log(a === "10"); // Output: false (strict equality)
5. Logical Operators
Logical operators evaluate expressions and return a boolean value.
Common Logical Operators:
&&
(AND)||
(OR)!
(NOT)
Example Code:
let x = 5;
let y = 10;
console.log(x > 2 && y < 15); // Output: true
console.log(x > 10 || y < 15); // Output: true
console.log(!(x > y)); // Output: true
Output:
(x > 2 && y < 15)
evaluates totrue
because both conditions are true.(x > 10 || y < 15)
evaluates totrue
because at least one condition is true.!(x > y)
evaluates totrue
becausex > y
is false, and!
negates it.
Summary
Operators in JavaScript provide essential functionality for performing calculations, making comparisons, assigning values, and evaluating logic. By understanding these operators, you can write efficient and effective JavaScript code.
Practice Questions:
Write a program to calculate the area of a rectangle using arithmetic operators.
Demonstrate the use of comparison operators to check if a number is within a range.
Use logical operators to determine if a person is eligible to vote (age > 18 and citizenship).
Subscribe to my newsletter
Read articles from Anmol singh directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
