JavaScript

coding learning websites codepractice

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 Objects

JS Classes & Modules

JS Async Programming

JS Advanced

JS HTML DOM

JS BOM (Browser Object Model)

JS Web APIs

JS AJAX

JS JSON

JS Graphics & Charts

JS Operators


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.

Types of Operators

1. Arithmetic Operators

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

Example

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

2. Assignment Operators

Assignment operators are used to assign values to variables. The simplest is =, but there are combined operators like +=, -=, *=, /=, and %=.

Example

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

3. Comparison Operators

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

Example

let ageSanya = 23;
let ageRiya = 21;

console.log(ageSanya > ageRiya); // true
console.log(ageSanya === 23);   // true
console.log(ageSanya != ageRiya); // true

4. Logical Operators

Logical operators are used to combine Boolean values. The common operators include:

Operator Description Example
&& AND true && false → false
`   `
! NOT !true → false

Example

let hasAccess = true;
let isAdmin = false;

console.log(hasAccess && isAdmin); // false
console.log(hasAccess || isAdmin); // true
console.log(!hasAccess);           // false

5. String Operators

The + operator can also be used to concatenate strings.

Example

let firstName = "Ananya";
let lastName = "Sharma";

let fullName = firstName + " " + lastName;
console.log("Full Name:", fullName); // Ananya Sharma

6. Ternary Operator

The ternary operator is a shorthand for an if-else statement. It uses the syntax: condition ? valueIfTrue : valueIfFalse.

Example

let ageMira = 22;
let canVote = ageMira >= 18 ? "Yes" : "No";
console.log("Can Mira vote?", canVote); // Yes

7. Type Operators

Type operators help check the type of a variable. The typeof operator returns the data type of a variable.

Example

let ageDiya = 21;
console.log(typeof ageDiya); // number

let nameRiya = "Riya";
console.log(typeof nameRiya); // string

Operator Precedence

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.

Example

let result = 10 + 5 * 2;
console.log(result); // 20, multiplication first

let correctResult = (10 + 5) * 2;
console.log(correctResult); // 30

Common Mistakes

  • 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.

Best Practices

  • 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.

Real-World Applications

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.

Practical Example

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

Summary of JS Operators

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.


Practice Questions

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?


Try a Short Quiz.

coding learning websites codepractice

No quizzes available.

JavaScript

online coding class codepractice

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 Objects

JS Classes & Modules

JS Async Programming

JS Advanced

JS HTML DOM

JS BOM (Browser Object Model)

JS Web APIs

JS AJAX

JS JSON

JS Graphics & Charts

Go Back Top