In JavaScript, Operators are special symbols used to perform operations on variables and values.
Operators are used to:
- Perform arithmetic calculations
- Compare values
- Combine conditions
- Assign values
- Concatenate strings
- Make decisions using conditions
JavaScript provides the following major types of operators:
- Arithmetic Operators
- Comparison Operators
- Logical Operators
- Ternary Operator
- String Operators
Arithmetic operators are used to perform mathematical calculations.
| Operator | Description | Example |
|---|---|---|
| + | Addition | a + b |
| - | Subtraction | a - b |
| * | Multiplication | a * b |
| / | Division | a / b |
| % | Modulus (Remainder) | a % b |
| ++ | Increment | a++ |
| -- | Decrement | a-- |
var a = 10;
var 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); // 0var a = 10;
console.log(a++); // 10
console.log(a); // 11
console.log(++a); // 12Explanation:
-
a++→ Post Increment First returns the value, then increments. -
++a→ Pre Increment First increments the value, then returns it.
var b = 5;
console.log(b--); // 5
console.log(b); // 4
console.log(--b); // 3Explanation:
b--→ Post Decrement--b→ Pre Decrement
Comparison operators compare two values and return either:
trueor
false| Operator | Meaning |
|---|---|
| == | Equal to |
| != | Not Equal to |
| === | Strict Equal |
| !== | Strict Not Equal |
| > | Greater Than |
| < | Less Than |
| >= | Greater Than or Equal |
| <= | Less Than or Equal |
var x = 10;
var y = "10";
console.log(x == y);Output:
true
Reason:
== checks only values.
var x = 10;
var y = "10";
console.log(x === y);Output:
false
Reason:
=== checks:
- Value
- Data Type
10 → Number
"10" → String
Hence false.
var x = 10;
var y = 20;
console.log(x != y); // true
console.log(x !== y); // true
console.log(x == y); // false
console.log(x === y); // false
console.log(x <= y); // true
console.log(x >= y); // falseLogical operators are used to combine conditions.
| Operator | Meaning |
|---|---|
| && | Logical AND |
| ! | Logical NOT |
Returns true only when all conditions are true.
var a = 10;
var b = 20;
var c = (a < b && b > a);
console.log(c);Output:
true
var a = 10;
var b = 20;
var c = (a > b && b > a);
console.log(c);Output:
false
Returns true if at least one condition is true.
var a = 10;
var b = 20;
var c = (a > b || b > a);
console.log(c);Output:
true
var a = 10;
var b = 20;
var c = (a > b || b < a);
console.log(c);Output:
false
Reverses the result.
var a = true;
console.log(!a);Output:
false
The Ternary Operator is a shorthand form of an if-else statement.
condition ? valueIfTrue : valueIfFalse;var n1 = 5;
var n2 = 10;
var result = n1 > n2 ? n1 : n2;
console.log(result);Output:
10
Explanation:
If n1 > n2
return n1
Else
return n2
var age = 20;
var status = age >= 18 ? "Eligible to Vote" : "Not Eligible";
console.log(status);Output:
Eligible to Vote
The + operator is also used for string concatenation.
It combines two or more strings.
var str1 = "Hello ";
var str2 = "World";
var result = str1 + str2;
console.log(result);Output:
Hello World
var fname = "Shaik";
var lname = "Basha";
console.log(fname + " " + lname);Output:
Shaik Basha
| == | === |
|---|---|
| Checks only value | Checks value and datatype |
| Performs type conversion | No type conversion |
| 10 == "10" → true | 10 === "10" → false |
-
Operators are symbols used to perform operations.
-
Arithmetic operators perform calculations.
-
Comparison operators return true or false.
-
Logical operators combine multiple conditions.
-
Ternary operator is a shorthand for if-else.
-
The + operator can be used for both:
- Addition
- String Concatenation
-
===is preferred over==because it checks both value and datatype.
JavaScript Operators are one of the fundamental concepts of programming. They are used to perform calculations, compare values, combine conditions, make decisions, and manipulate strings. Understanding operators is essential because they are used throughout JavaScript applications, from basic programs to advanced web development and frameworks such as React, Angular, and Node.js.