-
Hajipur, Bihar, 844101
Hajipur, Bihar, 844101
JS Basics
JS Variables & Operators
JS Data Types & Conversion
JS Numbers & Math
JS Strings
JS Dates
JS Arrays
JS Control Flow
JS Loops & Iteration
JS Functions
JS Functions
Function Definitions
Function Parameters
Function Invocation
Function Call
Function Apply
Function Bind
Function Closures
JS Arrow Function
JS Objects
JS Objects
JS Object Properties
JS Object Methods
JS Object Display
JS Object Constructors
Object Definitions
Object Get / Set
Object Prototypes
Object Protection
JS Classes & Modules
JS Async Programming
JS Advanced
JS Destructuring
JS Bitwise
JS RegExp
JS Precedence
JS Errors
JS Scope
JS Hoisting
JS Strict Mode
JS this Keyword
JS HTML DOM
DOM Intro
DOM Methods
DOM Document
DOM Elements
DOM HTML
DOM Forms
DOM CSS
DOM Animations
DOM Events
DOM Event Listener
DOM Navigation
DOM Nodes
DOM Collections
DOM Node Lists
JS BOM (Browser Object Model)
JS Web APIs
Web API Intro
Web Validation API
Web History API
Web Storage API
Web Worker API
Web Fetch API
Web Geolocation API
JS AJAX
AJAX Intro
AJAX XMLHttp
AJAX Request
AJAX Response
AJAX XML File
AJAX PHP
AJAX ASP
AJAX Database
AJAX Applications
AJAX Examples
JS JSON
JSON Intro
JSON Syntax
JSON vs XML
JSON Data Types
JSON Parse
JSON Stringify
JSON Objects
JSON Arrays
JSON Server
JSON PHP
JSON HTML
JSON JSONP
JS Graphics & Charts
In JavaScript, operators are symbols or keywords used to perform operations on values or variables. They are fundamental to programming because they allow you to manipulate data, make comparisons, perform calculations, and control the flow of a program. Operators are divided into several categories, including arithmetic, assignment, comparison, logical, bitwise, and more. Understanding how operators work is essential for writing functional and efficient JavaScript code.
In this chapter, you will learn about the different types of JavaScript operators, how to use them with variables, practical examples using names, operator precedence, common mistakes, best practices, and real-world applications.
Arithmetic operators perform basic mathematical calculations. The common arithmetic operators include:
| Operator | Description | Example |
|---|---|---|
+ |
Addition | 5 + 3 → 8 |
- |
Subtraction | 5 - 3 → 2 |
* |
Multiplication | 5 * 3 → 15 |
/ |
Division | 15 / 3 → 5 |
% |
Modulus (Remainder) | 5 % 3 → 2 |
** |
Exponentiation (Power) | 2 ** 3 → 8 |
++ |
Increment (adds 1) | let a = 5; a++; → 6 |
-- |
Decrement (subtracts 1) | let a = 5; a--; → 4 |
let ageAnanya = 22;
let ageIshita = 20;
let totalAge = ageAnanya + ageIshita;
console.log("Total age:", totalAge); // 42
let ageDifference = ageAnanya - ageIshita;
console.log("Age difference:", ageDifference); // 2
Assignment operators are used to assign values to variables. The simplest is =, but there are combined operators like +=, -=, *=, /=, and %=.
let score = 10;
score += 5; // Equivalent to score = score + 5
console.log(score); // 15
score *= 2; // Equivalent to score = score * 2
console.log(score); // 30
Comparison operators compare two values and return a Boolean (true or false).
| Operator | Description | Example |
|---|---|---|
== |
Equal to | 5 == "5" → true |
=== |
Strict equal to (value & type) | 5 === "5" → false |
!= |
Not equal | 5 != 3 → true |
!== |
Strict not equal | 5 !== "5" → true |
> |
Greater than | 5 > 3 → true |
< |
Less than | 5 < 3 → false |
>= |
Greater than or equal to | 5 >= 5 → true |
<= |
Less than or equal to | 5 <= 4 → false |
let ageSanya = 23;
let ageRiya = 21;
console.log(ageSanya > ageRiya); // true
console.log(ageSanya === 23); // true
console.log(ageSanya != ageRiya); // true
Logical operators are used to combine Boolean values. The common operators include:
| Operator | Description | Example |
|---|---|---|
&& |
AND | true && false → false |
| ` | ` | |
! |
NOT | !true → false |
let hasAccess = true;
let isAdmin = false;
console.log(hasAccess && isAdmin); // false
console.log(hasAccess || isAdmin); // true
console.log(!hasAccess); // false
The + operator can also be used to concatenate strings.
let firstName = "Ananya";
let lastName = "Sharma";
let fullName = firstName + " " + lastName;
console.log("Full Name:", fullName); // Ananya Sharma
The ternary operator is a shorthand for an if-else statement. It uses the syntax: condition ? valueIfTrue : valueIfFalse.
let ageMira = 22;
let canVote = ageMira >= 18 ? "Yes" : "No";
console.log("Can Mira vote?", canVote); // Yes
Type operators help check the type of a variable. The typeof operator returns the data type of a variable.
let ageDiya = 21;
console.log(typeof ageDiya); // number
let nameRiya = "Riya";
console.log(typeof nameRiya); // string
Operator precedence determines the order in which operations are executed. Multiplication, division, and modulus have higher precedence than addition and subtraction. Parentheses can be used to override the default order.
let result = 10 + 5 * 2;
console.log(result); // 20, multiplication first
let correctResult = (10 + 5) * 2;
console.log(correctResult); // 30
Confusing == and ===. Use === for strict comparison.
Forgetting operator precedence in complex calculations.
Using logical operators incorrectly with non-Boolean values.
Concatenating numbers and strings without conversion.
Use parentheses to make complex expressions readable.
Prefer strict comparison === and !== over == and !=.
Be careful when mixing strings and numbers.
Test logical conditions thoroughly to avoid unintended results.
Operators are used in nearly every JavaScript application, including:
Calculating totals and averages in forms or surveys.
Managing scores and points in games.
Checking user permissions with logical operators.
Concatenating names, addresses, or messages.
Performing data validation and type checking.
let ages = [22, 21, 23];
let totalAge = ages[0] + ages[1] + ages[2];
let averageAge = totalAge / ages.length;
console.log("Total Age:", totalAge); // 66
console.log("Average Age:", averageAge); // 22
JavaScript operators allow developers to manipulate values, compare data, perform calculations, and control logic. The main categories include arithmetic, assignment, comparison, logical, string, ternary, and type operators. Understanding operator behavior, precedence, and correct usage is essential for writing accurate, readable, and maintainable code.
This tutorial covers detailed explanations, practical examples, common mistakes, best practices, and real-world applications.
Q1. How do you add two numbers a = 5 and b = 10 and store the result in sum using an arithmetic operator?
Q2. How do you increment a variable x by 1 using the ++ operator?
Q3. How do you use the += assignment operator to add 20 to a variable points?
Q4. How do you compare two variables x = 5 and y = "5" using the == operator and the === operator?
Q5. How do you check if a number age is greater than or equal to 18 using a comparison operator?
Q6. How do you write a condition using logical AND (&&) to check if age is greater than 18 and citizen is true?
Q7. How do you use the modulus operator to check if a number x is even?
Q8. How do you use the typeof operator to find the type of a variable name?
Q9. How do you concatenate two strings "Hello" and "World" using the + operator and store in message?
Q10. How do you use instanceof to check if a variable person is an instance of a class Person?
JS Basics
JS Variables & Operators
JS Data Types & Conversion
JS Numbers & Math
JS Strings
JS Dates
JS Arrays
JS Control Flow
JS Loops & Iteration
JS Functions
JS Functions
Function Definitions
Function Parameters
Function Invocation
Function Call
Function Apply
Function Bind
Function Closures
JS Arrow Function
JS Objects
JS Objects
JS Object Properties
JS Object Methods
JS Object Display
JS Object Constructors
Object Definitions
Object Get / Set
Object Prototypes
Object Protection
JS Classes & Modules
JS Async Programming
JS Advanced
JS Destructuring
JS Bitwise
JS RegExp
JS Precedence
JS Errors
JS Scope
JS Hoisting
JS Strict Mode
JS this Keyword
JS HTML DOM
DOM Intro
DOM Methods
DOM Document
DOM Elements
DOM HTML
DOM Forms
DOM CSS
DOM Animations
DOM Events
DOM Event Listener
DOM Navigation
DOM Nodes
DOM Collections
DOM Node Lists
JS BOM (Browser Object Model)
JS Web APIs
Web API Intro
Web Validation API
Web History API
Web Storage API
Web Worker API
Web Fetch API
Web Geolocation API
JS AJAX
AJAX Intro
AJAX XMLHttp
AJAX Request
AJAX Response
AJAX XML File
AJAX PHP
AJAX ASP
AJAX Database
AJAX Applications
AJAX Examples
JS JSON
JSON Intro
JSON Syntax
JSON vs XML
JSON Data Types
JSON Parse
JSON Stringify
JSON Objects
JSON Arrays
JSON Server
JSON PHP
JSON HTML
JSON JSONP
JS Graphics & Charts
