Working with information: Operators in JavaScript

Srijan } Author Pic
SrijanJun 06, 2025 - JavaScript
Working with information: Operators in JavaScript - Reacted Node

Photo: Operators in JavaScript - ReactedNode

What you will learn

  • How to store and update information using assignment operators.
  • How to perform mathematical calculations using Arithmetic Operators.
  • How to combine strings (text) using concatenation.
  • How to compare values using comparison operators.
  • The crucial difference between checking equality loosely == and strictly ===.
  • How to make decisions using logical operators like &&, ||, and !.
  • How Logical Operators can sometimes skip work (short-circuiting).

Key concepts

Programming is about telling the computers to manipulate information -data.

Operators are the special symbols that tell the computer what manipulation to perform on the given data. You can think of data as ingredients and operators as the tools like knives, whisks, and spoons used for cooking.

Assignment Operators

Assignment operators assign or update values in a variable.

1let count = 10; // "=" assigns the value 10 to count
2count = count + 1; // increase count by 1

Compound Assignment Operators

Typing count = count + 1 can be a bit repetitive. In these cases, compound operators can act as shorthand, which combines a calculation and an assignment in one step.

Common shorthand operators are:

  • x += y is shorthand for x = x + y
  • x -= y is shorthand for x = x - y
  • x *= y is shorthand for x = x * y
  • x /= y is shorthand for x = x / y
1let bananas = 5;
2
3// Add seven more bananas
4bananas += 7; // Same as: bananas = bananas + 7
5console.log(bananas); // Output: 12

Arithmetic operators

Arithmetic operators are used to perform mathematical calculations.

These operators correspond directly to basic mathematical operations:

  • +: Addition
  • -: Subtraction
  • *: Multiplication
  • /: Division
  • % (Modulo): Gives the remainder of a division. It is handy for tasks like checking if a number is even or odd.
  • ** (Exponentiation): Raises the first number to the power of the second number (e.g., 2 ** 3 is 8).
1let sum = 22 + 55; // 77
2let product = 4 * 2; // 8
3let power = 2 ** 3; // 8
4let quotient = 2 / 2; // 1
5let remainder = 10 % 3; // 1

String concatenation

The process of joining pieces of text together is called string concatenation.

Apart from addition, we can reuse the + operator for string concatenation.

1let firstName = "Srijan";
2let greeting = "Hello, " + firstName + "!";
3
4console.log(greeting); // Hello, Srijan!

Comparison operators

Programs often need to make decisions based on whether certain conditions are true or false. To do this, we must be able to compare values.

Comparison operators let us compare two values and return either true or false. These operators allow us to ask questions about the relationship between values:

  • >: Greater than
  • <: Less than
  • >=: Greater than or equal to
  • <=: Less than or equal to
  • ==: Equal to (loose comparison - use with caution!)
  • !=: Not equal to (loose comparison)
  • ===: Strictly equal to (value and type must match - preferred!)
  • !==: Strictly not equal to (value or type must be different - preferred!)
1let age = 25;
2let requiredAge = 18;
3
4console.log(age > requiredAge); // true
5console.log(age < requiredAge); // false

Strict equality === vs Loose equality ==

Loose equality = tries to be helpful when the type of two data compared are different. How does it try to be helpful? By secretly converting one or both values to a common type before making the comparison.

For example, for 7 == "7", JavaScript converts the string "7" to the number 7, then compares 7 to 7, resulting in true.

Strict Equality ===, on the other hand, checks both the value and the type. It does not perform any coercion. So, 7 === "7", results in false.

Always go with strict equality === because it is predictable. There are no hidden type coercions, which leads to fewer bugs.

Logical operators

A decision can depend on more than one condition. For example, "Access granted if the user is logged in AND has admin rights" or "Display weekend message if it is Saturday or Sunday".

These logical operators can combine multiple boolean values:

  • && (Logical AND): Results in true only if both the conditions on its left and right are true. The result is false if either one (or both) is false.
  • || (Logical OR): Results in true if at least one of the conditions on its left or right is true. It's only false if both conditions are false.
  • ! (Logical NOT): This takes a single boolean value and inverts it. !true becomes false, and !false becomes true.
1let isLoggedIn = true;
2let isAdmin = false;
3let isWeekend = false;
4
5// AND (&&): Need both to be true
6console.log("Can access sensitive data?", isLoggedIn && isAdmin);
7// true && false -> false
8
9// OR (||): Need at least one to be true
10console.log("Show special offer?", isLoggedIn || isWeekend);
11// true || false -> true
12
13// NOT (!): Inverts the value
14console.log("Is NOT logged in?", !isLoggedIn); // !true -> false
15console.log("Is NOT admin?", !isAdmin); // !false -> true

Short-circuiting

The computer stops early if the answer is already known.

  • For AND &&: If the first condition evaluates to false, the entire expression evaluates to false, so the computer does not consider even evaluating the remaining conditions and short circuits.
  • For OR ||: If the first condition evaluates to true, the entire expression evaluates to true, so the computer does not consider even evaluating the remaining conditions and short circuits.
1false && anything; // returns false immediately
2true || anything; // returns true immediately

Why it matters

Operators are the verbs of programming. Without them, you can't perform calculations, compare, combine text, store information, or create logic.

Understanding operators is like learning how to use tools in the toolbox. Mastering them is the first step toward writing meaningful programs.