Operators in JavaScript

Operators in JavaScript allow you to perform operations on data such as calculations, comparisons, logical checks, and assignments. They are a fundamental part of working with variables and values, enabling you to write dynamic, readable, and functional code.


Arithmetic Operators in JavaScript

Arithmetic operators are used to perform basic mathematical operations on numeric values.

List of JavaScript Arithmetic Operators

OperatorDescriptionExampleOutput
+Addition5 + 38
-Subtraction10 - 46
*Multiplication6 * 318
/Division15 / 35
%Modulus (Remainder)17 % 52
++Incrementlet x = 5; x++6
--Decrementlet y = 5; y--4

Arithmetic Operator Examples

let x = 10;
let y = 3;

console.log(x + y); // Output: 13
console.log(x - y); // Output: 7
console.log(x * y); // Output: 30
console.log(x / y); // Output: 3.3333333333333335
console.log(x % y); // Output: 1
console.log(x ** y); // Output: 1000

Increment and Decrement Behavior

  • x++ or x--: value changes after evaluation
  • ++x or --x: value changes before evaluation

Comparison Operators in JavaScript

Comparison operators compare two values and return a Boolean value (true or false). They are commonly used in conditional logic.

JavaScript Comparison Operator Reference

OperatorDescriptionExampleOutput
==Equal to5 == '5'true
===Strict equal (value + type)5 === '5'false
!=Not equal5 != '6'true
!==Strict not equal5 !== '5'true
>Greater than10 > 5true
<Less than3 < 7true
>=Greater than or equal8 >= 8true
<=Less than or equal4 <= 3false

Comparison Operator Examples

let a = 5;
let b = '5';

console.log(a == b);   // true (loose equality)
console.log(a === b);  // false (strict equality)
console.log(a != b);   // false
console.log(a !== b);  // true
console.log(a > 3);    // true
console.log(a < 10);   // true
console.log(a >= 5);   // true
console.log(a <= 4);   // false

Difference Between == and ===

  • == compares values only
  • === compares both value and type (recommended)

Logical Operators in JavaScript

Logical operators are used to combine or invert Boolean values and expressions.

JavaScript Logical Operators

OperatorDescriptionExampleOutput
&&Logical ANDtrue && falsefalse
``Logical OR
!Logical NOT!truefalse

Logical Operator Examples

let isAdult = true;
let hasPermission = false;

let canEnter = isAdult && hasPermission; // false
let canView = isAdult || hasPermission;  // true
let isDenied = !hasPermission;           // true

Logical Operator Behavior

  • && → true only if both conditions are true
  • || → true if at least one condition is true
  • ! → reverses the Boolean value

Assignment Operators in JavaScript

Assignment operators assign values to variables and include shorthand forms for updating values.

JavaScript Assignment Operators

OperatorDescriptionExampleEquivalent
=Assignmentx = 10
+=Addition assignmentx += 5x = x + 5
-=Subtraction assignmentx -= 3x = x - 3
*=Multiplication assignmentx *= 2x = x * 2
/=Division assignmentx /= 4x = x / 4
%=Modulus assignmentx %= 2x = x % 2

Assignment Operator Examples

let x = 10;

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

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

x *= 2;
console.log(x); // 24

x /= 4;
console.log(x); // 6

x %= 3;
console.log(x); // 0

x = 2;
x **= 3;
console.log(x); // 8

Conditional (Ternary) Operator in JavaScript

The ternary operator provides a concise alternative to an if-else statement.

Ternary Operator Syntax

let result = condition ? valueIfTrue : valueIfFalse;

Basic Ternary Operator Example

let age = 20;
let access = age >= 18 ? "Allowed" : "Denied";

console.log(access); // Output: "Allowed"

Nested Ternary Operator Example

let score = 85;
let grade = score >= 90 ? 'A' : score >= 80 ? 'B' : 'C';

console.log(grade); // Output: "B"

⚠️ Best Practice:
Avoid excessive nesting of ternary operators as it reduces readability. Use if-else when logic becomes complex.


Summary of JavaScript Operators

JavaScript provides a rich set of operators to handle calculations, comparisons, logic, and assignments:

  • Arithmetic operators perform mathematical operations
  • Comparison operators evaluate conditions
  • Logical operators combine or invert Boolean expressions
  • Assignment operators simplify value updates
  • Ternary operator offers a concise conditional syntax

Understanding and using these operators effectively is essential for writing clear, expressive, and efficient JavaScript code.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *