JavaScript Operators (original) (raw)

Last Updated : 27 Dec, 2024

JavaScript operators are symbols or keywords used to perform operations on values and variables. They are the building blocks of JavaScript expressions and can manipulate data in various ways.

JavaScript-Operators

JavaScript Operators

**There are various operators supported by JavaScript.

1. JavaScript Arithmetic Operators

Arithmetic Operators perform mathematical calculations like addition, subtraction, multiplication, etc.

JavaScript `

const sum = 5 + 3; // Addition const diff = 10 - 2; // Subtraction const p = 4 * 2; // Multiplication const q = 8 / 2; // Division console.log(sum, diff, p, q);

`

2. JavaScript Assignment Operators

Assignment operators are used to assign values to variables. They can also perform operations like addition or multiplication before assigning the value.

JavaScript `

let n = 10; n += 5; n *= 2; console.log(n);

`

3. JavaScript Comparison Operators

Comparison operators compare two values and return a boolean (true or false). They are useful for making decisions in conditional statements.

JavaScript `

console.log(10 > 5); console.log(10 === "10");

`

4. JavaScript Logical Operators

Comparison operators are mainly used to perform the logical operations that determine the equality or difference between the values.

JavaScript `

const a = true, b = false; console.log(a && b); // Logical AND console.log(a || b); // Logical OR

`

5. JavaScript Bitwise Operators

Bitwise operators perform operations on binary representations of numbers.

JavaScript `

const res = 5 & 1; // Bitwise AND console.log(res);

`

6. JavaScript Ternary Operator

The ternary operator is a shorthand for conditional statements. It takes three operands.

JavaScript `

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

`

condition ? expression1 : expression2 evaluates expression1 if the condition is true, otherwise evaluates expression2.

7. JavaScript Comma Operator

**Comma Operator (,) mainly evaluates its operands from left to right sequentially and returns the value of the rightmost operand.

JavaScript `

let n1, n2 const res = (n1 = 1, n2 = 2, n1 + n2); console.log(res);

`

8. JavaScript Unary Operators

Unary operators operate on a single operand (e.g., increment, decrement).

JavaScript `

let x = 5; console.log(++x); // Pre-increment console.log(x--); // Post-decrement (Output: 6, then x becomes 5)

`

9. JavaScript Relational Operators

JavaScript **Relational operators are used to compare its operands and determine the relationship between them. They return a Boolean value (true or false) based on the comparison result.

JavaScript `

const obj = { length: 10 }; console.log("length" in obj); console.log([] instanceof Array);

`

10. JavaScript BigInt Operators

BigInt operators allow calculations with numbers beyond the safe integer range.

JavaScript `

const big1 = 123456789012345678901234567890n; const big2 = 987654321098765432109876543210n; console.log(big1 + big2);

`

Output

1111111110111111111011111111100n

11. JavaScript String Operators

JavaScript String Operators include concatenation (+) and concatenation assignment (+=), used to join strings or combine strings with other data types.

JavaScript `

const s = "Hello" + " " + "World"; console.log(s);

`

12. JavaScript Chaining Operator (?.)

The optional chaining operator allows safe access to deeply nested properties without throwing errors if the property doesn’t exist.

JavaScript `

const obj = { name: "Aman", address: { city: "Delhi" } }; console.log(obj.address?.city); console.log(obj.contact?.phone);

`