JavaScript operators are symbols that perform operations on operands, such as variables or values. There are various types of operators in JavaScript, and they can be categorized into the following groups:

1. Arithmetic Operators: These perform basic arithmetic operations like addition, subtraction, multiplication, division, etc.

let a = 10;
let b = 5;

console.log(a + b); // Addition: 15
console.log(a - b); // Subtraction: 5
console.log(a * b); // Multiplication: 50
console.log(a / b); // Division: 2
console.log(a % b); // Modulus (remainder): 0
console.log(a ** b); // Exponentiation: 100000

2. Assignment Operators: These assign values to variables, often in combination with an operation.

 

let x = 10;
x += 5; // Equivalent to: x = x + 5
console.log(x); // 15

let y = 20;
y *= 2; // Equivalent to: y = y * 2
console.log(y); // 40

3. Comparison Operators: These compare values and return a Boolean result (true or false).

let p = 5;
let q = 10;

console.log(p === q); // Equal: false
console.log(p !== q); // Not Equal: true
console.log(p < q);   // Less than: true
console.log(p > q);   // Greater than: false
console.log(p <= q);  // Less than or equal: true
console.log(p >= q);  // Greater than or equal: false

4. Logical Operators: These perform logical operations on values or expressions.

let isTrue = true;
let isFalse = false;

console.log(isTrue && isFalse); // Logical AND: false
console.log(isTrue || isFalse); // Logical OR: true
console.log(!isFalse);          // Logical NOT: true

5. Unary Operators: These operate on a single operand.

let num = 5;

console.log(-num); // Negation: -5
console.log(+num); // Unary plus: 5 (Doesn't change the value)
console.log(++num); // Increment: 6
console.log(--num); // Decrement: 5

6. Conditional (Ternary) Operator: This is a shorthand for an if statement.

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

 

7. Bitwise Operators: These perform bitwise operations on the binary representations of operands.

let a = 5; // Binary: 0101
let b = 3; // Binary: 0011

console.log(a & b); // Bitwise AND: 0001 (1 in decimal)
console.log(a | b); // Bitwise OR: 0111 (7 in decimal)
console.log(a ^ b); // Bitwise XOR: 0110 (6 in decimal)
console.log(~a);    // Bitwise NOT: 1010 (-6 in decimal)
console.log(a << 1); // Left shift by 1: 1010 (10 in decimal)
console.log(a >> 1); // Right shift by 1: 0010 (2 in decimal)

8. Type Operators:

  • typeof: Returns a string representing the type of the operand.
  • instanceof: Tests if an object is an instance of a specific class.

console.log(typeof "hello"); // "string"
console.log(typeof 42);      // "number"
console.log(typeof true);    // "boolean"

class Person {}
let person = new Person();
console.log(person instanceof Person); // true

9. Grouping Operator: Parentheses are used to group expressions and control the order of evaluation.

let result = (2 + 3) * 4;
console.log(result); // 20

 

These are the main categories of operators in JavaScript, and they cover a wide range of operations that you can perform on values and variables. Keep in mind that understanding how operators work and how they interact with operands is crucial for writing effective and meaningful JavaScript code.

Latest Tutorial

Newly published tutorials

Laravel - How to upload file to AWS S3 Bucket

Explore how we can upload file using the laravel on Amazon S3 bucket.

How to Make an AWS S3 Bucket Public

Learn how we can make an AWS S3 bucket public

Laravel 10 - Tutorial on how to use jQuery to validate the form

Learn how to use jQuery in Laravel 10 to validate the form