JavaScript Operators

JavaScript supports a variety of operators, which are symbols used to perform operations on variables and values. Here’s an overview of some common types of operators in JavaScript:

1. Arithmetic Operators:

let x = 10;
let y = 5;

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

2. Assignment Operators:

let a = 10;

a += 5; // Equivalent to a = a + 5
console.log(a); // Output: 15

a -= 3; // Equivalent to a = a - 3
console.log(a); // Output: 12

a *= 2; // Equivalent to a = a * 2
console.log(a); // Output: 24

a /= 4; // Equivalent to a = a / 4
console.log(a); // Output: 6

3. Comparison Operators:

let p = 5;
let q = "5";

console.log(p == q); // Equality (loose equality): true
console.log(p === q); // Strict equality (checks value and type): false
console.log(p != q); // Inequality (loose inequality): false
console.log(p !== q); // Strict inequality: true

console.log(x > y); // Greater than: true
console.log(x < y); // Less than: false
console.log(x >= y); // Greater than or equal to: true
console.log(x <= y); // Less than or equal to: false

4. Logical Operators:

let isTrue = true;
let isFalse = false;

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

5. Unary Operators:

let num = 5;

console.log(-num); // Unary negation: -5
console.log(+num); // Unary plus: 5 (no effect)
console.log(++num); // Increment: 6
console.log(--num); // Decrement: 5

6. Conditional (Ternary) Operator:

let age = 20;
let eligibility = (age >= 18) ? "Eligible" : "Not eligible";
console.log(eligibility); // Output: "Eligible"

These are some of the fundamental operators in JavaScript. They allow you to perform a wide range of operations, from basic arithmetic to complex conditional logic. Understanding and using these operators is essential for writing effective and concise JavaScript code.