JavaScript Operators Explained | Arithmetic, Comparison, Logical & More with Examples

Himmat Kumar Mar 25, 2025, 6:01 PM
Blog Thumbnail

JavaScript Operators

JavaScript operators are used to perform operations on values and variables — such as arithmetic, assignment, comparison, and more. Let’s break them down with simple examples.

JavaScript Assignment

The assignment operator = assigns a value to a variable:

let x = 10;     // Assign 10 to x
let y = 2;      // Assign 2 to y
let z = x + y;  // Assign 12 to z

JavaScript Addition

let x = 5;
let y = 2;
let z = x + y;  // Result is 7

JavaScript Multiplication

let x = 5;
let y = 2;
let z = x * y;  // Result is 10

Types of JavaScript Operators

  • Arithmetic Operators
  • Assignment Operators
  • Comparison Operators
  • String Operators
  • Logical Operators
  • Bitwise Operators
  • Ternary Operator
  • Type Operators

JavaScript Arithmetic Operators

Used for basic math:

let a = 3;
let x = (100 + 50) * a;  // Result is 450
Operator Description
+Addition
-Subtraction
*Multiplication
/Division
%Modulus (Remainder)
**Exponentiation (Power)
++Increment
--Decrement

JavaScript Assignment Operators

let x = 10;
x += 5;  // Same as x = x + 5
Operator Example Same As
=x = yx = y
+=x += yx = x + y
-=x -= yx = x - y
*=x *= yx = x * y
/=x /= yx = x / y
%=x %= yx = x % y

JavaScript Comparison Operators

Return true or false based on comparison:

let age = 20;
console.log(age >= 18); // true

JavaScript Logical Operators

  • && AND: true if both are true
  • || OR: true if one is true
  • ! NOT: reverses boolean
let isLoggedIn = true;
let isAdmin = false;

console.log(isLoggedIn && isAdmin); // false

JavaScript String Operator

The + operator also joins (concatenates) strings:

let firstName = "John";
let lastName = "Doe";
let fullName = firstName + " " + lastName;

JavaScript Bitwise Operators

Operate on binary numbers (less common in day-to-day JS):

  • & AND
  • | OR
  • ^ XOR
  • ~ NOT
  • << Left Shift
  • >> Right Shift

JavaScript Ternary Operator

A shorthand for if...else:

let age = 16;
let result = (age >= 18) ? "Adult" : "Minor";

JavaScript Type Operators

typeof checks the type, instanceof checks object type:

typeof "Hello"      // "string"
typeof 123           // "number"
[] instanceof Array  // true

Comments

Please login to leave a comment.

No comments yet.

Related Posts

javascript-data-types-explained
Himmat Kumar Mar 25, 2025, 12:17 PM

JavaScript Data Types Explained with Examples

introduction-to-javascript
Himmat Kumar Oct 27, 2023, 11:36 AM

Introduction JavaScript

javascript-functions-and-scope-guide
Himmat Kumar Mar 30, 2025, 5:26 AM

JavaScript Functions & Scope

javascript-date-and-time-guide
Himmat Kumar Mar 30, 2025, 6:46 AM

Date and Time in JavaScript — Easy Guide

javascript-variables-var-let-const-explained
Himmat Kumar Mar 25, 2025, 12:03 PM

JavaScript Variables: var vs let vs const

javascript-loops-for-while-do-while
Himmat Kumar Mar 29, 2025, 5:43 AM

JavaScript Loops — for, while, do...while

full-stack-development-bridging-front-end-back-end
Himmat Kumar Mar 11, 2025, 4:48 AM

Full-Stack Development: Bridging Front-End and Back-End