DEV Community

Anees Abdul
Anees Abdul

Posted on

Operators & Expressions in JavaScript

What are Operators in JavaScript?

  • Operators are symbols used to perform operations on values and variables.

Types of Operators in JavaScript
1. Arithmetic Operators:

Used to perform mathematical calculations
Operators List:

+ → Addition
- → Subtraction
* → Multiplication
/ → Division
% → Modulus
Enter fullscreen mode Exit fullscreen mode
let a = 10;
let b = 5;

console.log(a + b); // 15
console.log(a - b); // 5
console.log(a * b); // 50
console.log(a / b); // 2
console.log(a % b); // 0
Enter fullscreen mode Exit fullscreen mode

2. Assignment Operators:

Used to assign values to variables

let x = 10;

x += 5;  // x = x + 5 → 15
x -= 2;  // x = x - 2 → 13
Enter fullscreen mode Exit fullscreen mode
=
+=
-=
*=
/=
Enter fullscreen mode Exit fullscreen mode

3. Comparison Operators:

Used to compare two values (returns true/false)

console.log(10 > 5);   // true
console.log(10 == "10"); // true
console.log(10 === "10"); // false
Enter fullscreen mode Exit fullscreen mode
== → Equal (value only)
=== → Strict Equal (value + type)
!= → Not equal
> < >= <=
Enter fullscreen mode Exit fullscreen mode

4. Logical Operators:

Used to combine conditions

let age = 25;

console.log(age > 18 && age < 30); // true
console.log(age > 18 || age < 20); // true
console.log(!(age > 18));          // false
Enter fullscreen mode Exit fullscreen mode
&& → AND
|| → OR
! → NOT
Enter fullscreen mode Exit fullscreen mode

5. Unary Operators:
Operate on one value

let a = 5;

a++; // increment → 6
a--; // decrement → 5
Enter fullscreen mode Exit fullscreen mode
  1. Type Operators (Need to be discussed)

Used to check data types

let name = "Anees";

console.log(typeof name); // string
Enter fullscreen mode Exit fullscreen mode

Type Coercion:
JavaScript sometimes automatically converts types

console.log(10 - "2"); // 8
console.log(10 + "2"); // "102"
Enter fullscreen mode Exit fullscreen mode

Top comments (0)