JS Operators | RedoHub
HTML CSS Bootstrap JavaScript jQuery MySQL PHP Data Mining

JavaScript Operators

Written by RedoHub Editorial Team · Last updated: August 1, 2026
Advertisement

JavaScript operators are special symbols that tell JavaScript to perform an action on values and variables. They are used to calculate numbers, assign values, compare data, join text, and make logical decisions inside a program.


Why Operators Matter in JavaScript

Without operators, JavaScript could store values but would not be very useful in real programs. Operators let you build expressions such as adding prices, comparing ages, updating counters, or checking whether two values are equal.

In simple terms, operators are the working tools that help JavaScript do something with the data you write.


A Simple Operator Example

Here is a basic example that uses the assignment operator = and the addition operator +:

let x = 5;
let y = 6;
let total = x + y;

console.log(total);

In this example:

  • = assigns values to variables
  • + adds x and y

Main Types of JavaScript Operators

JavaScript has many operators, but beginners usually start with these main groups:

Operator Type Purpose Examples
Arithmetic Perform math calculations +, -, *, /, %
Assignment Assign values to variables =, +=, -=
Comparison Compare two values ==, ===, >, <
Logical Combine or reverse conditions &&, ||, !
String Join text values +, +=

Arithmetic Operators

Arithmetic operators are used for mathematical work such as addition, subtraction, multiplication, and division.

let a = 10;
let b = 4;

console.log(a + b);
console.log(a - b);
console.log(a * b);
console.log(a / b);

These operators are commonly used in totals, prices, scores, percentages, and counters.


Assignment Operators

Assignment operators store values inside variables. The basic assignment operator is =, but JavaScript also supports shortcut assignment operators.

let points = 10;
points += 5;
points -= 2;

console.log(points);

Here, += adds and assigns in one step, while -= subtracts and assigns in one step.


Comparison Operators

Comparison operators check whether two values are equal, different, greater, or smaller. The result is always true or false.

let age = 18;

console.log(age >= 18);
console.log(age === 18);
console.log(age != 20);
Note: Comparison operators are often used with if statements, loops, and form validation because they help JavaScript make decisions.

Logical Operators

Logical operators work with conditions. They let you combine multiple checks or reverse a result.

let isLoggedIn = true;
let isAdmin = false;

console.log(isLoggedIn && isAdmin);
console.log(isLoggedIn || isAdmin);
console.log(!isAdmin);

These operators are useful when a decision depends on more than one condition.


String Operators

The plus operator + can also join strings together. This is called string concatenation.

let firstName = "Mim";
let lastName = "Akter";
let fullName = firstName + " " + lastName;

console.log(fullName);
Tip: The same operator can behave differently depending on the values it works with. With numbers, + adds. With strings, it joins text.

Operators and Expressions

Operators are usually part of expressions. An expression combines values, variables, and operators to produce a result.

let result = (5 + 3) * 2;

In this line, the operators work together inside one expression, and JavaScript produces the final value.


Key Points to Remember

  • Operators tell JavaScript what action to perform on values and variables
  • Arithmetic operators are used for calculations
  • Assignment operators store or update variable values
  • Comparison operators return true or false
  • Logical operators work with conditions and decisions
  • The + operator can add numbers or join strings

Common Mistakes to Avoid

Using = instead of === in a condition. if (age = 18) assigns 18 to age and the condition is always truthy — it doesn't compare anything. This is one of the most common JavaScript bugs.
Assuming + always adds numbers. If either side is a string — often from a form input, which is always text — + concatenates instead: "5" + 5 gives "55", not 10.
Using == instead of ===. Loose equality coerces types before comparing, so "0" == 0 is true. Strict equality avoids that entirely.

Try It: The String-Concatenation Trap

This is the exact bug that happens when reading a number from an HTML input and forgetting to convert it:

let priceFromInput = "50";  // form inputs are always strings
let quantity = 3;

let wrongTotal = priceFromInput + quantity;   // "503"  (concatenated!)
let rightTotal = Number(priceFromInput) * quantity; // 150 (correct)

console.log(wrongTotal, rightTotal);

Frequently Asked Questions

Q: What's the difference between == and ===?

A: == converts types before comparing (loose equality); === compares both value and type with no conversion (strict equality). Use === by default.

Q: Why did "5" + 5 give "55" instead of 10?

A: Because one side is a string, JavaScript's + operator switches to string concatenation instead of arithmetic. Wrap the string in Number() first if you need math.

Q: What does the % operator do?

A: It's the remainder (modulo) operator — 7 % 2 is 1. Commonly used to check even/odd numbers. See the MDN operators reference for the full list.

Advertisement
Advertisement
Advertisement