DEV Community

Indumathy
Indumathy

Posted on

JavaScript Operators

What are Operators?
Operators are symbols used to perform operations on values or variables.
Example:
let result = 5 + 3;
Here, '+' is an operator that adds two numbers.

Types of JavaScript Operators

  1. Arithmetic Operators
  2. Assignment Operators
  3. Comparison Operators
  4. Logical Operators
  5. String Operators

1. Arithmetic Operators

Used for mathematical calculations.

  • Addition (5 + 3)
  • Subtraction (5 - 3)
  • Multiplication (5 * 3) / Division (10 / 2) % Modulus (10 % 3)

Increment / Decrement:

x++ Post Increment
++x Pre Increment
x-- Post Decrement
--x Pre Decrement

Example:

let x = 5;
x++;
console.log(x);

Output: 6

2. Assignment Operators

Used to assign values to variables.

= x = 5
+= x += 5 (x = x + 5)
-= x -= 5 (x = x - 5)
*= x *= 5 (x = x * 5)
/= x /= 5 (x = x / 5)

Example:
let x = 10;
x += 5;
console.log(x);

Output: 15

3. Comparison Operators

Used to compare two values. Result is true or false.

== 5 == '5' → true
=== 5 === '5' → false
!= 5 != 3 → true

5 > 3 → true
< 5 < 3 → false
= 5 >= 5 → true
<= 5 <= 8 → true

Note:
== checks value only
=== checks value and data type

4. Logical Operators

Used to combine or check conditions.

&& AND
|| OR
! NOT

Example AND:(returns true only if both conditions true)

let age = 20;
if(age > 18 && age < 60){
console.log('Eligible for job');
}
Output: Eligible for job

Example OR:(returns true if atleast one condition passed)

let marks = 35;
if(marks >= 40 || marks >= 35){
console.log('You passed');
}

Output: You passed

5. String Operator

Used to combine strings.

Example:
let first = 'Hello';
let second = 'World';
console.log(first + ' ' + second);

Output: Hello World

Conclusion
JavaScript operators are fundamental in programming. They help perform calculations, assign values, compare data, and combine conditions. Understanding operators is an important step for beginners learning JavaScript.

Top comments (0)