Working with information: Operators in JavaScript
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.
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 forx = x + y
x -= y
is shorthand forx = x - y
x *= y
is shorthand forx = x * y
x /= y
is shorthand forx = x / y
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).
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.
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!)
===
vs Loose equality ==
Strict 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 intrue
only if both the conditions on its left and right aretrue
. The result isfalse
if either one (or both) isfalse
.||
(Logical OR): Results intrue
if at least one of the conditions on its left or right istrue
. It's onlyfalse
if both conditions arefalse
.!
(Logical NOT): This takes a single boolean value and inverts it.!true
becomesfalse
, and!false
becomestrue
.
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.
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.