Skip to main content

Command Palette

Search for a command to run...

JavaScript Operators: The Basics You Need to Know

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

Remember, in our childhood, we were often told by our elders that if we didn’t know how to calculate, how would we deal in the market? Those who didn’t like to do sums in childhood must have this memory. Yes that’s what I also want to make you realize. Similarly, in all programming languages as well as JavaScript we have the operators for the same purpose.

Whenever you need to build any real world application you must have to use operators. Let it be to do basic calculations or checking conditions in Control flow and many more. In this blog we will learn about operators in JS.

What operators are?

An operator is a symbol that tells JavaScript to perform a specific operation on one or more values(operands). For example:

let sum= 5+3;

here 5 , 3 → operands & ‘+’ → operator

Types of operators in JavaScript

Here are some most commonly used operators in JavaScript.

  1. Arithmetic operators (+, -, *, /, %)

  2. Comparison operators (==, ===, !=, >, <)

  3. Logical operators (&&, ||, !)

  4. Assignment operators (=, +=, -=)

1. Arithmetic Operators:

These operators are used to do basic operations(mathematical).

Operator Description Example
+ Addition 5 + 3
- Subtraction 5 - 3
* Multiplication 5 * 3
/ Division 10 / 2
% Modulus (remainder) 10 % 3

Example:

let a=5, b=2;
console.log(a+b); //7
console.log(a-b); //3
console.log(a*b); //10
console.log(a/b); //2.5
console.log(a%b); //1

2. Comparison operators

These operators are used to compare between two variables.

Operator Description
== Equal to
=== Strict equal to
!= Not equal to
> Greater than
< Less than

Example:

let x = 5;
let y = "5";

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

Difference between === and ==

== compares only the values & perform type conversion if needed.

=== compares only the value, never does type conversion.

console.log(7=='7'); //true
console.log(7==7); //false

3. Logical operators

These operators are used for combining more than one conditions.

Operator Meaning
&& Logical AND
! Logical NOT

Example :

let age = 20;
let hasID = true;

console.log(age >= 18 && hasID); // true
console.log(age < 18 || hasID);  // true
console.log(!hasID);             // false

Truth Table for Logical Operators

4. Assignment Operators

These operators are used to assign values to a variable

Operator Example Meaning
= x = 5 Assign value
+= x += 3 x = x + 3
-= x -= 2 x = x - 2

Example

let num=10;

num+=5;
console.log(num);// 15

num-=3;
console.log(num);// 12

Example: Evaluating a Student's Result

Let's consider a simple real-world situation.

Suppose your teacher wants to check whether a student has passed an exam. The student has marks in Math and Science. To determine the result, the teacher needs to:

  • Calculate the total marks

  • Check whether the student has scored more than 40 in each subject

  • Ensure the total marks are at least 100

We can use different JavaScript operators to perform these checks.

Code

let math = 55;
let science = 50;

// Arithmetic operator
let total = math + science;

// Assignment operator (adding bonus marks)
total += 5;

console.log("Total marks:", total); //110

// Comparison operators
console.log(math > 40); //true
console.log(science > 40); //true

// Logical operator
let passed = (total >= 100) && (math > 40) && (science > 40);

console.log("Student passed:", passed); //true

// Difference between == and ===
let x = 100;
let y = "100";

console.log(x == y);   // true
console.log(x === y);  // false