[JS Basics] Operators in JavaScript: Overview and Easy-to-Follow Practical Examples

Welcome to the world of JavaScript programming! If we compare JavaScript to a language, then variables and values are the nouns, while operators are the verbs—the glue that brings logic and power to your code. They are the most fundamental tools that let you perform calculations, compare values, and make decisions.

Operators in JavaScript

In this article, we'll dive into each basic type of operator, from familiar arithmetic to complex logic comparisons. Whether you're a beginner or want to reinforce your knowledge, this is an essential toolkit for conquering JavaScript.

1. What is an Operator? A Quick Overview 😊

Simply put, an operator is a special symbol (+, -, *, /, =, >) used to perform an operation on one, two, or three values. These values are called operands.

Check out this classic example:

let a = 5 + 10
  • + is the operator (addition).
  • 5 and 10 are the operands.
  • = is also an operator (assignment).

JavaScript provides many different types of operators, and we'll categorize them for easier understanding.

2. Arithmetic Operators 🧮

These are the most familiar operators, used for basic math. Think of them as your pocket calculator!

OperatorNameExampleResultDescription
+Addition10 + 515Adds two operands.
-Subtraction10 - 55Subtracts the right operand from the left.
*Multiplication10 * 550Multiplies two operands.
/Division10 / 52Divides the left operand by the right.
%Modulus (Remainder)10 % 31Returns the remainder of division.
**Exponentiation2 ** 38Raises the left operand to the power of the right (equivalent to 222).
++Incrementlet a = 5; a++;a is now 6Increases the value of a variable by 1.
--Decrementlet a = 5; a--;a is now 4Decreases the value of a variable by 1.

💡 Special note about +: When used with strings, the + operator performs concatenation.

let greeting = 'Hello ' + 'world!'
console.log(greeting) // Result: "Hello world!"

let number = '5' + 3
console.log(number) // Result: "53" (because "5" is a string, JavaScript converts 3 to a string and concatenates)

3. Assignment Operators ✍️

Assignment operators assign a value to a variable. The most basic is the equals sign (=). However, JavaScript also provides compound assignment operators for shorter, cleaner code.

OperatorEquivalent toExample
x = yx = ylet x = 10;
x += yx = x + ylet x = 10; x += 5; // x is now 15
x -= yx = x - ylet x = 10; x -= 5; // x is now 5
x *= yx = x * ylet x = 10; x *= 5; // x is now 50
x /= yx = x / ylet x = 10; x /= 5; // x is now 2
x %= yx = x % ylet x = 10; x %= 3; // x is now 1
x **= yx = x ** ylet x = 2; x **= 3; // x is now 8

These operators not only make your code shorter but also easier to read once you're familiar with them.

4. Comparison Operators 🤔

Comparison operators are used in logic statements to determine equality or difference between variables or values. The result of a comparison is always a boolean (true or false). This is the foundation for branching and decision-making in code (e.g., in if...else statements).

OperatorNameExampleResult
==Equal (loose comparison)5 == "5"true
===Strictly equal (strict comparison)5 === "5"false
!=Not equal (loose comparison)5 != "5"false
!==Strictly not equal (strict comparison)5 !== "5"true
>Greater than10 > 5true
<Less than10 < 5false
>=Greater than or equal to10 >= 10true
<=Less than or equal to10 <= 5false

🔥 The Classic Battle: == vs. ===

This is one of the most important differences every JavaScript developer must understand.

  • == (Loose Equality): Compares values after performing type coercion. In 5 == "5", JavaScript automatically converts the string "5" to the number 5 before comparing, so the result is true.
  • === (Strict Equality): Compares both value and type. No type conversion is performed. Since 5 (number) and "5" (string) are different types, 5 === "5" returns false.

👉 Golden rule: Always prefer === and !== to avoid hidden bugs caused by automatic type coercion. This makes your code clearer, more predictable, and less error-prone.

5. Logical Operators 🧠

Logical operators are used to combine multiple comparison expressions, allowing you to build complex conditions.

| Operator | Name | Description | | :------- | :------ | :--------------------------------------------------------------------------------- | ------ | ----------------------------------------------------- | | && | AND | Returns true if both operands are true. | | | | | OR | Returns true if at least one operand is true. | | ! | NOT | Inverts the boolean value of the operand (true becomes false, and vice versa). |

Real-world example:

let age = 25
let hasDriverLicense = true

// Check if the person is old enough AND has a driver's license
if (age >= 18 && hasDriverLicense) {
  console.log('You are allowed to drive.') // This will print
}

let isWeekend = false
let isHoliday = true

// Check if it's a day off (weekend OR holiday)
if (isWeekend || isHoliday) {
  console.log('Today is a day off!') // This will print
}

let isRaining = false
if (!isRaining) {
  // It's NOT raining
  console.log('Let’s go outside for a walk!') // This will print
}

6. Operator Precedence

When an expression contains multiple operators, JavaScript executes them in a certain order of precedence. For example, multiplication (*) is always performed before addition (+).

let result = 5 + 10 * 2 // 10 * 2 is performed first
console.log(result) // Result is 25, not 30

let result2 = (5 + 10) * 2 // (5 + 10) is performed first
console.log(result2) // Result is 30

Conclusion: Operators are the "Core" of JavaScript Logic

Operators are the backbone of logic in JavaScript programming. Understanding and mastering them—especially the difference between == and ===—will help you write code more efficiently, accurately, and professionally.

Don't just read—open your code editor, experiment with each operator, and create your own expressions. Practice is the shortest path to mastering these powerful tools. Good luck on your coding journey! 💻✨

Related Posts

[JS Basics] What is JavaScript? Why is it Important for Developers?

What is JavaScript? This article explains in detail the concept, role, and applications of JavaScript—the indispensable programming language in modern web development.

[JS Basics] How to Set Up a JavaScript Runtime Environment

Are you new to programming and want to start with JavaScript? This article will guide you step-by-step on how to set up a JavaScript runtime environment on your computer easily and quickly.

[JS Basics] Variables and Data Types: Essential Concepts to Master

Mastering variables and data types is the key to becoming a great JavaScript developer. This article provides you with a solid foundation and practical examples.

Master JavaScript Basic Rules in 5 Minutes - Quick Guide

Start your JavaScript journey with the most fundamental rules. Learn how to write proper code, avoid common mistakes, and build a solid foundation for your programming career.