What Are Operators In JavaScript? See Examples

Operators in JavaScript are special symbols used to perform operations on variables and values. They are essential for calculations, comparisons, logic building, and decision-making in programs.

In this guide, we explain all major types of operators with clear examples.


What Are Operators?

Operators take one or more values (operands) and produce a result.

Example

let sum = 5 + 10;  // + is an operator

JavaScript provides several categories of operators, each serving a different purpose.


1. Arithmetic Operators

Used for mathematical operations.

OperatorMeaningExample
+Addition5 + 2 = 7
-Subtraction7 - 3 = 4
*Multiplication6 * 2 = 12
/Division10 / 2 = 5
%Modulus (remainder)10 % 3 = 1
**Exponent2 ** 3 = 8
++Incrementx++
--Decrementx--

Example

let a = 10;
let b = 3;
console.log(a % b);  // 1

2. Assignment Operators

Used to assign values.

OperatorMeaningExample
=Simple assignmentx = 10
+=Add and assignx += 5
-=Subtract and assignx -= 2
*=Multiply and assignx *= 3
/=Divide and assignx /= 2

Example

let x = 10;
x += 5;  
console.log(x);  // 15

3. Comparison Operators

Used to compare values. Output is true or false.

OperatorMeaning
==Equal to
===Strict equal (checks value + type)
!=Not equal
!==Strict not equal
>Greater than
<Less than
>=Greater or equal
<=Less or equal

Example

5 == "5";   // true (loose comparison)
5 === "5";  // false (strict comparison)

4. Logical Operators

Used in conditions and decision making.

OperatorMeaning
&&AND
`
!NOT

Example

let age = 20;
console.log(age > 18 && age < 30); // true

5. String Operators

The + operator joins strings.

Example

let first = "Hello";
let second = "World";
console.log(first + " " + second);  // "Hello World"

6. Ternary Operator

A compact if-else statement.

Syntax

condition ? valueIfTrue : valueIfFalse

Example

let age = 18;
let canVote = (age >= 18) ? "Yes" : "No";
console.log(canVote); // "Yes"

7. Type Operators

typeof – returns data type

instanceof – checks if an object belongs to a class

Examples

typeof "Hello";   // "string"
[] instanceof Array;  // true

8. Bitwise Operators (Advanced)

Work on binary numbers.

OperatorMeaning
&AND
``
^XOR
~NOT
<<Left shift
>>Right shift

Example

console.log(5 & 1); // 1

Conclusion

Operators allow JavaScript to perform calculations, make decisions, compare values, and manage program logic. Mastering them is essential for building any type of application — from simple scripts to advanced web apps.


📌 Citations

🔗 View other articles about Javascript:
https://savanka.com/category/learn/js/

🔗 External Javascript Documentation:
https://www.w3schools.com/js/

Leave a Comment

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *