JS Operators: Arithmetic, Logical, Comparison
JavaScript operators let you do math, compare values, and combine conditions. Learn the three core operator groups every beginner needs to know.
Operators are the symbols that tell JavaScript to perform actions on values. Every time you add two numbers, check if a value is greater than another, or combine two conditions, you are using an operator.
There are many operators in JavaScript, but three groups cover most of what you will write as a beginner: arithmetic operators for math, comparison operators for checking values, and logical operators for combining conditions.
The Three Core Operator Groups
| Group | Purpose | Example Operators |
|---|---|---|
| Arithmetic | Perform math operations | + - * / % ** |
| Comparison | Compare two values, return true or false | === !== > < >= <= |
| Logical | Combine or invert boolean conditions | `&& |
Arithmetic Operators
Arithmetic operators do math, just like a calculator.
const sum = 10 + 5; // 15
const difference = 10 - 5; // 5
const product = 10 * 5; // 50
const quotient = 10 / 5; // 2
const remainder = 10 % 3; // 1 (remainder of 10 / 3)
const power = 2 ** 3; // 8 (2 raised to the power of 3)Each line above runs one operator on two numbers and stores the result. The comment next to each line shows exactly what the constant holds after that operation runs.
Modulo returns the remainder after division instead of the quotient. This makes it the standard way to check whether a number is even or odd, since even numbers always leave a remainder of 0 when divided by 2:
console.log(7 % 2); // 1, so 7 is odd
console.log(8 % 2); // 0, so 8 is evenA remainder of 1 means the number is odd, and a remainder of 0 means it divides evenly. This check shows up often in loops that need to alternate behavior every other item.
Exponentiation raises the left number to the power of the right number, so it covers squares, cubes, and any other repeated multiplication in one step:
console.log(3 ** 2); // 9 (3 squared)
console.log(2 ** 4); // 16 (2 to the power of 4)The first line multiplies 3 by itself twice, and the second multiplies 2 by itself four times. Reach for this operator instead of writing out repeated multiplication by hand.
The + operator does two things
The + operator adds numbers, but it also joins strings together, and mixing the two types changes which behavior you get:
console.log(5 + 5); // 10 (number addition)
console.log("Hello " + "JS"); // "Hello JS" (string concatenation)
console.log("Score: " + 100); // "Score: 100" (number converted to string)When both sides are numbers, plus adds them. When either side is a string, JavaScript converts the other value to a string and joins the two. This is a common source of beginner bugs, so confirm both values are numbers first if you want addition instead of joining. The other arithmetic operators do not have this dual behavior and always convert strings to numbers.
Comparison Operators
Comparison operators compare two values and always return true or false. They are the foundation of decision-making in code.
console.log(5 === 5); // true (strict equality)
console.log(5 !== 3); // true (strict inequality)
console.log(10 > 5); // true (greater than)
console.log(3 < 7); // true (less than)
console.log(8 >= 8); // true (greater than or equal)
console.log(4 <= 2); // false (less than or equal)Each line compares the two numbers and produces a plain true or false answer, which is why comparisons are the building blocks of every conditional check you write.
Strict comparison (=== / !==) checks both value and type without converting anything, so it behaves predictably. Loose comparison (== / !=) converts types before comparing, which can produce surprising results:
console.log(0 == false); // true (loose: converts false to 0)
console.log(0 === false); // false (strict: number vs boolean, no match)The loose check treats 0 and false as equal because it converts one type to the other before comparing. The strict check refuses to convert anything, so a number and a boolean can never match. Prefer the strict form in your own code. For more on this, see the comparison operators guide.
Logical Operators
Logical operators combine or flip boolean values. They are what let you check multiple conditions at once.
const isLoggedIn = true;
const isAdmin = false;
console.log(isLoggedIn && isAdmin); // false (AND: both must be true)
console.log(isLoggedIn || isAdmin); // true (OR: at least one true)
console.log(!isLoggedIn); // false (NOT: flips the value)- AND returns true only when both sides are true.
- OR returns true when at least one side is true.
- NOT flips a true value to false and a false value to true.
A common real-world pattern is checking that a user is both logged in and has the right role:
const user = { loggedIn: true, role: "admin" };
if (user.loggedIn && user.role === "admin") {
console.log("Access granted to admin panel");
}This condition only runs the log statement when both user.loggedIn and the role check are true, since AND requires every side to pass.
Operator Precedence at a Glance
When an expression has multiple operators, JavaScript does not just read left to right. It follows a precedence order, similar to how multiplication happens before addition in math class:
console.log(5 + 3 * 2); // 11, not 16 (multiplication first)
console.log((5 + 3) * 2); // 16 (parentheses override precedence)Multiplication runs before addition in the first line, so the product is calculated before it is added to 5. Parentheses in the second line force the addition to run first instead.
Arithmetic runs before comparison, and comparison runs before the logical operators, so mixed expressions evaluate math first, then checks, then logic. When an expression combines several operator types, add parentheses to make the intended order explicit instead of relying on memorized rules. For the full precedence order, see the operator precedence guide.
Putting Them Together
Real code combines all three groups. Here is an example that calculates a discount, checks a condition, and uses logic to decide the final price:
const price = 100;
const quantity = 3;
const isMember = true;
const subtotal = price * quantity; // arithmetic
const qualifiesForDiscount = subtotal > 200; // comparison
const finalPrice = qualifiesForDiscount && isMember
? subtotal * 0.8 // logical + arithmetic
: subtotal;
console.log(finalPrice); // 240 (300 * 0.8 because both conditions are true)This small snippet uses arithmetic to calculate the subtotal, comparison to check if it passes a threshold, and logical AND to confirm both conditions before applying a discount.
Rune AI
Key Insights
- Arithmetic operators (+, -, *, /, %, **) perform math operations.
- Comparison operators (===, !==, >, <, >=, <=) return true or false.
- Logical operators (&&, ||, !) combine or invert boolean values.
- Operator precedence determines which operation runs first.
- Use === (strict equality) instead of == to avoid unexpected type coercion.
Frequently Asked Questions
What is the difference between = and === in JavaScript?
What does the % operator do?
Can I mix arithmetic and comparison operators in one expression?
Conclusion
JavaScript operators are the tools that let you perform calculations, compare values, and build logic. Arithmetic operators handle math, comparison operators produce true or false results, and logical operators combine multiple conditions. Understanding these three groups gives you everything you need to start writing real decision-making code.
More in this topic
JavaScript While Loop Explained: A Complete Guide
A while loop repeats code as long as a condition stays true. Learn the syntax, see practical examples, and understand when a while loop is the right choice over a for loop.
JavaScript Loops Tutorial: for, while, do while
Loops let JavaScript repeat code without writing it twice. Learn the three core loop types, what each one does, and when to choose one over another.
How to Loop Through Arrays Using JS for Loops Guide
Looping through arrays is the most practical use of JavaScript for loops. Learn how to access each element by index, avoid off-by-one errors, and choose between for, for...of, and forEach.