Operators are essential components in JavaScript that allow you to perform various operations on variables and values. This guide covers different types of JavaScript operators, including arithmetic, assignment, comparison, logical, and more.

1. Arithmetic Operators:

Arithmetic operators are used to perform mathematical calculations.

OperatorDescriptionExampleResult
+Addition5 + 38
-Subtraction5 - 32
*Multiplication5 * 315
/Division6 / 32
%Modulus (Remainder)5 % 32
++Incrementlet a = 1; a++2
--Decrementlet a = 1; a--0

Example:

let a = 10;
let b = 5;
let sum = a + b; // sum is 15
let difference = a - b; // difference is 5
let product = a * b; // product is 50
let quotient = a / b; // quotient is 2
let remainder = a % b; // remainder is 0

2. Assignment Operators:

Assignment operators assign values to variables.

OperatorDescriptionExampleResult
=Assignmentx = 5x = 5
+=Addition Assignmentx += 5x = x + 5
-=Subtraction Assignmentx -= 5x = x - 5
*=Multiplication Assignmentx *= 5x = x * 5
/=Division Assignmentx /= 5x = x / 5
%=Modulus Assignmentx %= 5x = x % 5

Example:

let x = 10;
x += 5; // x is now 15
x -= 3; // x is now 12
x *= 2; // x is now 24
x /= 4; // x is now 6
x %= 2; // x is now 0

3. Comparison Operators:

Comparison operators compare two values and return a boolean value (true or false).

OperatorDescriptionExampleResult
==Equal to5 == 5true
===Equal value and type5 === "5"false
!=Not equal5 != 3true
!==Not equal value or type5 !== "5"true
>Greater than5 > 3true
<Less than5 < 3false
>=Greater than or equal to5 >= 5true
<=Less than or equal to5 <= 3false

Example:

let a = 10;
let b = 20;
console.log(a == b); // false
console.log(a != b); // true
console.log(a < b); // true
console.log(a > b); // false
console.log(a <= 10); // true
console.log(a === "10"); // false

4. Logical Operators:

Logical operators are used to combine multiple comparison operations.

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

Example:

let age = 25;
let hasLicense = true;
let canDrive = age >= 18 && hasLicense; // true

let isAdult = age >= 18;
let canVote = isAdult || hasLicense; // true

let isMinor = !isAdult; // false

5. String Operators:

String operators are used to concatenate (combine) strings.

OperatorDescriptionExampleResult
+Concatenation"Hello" + " World""Hello World"
+=Concatenation Assignmentlet text = "Hello"; text += " World";"Hello World"

Example:

let greeting = "Hello, ";
let name = "Alice";
let welcomeMessage = greeting + name; // "Hello, Alice"

6. Conditional (Ternary) Operator:

The conditional (ternary) operator is a shorthand for the if...else statement.

Syntax:

condition ? expressionIfTrue : expressionIfFalse

Example:

let age = 20;
let canVote = (age >= 18) ? "Yes" : "No";
console.log(canVote); // "Yes"

7. Type Operators

Type operators are used to identify the type of a variable.

OperatorDescriptionExampleResult
typeofReturns the typetypeof "John""string"
instanceofReturns true if an object is an instance of a classobj instanceof Objecttrue

Example:

let name = "Alice";
console.log(typeof name); // "string"

let date = new Date();
console.log(date instanceof Date); // true

Conclusion:

JavaScript operators are powerful tools that enable you to perform a wide range of operations, from arithmetic calculations and value assignments to comparisons and logical operations. Understanding how to use these operators effectively is essential for writing efficient and functional JavaScript code. By mastering these operators, you can manipulate data, make decisions, and create complex expressions with ease.