DEV Community

Cover image for JavaScript Operators: The Basics You Need to Know
Souvik Guha Roy
Souvik Guha Roy

Posted on

JavaScript Operators: The Basics You Need to Know

When writing JavaScript programs, we often need to perform calculations, compare values, or make decisions.
To do these tasks, JavaScript provides operators.

Operators are one of the most fundamental parts of programming because they allow us to work with values and variables.

In this article, we will explore the most commonly used operators in JavaScript and see how they work with simple examples.


Topics Covered

In this blog we will learn:

  • What operators are
  • Arithmetic operators (+, -, *, /, %)
  • Comparison operators (==, ===, !=, >, <)
  • Logical operators (&&, ||, !)
  • Assignment operators (=, +=, -=)

What Are Operators?

An operator is a symbol used to perform an operation on values or variables.

Example:

let result = 5 + 3;
Enter fullscreen mode Exit fullscreen mode

Here:

  • 5 and 3 are operands
  • + is the operator
  • The result is 8

Operators help us perform tasks like:

  • Mathematical calculations
  • Comparing values
  • Combining conditions
  • Assigning values to variables

Arithmetic Operators

Arithmetic operators are used to perform basic mathematical calculations.

Operator Description Example
+ Addition 5 + 3
- Subtraction 5 - 3
* Multiplication 5 * 3
/ Division 6 / 2
% Modulus (remainder) 7 % 2

Example:

let a = 10;
let b = 5;

console.log(a + b); 
console.log(a - b); 
console.log(a * b); 
console.log(a / b); 
console.log(a % b);
Enter fullscreen mode Exit fullscreen mode

Output:

15
5
50
2
0
Enter fullscreen mode Exit fullscreen mode

The % operator returns the remainder after division.

Example:

7 % 2 = 1
Enter fullscreen mode Exit fullscreen mode

Comparison Operators

Comparison operators are used to compare two values.
They always return a boolean value (true or false).

Operator Description
== Equal to (value only)
=== Strict equal (value and type)
!= Not equal
> Greater than
< Less than

Example:

let x = 5;
let y = "5";

console.log(x == y);
console.log(x === y);
Enter fullscreen mode Exit fullscreen mode

Output:

true
false
Enter fullscreen mode Exit fullscreen mode

Why?

  • == compares values only
  • === compares value and data type

So:

5 == "5"    true
5 === "5"   false
Enter fullscreen mode Exit fullscreen mode

For this reason, developers usually prefer === in modern JavaScript.


Logical Operators

Logical operators are used to combine multiple conditions.

Operator Meaning
&& AND
` ` OR
! NOT

Logical AND (&&)

Returns true only if both conditions are true.

Example:

let age = 20;

if (age > 18 && age < 30) {
  console.log("You are eligible");
}
Enter fullscreen mode Exit fullscreen mode

Logical OR (||)

Returns true if at least one condition is true.

Example:

let isWeekend = true;
let isHoliday = false;

if (isWeekend || isHoliday) {
  console.log("You can relax today");
}
Enter fullscreen mode Exit fullscreen mode

Logical NOT (!)

Reverses a boolean value.

Example:

let isLoggedIn = false;

console.log(!isLoggedIn);
Enter fullscreen mode Exit fullscreen mode

Output:

true
Enter fullscreen mode Exit fullscreen mode

Truth Table for Logical Operators

| A | B | A && B | A || B |
| ----- | ----- | ------ | ------ |
| true | true | true | true |
| true | false | false | true |
| false | true | false | true |
| false | false | false | false |


Assignment Operators

Assignment operators are used to assign values to variables.

Operator Example Meaning
= x = 10 Assign value
+= x += 5 x = x + 5
-= x -= 3 x = x - 3

Example:

let score = 10;

score += 5;
console.log(score);

score -= 3;
console.log(score);
Enter fullscreen mode Exit fullscreen mode

Output:

15
12
Enter fullscreen mode Exit fullscreen mode

Assignment Practice

Let’s apply everything we learned.

1. Arithmetic operations

let a = 8;
let b = 4;

console.log(a + b);
console.log(a - b);
console.log(a * b);
console.log(a / b);
Enter fullscreen mode Exit fullscreen mode

2. Comparing values

let x = 10;
let y = "10";

console.log(x == y);
console.log(x === y);
Enter fullscreen mode Exit fullscreen mode

3. Using logical operators

let age = 22;
let hasID = true;

if (age >= 18 && hasID) {
  console.log("You can enter");
}
Enter fullscreen mode Exit fullscreen mode

Visualizing Operator Categories

Category Operators
Arithmetic +, -, *, /, %
Comparison ==, ===, !=, >, <
Logical &&, ` , !`
Assignment =, +=, -=

Final Thoughts

Operators are essential in JavaScript because they allow us to:

  • Perform calculations
  • Compare values
  • Combine conditions
  • Assign values to variables

By mastering these basic operators, you will be able to write more powerful and logical JavaScript programs.

As you continue learning, you will encounter many more operators, but these are the most commonly used ones in everyday coding.


Top comments (0)