Operators in JavaScript | AsgarTech
Operators in JavaScript are symbols or keywords that perform specific operations on one or more operands (values or variables). There are various types of operators in JavaScript, including:
Arithmetic operators: perform mathematical operations on operands.
Example:
let x = 10;
let y = 5;
console.log(x + y); // 15
console.log(x - y); // 5
console.log(x * y); // 50
console.log(x / y); // 2
console.log(x % y); // 0
Assignment operators: assign a value to a variable.
Example:
let x = 10;
x += 5; // equivalent to x = x + 5
console.log(x); // 15
Comparison operators: compare two operands and return a Boolean value (true or false).
let x = 10;
let y = 5;
console.log(x > y); // true
console.log(x < y); // false
console.log(x == y); // false
console.log(x != y); // true
Logical operators: perform logical operations on two or more Boolean values.
let x = true;
let y = false;
console.log(x && y); // false
console.log(x || y); // true
console.log(!x); // false
Bitwise operators: perform bitwise operations on binary representations of integers.
let x = 5; // 101 in binary
let y = 3; // 011 in binary
console.log(x & y); // 001 (bitwise AND)
console.log(x | y); // 111 (bitwise OR)
console.log(x ^ y); // 110 (bitwise XOR)
console.log(~x); // -6 (bitwise NOT)
console.log(x << 1); // 10 (left shift)
console.log(x >> 1); // 2 (right shift)
These are just a few examples of the many operators available in JavaScript. Understanding how to use them effectively can help you write more efficient and concise code.
No comments: