DEV Community

ASHWINTH
ASHWINTH

Posted on Edited on

Js Operators

Operators are special symbols or keywords that tell JavaScript to perform an operation on one or more values. For example, operators can be used to perform calculations, compare values, assign values, and combine conditions.
In this blog, we’ll explore the different types of JavaScript operators with simple examples.

  1. Arithmetic Operators Arithmetic operators are used to perform mathematical calculations. Operator Meaning Example Result
  2. Addition 10 + 5 15
  3. Subtraction 10 - 5 5
  4. Multiplication 10 * 5 50 / Division 10 / 5 2 % Remainder 10 % 3 1 ** Exponentiation 2 ** 3 8
Example
let a = 10;
let b = 3;

console.log(a + b); // 13
console.log(a - b); // 7
console.log(a * b); // 30
console.log(a / b); // 3.333...
console.log(a % b); // 1
Enter fullscreen mode Exit fullscreen mode
  1. Assignment Operators Assignment operators are used to assign values to variables. The most basic assignment operator is =.
let age = 20;
JavaScript also provides shorthand assignment operators.
Operator    Example Equivalent to
=   x = 5   x = 5
+=  x += 5  x = x + 5
-=  x -= 5  x = x - 5
*=  x *= 5  x = x * 5
/=  x /= 5  x = x / 5
Example
let score = 10;

score += 5;
Enter fullscreen mode Exit fullscreen mode

console.log(score); // 15

  1. Comparison Operators Comparison operators are used to compare two values. They return either true or false.
Operator    Meaning
==  Equal value
=== Equal value and type
!=  Not equal value
!== Not equal value or type
>   Greater than
<   Less than
>=  Greater than or equal to
<=  Less than or equal to
Example
let age = 18;

console.log(age >= 18); // true
console.log(age < 18);  // false
Enter fullscreen mode Exit fullscreen mode

console.log(5 == "5");  // true
console.log(5 === "5"); // false
Enter fullscreen mode Exit fullscreen mode
  1. Logical Operators Logical operators are commonly used with conditions. There are three main logical operators: && — AND || — OR ! — NOT AND (&&) Returns true when both conditions are true.
let age = 25;
let hasID = true;

console.log(age >= 18 && hasID); // true
OR (||)
Returns true when at least one condition is true.
let isStudent = false;
let isEmployee = true;

console.log(isStudent || isEmployee); // true
NOT (!)
Reverses a boolean value.
let loggedIn = true;
Enter fullscreen mode Exit fullscreen mode

console.log(!loggedIn); // false

  1. Increment and Decrement Operators The increment operator ++ increases a value by 1. The decrement operator -- decreases a value by 1.
let count = 5;

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

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

These operators are frequently used in loops.

  1. Ternary Operator The ternary operator is a short way to write a simple if...else condition. Its syntax is: condition ? valueIfTrue : valueIfFalse; E
xample
let age = 20;

let result = age >= 18 ? "Adult" : "Minor";

console.log(result); // Adult
Instead of writing:
if (age >= 18) {
    result = "Adult";
} else {
    result = "Minor";
}
Enter fullscreen mode Exit fullscreen mode

the ternary operator allows us to write the same simple decision in one line.

Top comments (0)