DEV Community

Cover image for OPERATORS
Vinoth Kumar
Vinoth Kumar

Posted on

OPERATORS

OPRATORS : 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.

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

EXAMPLE:

let x = (100 + 50) * a;
Enter fullscreen mode Exit fullscreen mode

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

EXAMPLE:

let x = 10;
x += 5;
Enter fullscreen mode Exit fullscreen mode

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

EXAMPLE:

let text1 = "20";
let text2 = "5";
let result = text1 < text2;
Enter fullscreen mode Exit fullscreen mode

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

EXAMPLE:

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

Bitwise operators : perform operations on binary representations of numbers.

EXAMPLE:

const res = 5 & 1; // Bitwise AND
console.log(res);
Enter fullscreen mode Exit fullscreen mode

Ternary operator : is a shorthand for conditional statements. It takes three operands.

EXAMPLE:

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

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

EXAMPLE:

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

Unary operators : operate on a single operand.

EXAMPLE:

let x = 5;

console.log(+x);
console.log(-x);

console.log(++x);
console.log(--x);

console.log(!x);
Enter fullscreen mode Exit fullscreen mode

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.

EXAMPLE:

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

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

EXAMPLE:

const big1 = 123456789012345678901234567890n;
const big2 = 987654321098765432109876543210n;
console.log(big1 + big2);
Enter fullscreen mode Exit fullscreen mode

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

EXAMPLE:

const s = "Hello" + " " + "World";
console.log(s);
Enter fullscreen mode Exit fullscreen mode

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

EXAMPLE:

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

Top comments (0)