Thursday, June 20, 2024

OPERATORS

 

JavaScript provides a variety of operators that allow you to perform operations on variables and values. Here's an overview of the different types of operators:

JavaScript Operators

  • Operators in JavaScript can be categorized into several types based on their functionality:


Arithmetic Operators

  • Arithmetic operators perform mathematical calculations on numeric operands.

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 (remainder): 0
console.log(++x); // Increment: 11 (increments x by 1 before returning)
console.log(--y); // Decrement: 4 (decrements y by 1 before returning)


Assignment Operators

  • Assignment operators assign values to JavaScript variables.

let x = 10;
let y = 5;

x += y; // Equivalent to x = x + y
console.log(x); // Output: 15

x -= y; // Equivalent to x = x - y
console.log(x); // Output: 10

x *= y; // Equivalent to x = x * y
console.log(x); // Output: 50

x /= y; // Equivalent to x = x / y
console.log(x); // Output: 10

x %= y; // Equivalent to x = x % y
console.log(x); // Output: 0


Comparison Operators

  • Comparison operators are used to compare two values and return a Boolean result (true or false).

let a = 5;
let b = 10;

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


Equality Operators

  • Equality operators compare two values and return true if they are equal, false otherwise. == performs type coercion, while === checks both value and type.

let x = 5;
let y = '5';

console.log(x == y); // Loose equality: true (coerces types)
console.log(x === y); // Strict equality: false (different types)
console.log(x != y); // Not equal: false (coerces types)
console.log(x !== y); // Not strict equal: true (different types)


Ternary Operator

  • The ternary operator (condition ? expr1 : expr2) is a shorthand for an if...else statement.

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


Logical Operators

  • Logical operators are used to combine conditional statements.

let x = true;
let y = false;

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


Bitwise Operators

  • Bitwise operators perform bitwise operations on operands.

let a = 5; // Binary: 101
let b = 3; // Binary: 011

console.log(a & b); // Bitwise AND: 1 (binary 001)
console.log(a | b); // Bitwise OR: 7 (binary 111)
console.log(a ^ b); // Bitwise XOR: 6 (binary 110)
console.log(~a); // Bitwise NOT: -6 (binary 11111111111111111111111111111010 in 32-bit system)
console.log(a << 1); // Bitwise Left Shift: 10 (binary 1010)
console.log(a >> 1); // Bitwise Right Shift: 2 (binary 10)

No comments:

Post a Comment