DEV Community

Cover image for Operators in js
Vigneshwaran V
Vigneshwaran V

Posted on

Operators in js

what is operators?

An operator in JavaScript is a special symbol or keyword used to perform operations on values and variables. The values that the operator acts upon are called operands.

  • For example, in the expression let sum = 10 + 5;, the + symbol is the operator, while 10 and 5 are the operands.

Classification by Number of Operands

JavaScript operators are structurally divided into three main categories based on how many values they manipulate:

  • Unary Operators: Work on a single operand (e.g., ++x or typeof x). Ex:
// Case 1: Postfix
let x = 12;
let y = x++;
console.log(x);
console.log(y);

// Case 2: Preifix
x = 10;
y = ++x;
console.log(x);
console.log(y);
Enter fullscreen mode Exit fullscreen mode
  • Binary Operators: Work on two operands (e.g., x + y or x == y). Ex:
10 + 20
// 30

20 - 5
// 15

30 / 6
// 5
Enter fullscreen mode Exit fullscreen mode

Ternary Operator: Works on three operands. JavaScript has exactly one, the conditional operator (condition ? value1 : value2).
Ex:

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

Operators types:

1. Arithmetic Operators

Used for mathematical calculations.


console.log(10 + 5); // 15
console.log(10 % 3); // 1
Enter fullscreen mode Exit fullscreen mode

2. Assignment Operators

Used to assign values to variables.


let x = 10;
x += 5;        //x=x+5
console.log(x); // 15
Enter fullscreen mode Exit fullscreen mode

3. Comparison Operators

Used to compare values. They return true or false.


console.log(5 == "5");   // true
console.log(5 === "5");  // false
console.log(10 > 5);     // true
Enter fullscreen mode Exit fullscreen mode

4.logical operators

Logical operators in JavaScript are symbols used to combine or modify expressions to determine the logic between values and control program flow.

  • While they are typically used with Boolean (true/false) values in conditional statements like if blocks, they can actually be applied to values of any data type.

1. Logical AND (&&)

  • Returns true only when all conditions are true.

  • The && operator stops at the first falsy value it encounters and returns it. If all values are truthy, it returns the last value.
    Syntax

condition1 && condition2
Enter fullscreen mode Exit fullscreen mode

Example:

let age = 20;

console.log(age > 18 && age < 30);
Enter fullscreen mode Exit fullscreen mode

Output:

true
Enter fullscreen mode Exit fullscreen mode

2. Logical OR (||)

  • Returns true if at least one condition is true.

  • The || operator stops at the first truthy value it encounters and returns it. If all values are falsy, it returns the last value.

Syntax:

condition1 || condition2
Enter fullscreen mode Exit fullscreen mode

Example

let age = 15;

console.log(age < 18 || age > 60);
Enter fullscreen mode Exit fullscreen mode

Output

true 
Enter fullscreen mode Exit fullscreen mode

3. Logical NOT (!)

Reverses the result.

  • true becomes false.

  • false becomes true.
    Unlike && and ||, the ! operator always returns a strict boolean (true or false). It converts the operand to its boolean equivalent and then flips it.

Example

let isLoggedIn = true;

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

Output:

false
Enter fullscreen mode Exit fullscreen mode

5. Increment and Decrement Operators

  • ++ operator is used to increase the value by 1.

  • -- operator is used to decrease the value by 1.

let n = 5;

n++;
console.log(n); // 6

n--;
console.log(n); // 5
Enter fullscreen mode Exit fullscreen mode

Pre and Post Increment

In JavaScript, the main difference between pre-increment and post-increment is the order in which the value is changed and returned. Both operations add 1 to the target variable, but they behave differently when used inside expressions, assignments, or functions.

  • Pre-Increment ++x Increments first, then returns.New value (after adding 1).

  • Post-Increment x++ Returns first, then increments.Old value (before adding 1).

  • same rule is applicable for decrement operator.

Example

let a = 5;

console.log(++a); // 6 (increment first)

let b = 5;
console.log(b++); // 5 (use first, then increment)
console.log(b);   // 6
Enter fullscreen mode Exit fullscreen mode

Remember

  • && → All conditions must be true.

  • || → At least one condition must be true.

  • ! → Opposite of the condition.

References

https://www.freecodecamp.org/news/unary-binary-ternary-operators-javascript/

https://www.geeksforgeeks.org/javascript/javascript-operators/

Top comments (0)