JavaScript 101: A Beginner’s Guide to JavaScript Operators
Source: Dev.to
Alright, now that you know a bit about how control flow works, let’s spice things up a bit. In this article, I’ll show you a bit about which operators we have in JavaScript which will really level up how you control your code’s flow.
You can see the original post here.
What are operators used for?
Let’s first start by defining what operators are. In JavaScript, operators are symbols or keywords that are used in order to perform a specific operation; these operations can be mathematical, logical, assignments, comparisons, and others. In a nutshell, they are used to manipulate data and control the flow of a program as it runs.
Which operators do we have, and what are they used for?
According to the MDN Web Docs, the latest operators we have can be divided into four groups: Arithmetic, Assignment, Comparison, Logical, and others. I’ll be explaining each of them next.
Arithmetic Operators
As you may already guess, these operators allow us to make mathematical operations. The most common usage for these are things you all know by heart:
// Addition (+)
5 + 3 // = 8
// Subtraction (-)
10 - 4 // = 6
// Multiplication (*)
6 * 7 // = 42
// Division (/)
10 / 2 // = 5
But these are not the only ones; some arithmetic operators that are a bit less common are:
// Remainder (%)
12 % 5 // = 2
// Exponentiation (**)
2 ** 3 // = 8
The remainder operator, represented by the percent sign (%), is a mathematical operator used in programming to find the remainder of a division. For example, 5 % 2 results in 1 because 2 goes into 5 two times with a remainder of 1.
Finally, we have the unary arithmetic variants which are used to either increase/decrease or to transform values to int or make them negative:
++/--(increment / decrement)- Unary
+and-to coerce or negate
let x = 5;
x++; // x becomes 6
let y = +"42"; // y is 42 (number)
let z = -10; // z is -10
Assignment Operators
Assignment operators are used to either store or update a value in a variable. The most basic version is the = operator, which assigns a value. The rest of the operators are called “compound” because they combine an arithmetic operation with assignment.
These shortcuts are great for making code cleaner and reducing repetition.
// Basic assignment:
let count = 0;
count = 5;
// Compound operators:
count += 2; // count = count + 2
count -= 3; // count = count - 3
count *= 3; // count = count * 3
// The above replace things like:
count = count * 3;
There are more assignment operators; you can see the full list in the MDN documentation here.
Comparison Operators
A quick trivia fact: JavaScript was created in May 1995. At first it only had loose comparison operators (== and !=). In December 1999, with ECMAScript 3 (ES3), the strict equality operators (=== and !==) were introduced, making our lives a little easier.
Loose Equality (== and !=)
// Not type‑safe:
"3" == 3 // true
3 != '3' // false
null == undefined // true
[] == false // true
Strict Equality (=== and !==)
0 === false // false (different types)
"5" === 5 // false (different types)
"hello" === "hello" // true (same value, same type)
1 === 1 // true (same type, same value)
3 !== '3' // true (different types)
💡 Recommendation: Always use === (and !==) unless you have a good reason to allow coercion.
Relational Operators
3 4 // false
4 >= 4 // true
4 = 18 ? "adult" : "minor";
Comparison: if…else vs. ternary
// Using if…else
function getFinalPrice(price, discount) {
let result;
if (discount > 0) {
result = price - price * discount;
} else {
result = price;
}
return result;
}
// Using a ternary operator
function getFinalPrice(price, discount) {
return discount > 0
? price - price * discount
: price;
}
💡 Tip: Ternary operators can make code shorter and more readable, but overusing them—or chaining many ternaries together—can hurt maintainability. Use them judiciously.
That’s… it, for now
These are the most common operators you’ll encounter in JavaScript, though they’re not the only ones. I may explore more advanced operators later in this series.
You should now feel prepared to start writing code with the concepts covered here.
Next up in the JavaScript 101 series: loops—specifically for, while, and do…while.
