Skip to content

Latest commit

 

History

History
153 lines (128 loc) · 3.77 KB

File metadata and controls

153 lines (128 loc) · 3.77 KB

1. Arithmetic Operators

Operator Description
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus (Remainder)
++ Increment (Pre/Post)
-- Decrement (Pre/Post)

Example:

let a = 50, b = 100;
console.log(a + b); // 150
console.log(b - a); // 50
console.log(a * b); // 5000
console.log(b / a); // 2
console.log(b % a); // 0

// Increment & Decrement
console.log(++a); // Pre-increment: 51
console.log(a);   // Value after increment: 51
console.log(a++); // Post-increment: 51 (then a becomes 52)
console.log(a);   // Value after increment: 52
console.log(--b); // Pre-decrement: 99
console.log(b);   // Value after decrement: 99
console.log(b--); // Post-decrement: 99 (then b becomes 98)
console.log(b);   // Value after decrement: 98

2. Assignment Operators

Operator Example Equivalent To
= x = y Assign y to x
+= x += y x = x + y
-= x -= y x = x - y
*= x *= y x = x * y
/= x /= y x = x / y
%= x %= y x = x % y

Example:

let x = 10;
x += 5; // x = 15
x -= 2; // x = 13
x *= 2; // x = 26
x /= 2; // x = 13
x %= 3; // x = 1
console.log(x); // 1

3. Bitwise Operators

Operator Description
& Bitwise AND
` `
^ Bitwise XOR
~ Bitwise NOT
<< Left Shift
>> Right Shift
>>> Zero-fill Right Shift

Example:

let p = 2, q = 3;
console.log(p & q); // 2 (0010 & 0011 = 0010)
console.log(p | q); // 3 (0010 | 0011 = 0011)
console.log(p ^ q); // 1 (0010 ^ 0011 = 0001)

4. Comparison Operators

Operator Description
== Equal to (value)
=== Strictly equal to (value & type)
!= Not equal to
!== Strictly not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to

Example:

let num1 = 10, num2 = '10';
console.log(num1 == num2);  // true (checks only value)
console.log(num1 === num2); // false (checks value & type)
console.log(num1 != num2);  // false
console.log(num1 !== num2); // true
console.log(num1 > num2);   // false
console.log(num1 >= num2);  // true
console.log(num1 <= num2);  // true

5. Logical Operators

Operator Description
&& Logical AND
`
! Logical NOT

Truth Table:

| A | B | A && B | A || B | !A | |---|---|--------|--------|----| | 0 | 0 | 0 | 0 | 1 | | 0 | 1 | 0 | 1 | 1 | | 1 | 0 | 0 | 1 | 0 | | 1 | 1 | 1 | 1 | 0 |

Example:

let val1 = true, val2 = false;
console.log(val1 && val2); // false
console.log(val1 || val2); // true
console.log(!val2);        // true

6. Miscellaneous Operators

Ternary Operator (?:)

A shorthand for if-else statements.

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

typeof Operator

Returns the type of a variable.

console.log(typeof 123);      // number
console.log(typeof 'Hello');  // string
console.log(typeof true);     // boolean
console.log(typeof {});       // object
console.log(typeof undefined);// undefined