Introduction to JavaScript Operators

JavaScript operators are symbols or keywords used to perform operations on values or variables. They help manipulate data, compare values and control the logic of your code. Operators are a core part of JavaScript, enabling developers to build functional and dynamic applications.

Types of JavaScript Operators

JavaScript provides several types of operators, including:

  1. Arithmetic Operators
  2. Assignment Operators
  3. Comparison Operators
  4. Logical Operators
  5. Bitwise Operators
  6. String Operators
  7. Conditional (Ternary) Operators
  8. Type Operators

1. Arithmetic Operators

Arithmetic operators perform mathematical calculations.

OperatorDescriptionExampleOutput
+Addition5 + 38
Subtraction10 – 46
*Multiplication7 * 214
/Division20 / 54
%Modulus (Remainder)9 % 21
**Exponentiation3 ** 29

Example:

let a = 10;
let b = 3;
console.log(a + b); // Output: 13
console.log(a % b); // Output: 1

2. Assignment Operators

Assignment operators assign values to variables.

OperatorDescriptionExampleEquivalent To
=Assignx = 5x = 5
+=Add and Assignx += 2x = x + 2
-=Subtract and Assignx -= 3x = x – 3
*=Multiply and Assignx *= 4x = x * 4
/=Divide and Assignx /= 5x = x / 5
%=Modulus and Assignx %= 6x = x % 6

Example:

let x = 10;
x += 5; // Same as x = x + 5
console.log(x); // Output: 15

3. Comparison Operators

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

OperatorDescriptionExampleOutput
==Equal to5 == “5”true
===Strict equal to5 === “5”false
!=Not equal to5 != “5”false
!==Strict not equal to5 !== “5”true
>Greater than10 > 5true
<Less than3 < 7true
>=Greater than or equal to8 >= 8true
<=Less than or equal to9 <= 6false

Example:

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

4. Logical Operators

Logical operators perform logical operations and are often used with Boolean values.

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

Example:

let hasLicense = true;
let hasCar = false;
console.log(hasLicense && hasCar); // Output: false

5. Bitwise Operators

Bitwise operators perform operations on binary representations of numbers.

OperatorDescriptionExampleOutput
&AND5 & 11
``OR`5
^XOR5 ^ 14
~NOT~5-6
<<Left shift5 << 110
>>Right shift5 >> 12

6. String Operators

String operators are used to manipulate text.

OperatorDescriptionExampleOutput
+Concatenation“Hello ” + “World”“Hello World”
+=Appendgreeting += “World”“Hello World”

Example:

let text = "JavaScript ";
text += "is awesome!";
console.log(text); // Output: JavaScript is awesome!

7. Conditional (Ternary) Operator

The ternary operator simplifies conditional statements into a single line.

condition ? expressionIfTrue : expressionIfFalse;

Example:

let age = 18;
let status = age >= 18 ? "Adult" : "Minor";
console.log(status); // Output: Adult

8. Type Operators

Type operators are used to check or manipulate data types.

OperatorDescriptionExampleOutput
typeofReturns the data typetypeof “Hello”“string”
instanceofChecks object instance[] instanceof Arraytrue

Leave a Comment