Contents

Operators

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

Arithmetic Operators

Arithmetic operators are used to perform basic mathematical operations on numbers.

OperatorDescriptionExampleOutput
+Addition5 + 38
-Subtraction10 - 46
*Multiplication6 * 318
/Division15 / 35
%Modulus (Remainder)17 % 52
++Increment (adds 1)let x = 5; x++6
--Decrement (subtracts 1)let y = 5; y--4
Examples:
				
					let x = 10;
let y = 3;
console.log(x + y); // Outputs: 13
console.log(x - y); // Outputs: 7
console.log(x * y); // Outputs: 30
console.log(x / y); // Outputs: 3.3333333333333335
console.log(x % y); // Outputs: 1
console.log(x ** y); // Outputs: 1000



				
			
  • Increment and Decrement: When used as x++ or x--, the value is changed after the expression is evaluated. When used as ++x or --x, the value is changed before the expression is evaluated.

Comparison Operators

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

OperatorDescriptionExampleOutput
==Equal to5 == '5'true
===Strict equal to (type + value)5 === '5'false
!=Not equal to5 != '6'true
!==Strict not equal to5 !== '5'true
>Greater than10 > 5true
<Less than3 < 7true
>=Greater than or equal to8 >= 8true
<=Less than or equal to4 <= 3false
Examples:
				
					let a = 5;
let b = '5';
console.log(a == b); // Outputs: true (equality operator)
console.log(a === b); // Outputs: false (strict equality operator)
console.log(a != b); // Outputs: false (inequality operator)
console.log(a !== b); // Outputs: true (strict inequality operator)
console.log(a > 3); // Outputs: true
console.log(a < 10); // Outputs: true
console.log(a >= 5); // Outputs: true
console.log(a <= 4); // Outputs: false



				
			
  • == vs. ===: == compares values but not types (loose equality), while === compares both values and types (strict equality).

Logical Operators

Logical operators are used to combine multiple Boolean expressions or values and return a Boolean result.

OperatorDescriptionExampleOutput
&&Logical ANDtrue && falsefalse
||Logical OR true && false true 
!Logical NOT!truefalse
Examples:
				
					let isAdult = true;
let hasPermission = false;

let canEnter = isAdult && hasPermission; // false (both must be true)
let canView = isAdult || hasPermission;  // true (at least one must be true)
let isDenied = !hasPermission;           // true (negates the value)

				
			
  • && (AND): Returns true if both operands are true.
  • || (OR): Returns true if at least one operand is true.
  • ! (NOT): Returns the opposite Boolean value.

Assignment Operators

Assignment operators are used to assign values to variables. They also include shorthand operators for performing arithmetic operations and updating variables.

OperatorDescriptionExampleEquivalent to
=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
Examples:
				
					let x = 10;
x += 5; // Equivalent to x = x + 5
console.log(x); // Outputs: 15

x -= 3; // Equivalent to x = x - 3
console.log(x); // Outputs: 12

x *= 2; // Equivalent to x = x * 2
console.log(x); // Outputs: 24

x /= 4; // Equivalent to x = x / 4
console.log(x); // Outputs: 6

x %= 3; // Equivalent to x = x % 3
console.log(x); // Outputs: 0

x = 2;
x **= 3; // Equivalent to x = x ** 3
console.log(x); // Outputs: 8





				
			

Conditional (Ternary) Operator

The ternary operator (condition ? expression1 : expression2) is a shorthand way of writing an if-else statement. It checks a condition and returns one of two values based on whether the condition is true or false.

Syntax:
				
					let result = condition ? valueIfTrue : valueIfFalse;

				
			
Example:
				
					let age = 20;
let access = age >= 18 ? "Allowed" : "Denied";
console.log(access); // Output: "Allowed"

				
			

In this example, the condition (age >= 18) is evaluated. If it’s true, "Allowed" is assigned to access; otherwise, "Denied" is assigned.

Nested Ternary Example:
				
					let score = 85;
let grade = score >= 90 ? 'A' : score >= 80 ? 'B' : 'C';
console.log(grade); // Output: "B"

				
			

While you can nest ternary operators, it’s best to avoid excessive nesting as it can reduce code readability. In such cases, using an if-else statement is preferable.

Summary

JavaScript provides a variety of operators to perform arithmetic calculations, comparisons, logical operations, and value assignments. Arithmetic operators handle basic math, while comparison operators are used to evaluate conditions. Logical operators (&&, ||, !) allow you to combine or invert Boolean expressions. Assignment operators simplify updating variable values, and the conditional (ternary) operator offers a concise way to write if-else conditions. Understanding how to use these operators effectively is fundamental to writing functional and expressive JavaScript code.