Mapagam
  • JavaScript 
  • Web APIs 
  • TypeScript 
  • React 

JavaScript Operators Explained: Arithmetic, Logical & Comparison

Posted on April 11, 2025 • 5 min read • 1,047 words
Share via
Mapagam
Link copied to clipboard

Explore JavaScript operators: arithmetic, comparison, and logical operators with examples to enhance your coding skills and efficiency.

On this page
1. Arithmetic Operators in JavaScript 1.1. The Common Arithmetic Operators 1.2. Operator Precedence 2. Comparison Operators in JavaScript 2.1. The Common Comparison Operators 3. Logical Operators in JavaScript 3.1. The Common Logical Operators 3.2. Using Logical Operators in Conditional Statements 4. Conclusion

JavaScript is one of the most widely used programming languages in web development. As with any programming language, understanding the core concepts of JavaScript is vital for writing clean and efficient code. One of the foundational concepts in JavaScript is operators. Operators are symbols that perform operations on variables and values, and they play a significant role in almost every JavaScript program.

1. Arithmetic Operators in JavaScript

Arithmetic operators are used to perform basic mathematical operations like addition, subtraction, multiplication, and division. These operators allow developers to manipulate numeric data types and perform calculations.

1.1. The Common Arithmetic Operators

The following are the most common arithmetic operators in JavaScript:

1.1.1. Addition (+)

The addition operator is used to add two numbers together. It can also be used to concatenate strings.

let a = 10;
let b = 5;
let result = a + b; // 15

If one of the operands is a string, the + operator will concatenate them instead of adding them:

let x = 'Hello, ';
let y = 'World!';
let greeting = x + y; // "Hello, World!"

1.1.2. Subtraction (-)

The subtraction operator is used to subtract one value from another.

let a = 20;
let b = 5;
let result = a - b; // 15

1.1.3. Multiplication (*)

The multiplication operator is used to multiply two values.

let a = 4;
let b = 5;
let result = a * b; // 20

1.1.4. Division (/)

The division operator is used to divide one value by another.

let a = 10;
let b = 2;
let result = a / b; // 5

1.1.5. Modulo (%)

The modulo operator returns the remainder of a division operation. It’s often used to check if a number is divisible by another.

let a = 10;
let b = 3;
let result = a % b; // 1

1.1.6. Exponentiation (**)

Introduced in ECMAScript 6 (ES6), the exponentiation operator raises a number to the power of another number.

let a = 2;
let b = 3;
let result = a ** b; // 8 (2^3)

1.2. Operator Precedence

In JavaScript, operators have a specific precedence, meaning some operations are executed before others. For instance, multiplication and division have a higher precedence than addition and subtraction. To control the order of operations, you can use parentheses.

let result = 5 + 3 * 2; // 11, multiplication occurs before addition
let resultWithParentheses = (5 + 3) * 2; // 16

2. Comparison Operators in JavaScript

Comparison operators are used to compare two values, and they return a Boolean value (true or false) based on the comparison. These operators are essential for conditional statements, such as if and switch, to determine the flow of execution.

2.1. The Common Comparison Operators

2.1.1. Equal to (==)

The equality operator checks whether two values are equal. It performs type coercion, meaning it converts the operands to the same type before comparing them.

let a = 10;
let b = '10';
let result = a == b; // true (type coercion occurs)

2.1.2. Strict Equal to (===)

The strict equality operator checks whether two values are equal and of the same type. It does not perform type coercion.

let a = 10;
let b = '10';
let result = a === b; // false (different types)

2.1.3. Not Equal to (!=)

The inequality operator checks whether two values are not equal. Like ==, it performs type coercion.

let a = 10;
let b = '10';
let result = a != b; // false (type coercion occurs)

2.1.4. Strict Not Equal to (!==)

The strict inequality operator checks whether two values are not equal or not of the same type. It doesn’t perform type coercion.

let a = 10;
let b = '10';
let result = a !== b; // true (different types)

2.1.5. Greater Than (>)

The greater-than operator checks if the value on the left is greater than the value on the right.

let a = 15;
let b = 10;
let result = a > b; // true

2.1.6. Less Than (<)

The less-than operator checks if the value on the left is less than the value on the right.

let a = 5;
let b = 10;
let result = a < b; // true

2.1.7. Greater Than or Equal to (>=)

The greater-than or equal-to operator checks if the value on the left is greater than or equal to the value on the right.

let a = 10;
let b = 10;
let result = a >= b; // true

2.1.8. Less Than or Equal to (<=)

The less-than or equal-to operator checks if the value on the left is less than or equal to the value on the right.

let a = 5;
let b = 10;
let result = a <= b; // true

3. Logical Operators in JavaScript

Logical operators are used to perform logical operations on Boolean values. They are often used in conditional statements to control program flow based on multiple conditions.

3.1. The Common Logical Operators

3.1.1. Logical AND (&&)

The logical AND operator returns true if both operands are true, and false otherwise.

let a = true;
let b = false;
let result = a && b; // false

3.1.2. Logical OR (||)

The logical OR operator returns true if at least one of the operands is true, and false only if both operands are false.

let a = true;
let b = false;
let result = a || b; // true

3.1.3. Logical NOT (!)

The logical NOT operator inverts the Boolean value of an operand. If the operand is true, it returns false; if the operand is false, it returns true.

let a = true;
let result = !a; // false

3.2. Using Logical Operators in Conditional Statements

Logical operators are commonly used in if statements to combine multiple conditions.

let age = 25;
let isStudent = false;

if (age > 18 && !isStudent) {
  console.log("You are an adult, but not a student.");
}

In the above example, both conditions must be true for the message to be printed.

4. Conclusion

Understanding JavaScript operators is a fundamental aspect of learning the language. Operators are essential for performing mathematical calculations, comparing values, and making logical decisions within your code.

JavaScript Operators   Arithmetic Operators   Comparison Operators   Logical Operators   JavaScript Programming  
JavaScript Operators   Arithmetic Operators   Comparison Operators   Logical Operators   JavaScript Programming  
 What Are JavaScript Ternary Operators? Simple Examples for Beginners
Truthy vs Falsy Values in JavaScript: Understanding Conditional Logic 

More Reading!

  1. Short-Circuiting in JavaScript: Master Logical Operators Like a Pro
On this page:
1. Arithmetic Operators in JavaScript 1.1. The Common Arithmetic Operators 1.2. Operator Precedence 2. Comparison Operators in JavaScript 2.1. The Common Comparison Operators 3. Logical Operators in JavaScript 3.1. The Common Logical Operators 3.2. Using Logical Operators in Conditional Statements 4. Conclusion
Follow me

I work on everything coding and technology

   
Mapagam
Mapagam is your go-to resource for all things related to frontend development. From the latest frameworks and libraries to tips, tutorials, and best practices, we dive deep into the ever-evolving world of web technologies.
Licensed under Creative Commons (CC BY-NC-SA 4.0).
 
Frontend
JavaScript 
Web Api 
TypeScript 
React 
Social
Linkedin 
Github 
Mapagam
Code copied to clipboard