JavaScript: Operators
JavaScript Operators and Type Conversions
1 Overview
📘 Operators
Operators are symbols that perform operations on values and variables in JavaScript. They include arithmetic operators (+, -, *, /), assignment operators (=, +=, -=), comparison operators (==, ===, !=), logical operators (&&, ||, !), and more. JavaScript also performs automatic type conversions when operators are applied to different data types, which is important to understand for writing correct code.
2 Terms: Unary, Binary, Operand
Before we dive into operators, let’s understand some terminology:
- Operand: What operators are applied to. In
5 * 2
, there are two operands:5
(left) and2
(right) - Unary operator: Has a single operand. Example:
-x
(negation) - Binary operator: Has two operands. Example:
x - y
(subtraction)
2.1 Unary Operator Example
2.2 Binary Operator Example
3 Arithmetic Operators
The following math operations are supported:
- Addition:
+
- Subtraction:
-
- Multiplication:
*
- Division:
/
- Remainder:
%
- Exponentiation:
**
3.1 Basic Operations
3.2 Remainder %
The remainder operator %
returns the remainder of integer division:
3.3 Exponentiation **
The exponentiation operator **
raises a number to a power:
It also works with non-integer powers:
4 String Concatenation with Binary +
Usually, the plus operator +
sums numbers. But if the binary +
is applied to strings, it merges (concatenates) them:
4.1 String + Number
If any of the operands is a string, the other one is converted to a string too:
4.2 Order Matters
Here, operators work left to right. First 2 + 2 = 4
, then 4 + '1' = "41"
.
Here, the first operand is a string, so all operands are treated as strings: '1' + 2 = "12"
, then "12" + 2 = "122"
.
5 Numeric Conversion: Unary +
The unary plus +
applied to a single value doesn’t do anything to numbers. But if the operand is not a number, it converts it into a number:
It does the same thing as Number(...)
, but is shorter.
5.1 Practical Example
6 Assignment Operator
The assignment operator =
assigns a value to a variable:
6.1 Assignment Returns a Value
The assignment =
returns a value. The call x = value
writes the value
into x
and then returns it:
6.2 Chaining Assignments
Chained assignments evaluate from right to left: c = 4
, then b = 4
, then a = 4
.
7 Modify-in-Place
We often need to apply an operator to a variable and store the result in that same variable:
This can be shortened using +=
and *=
:
7.1 All Modify-and-Assign Operators
Short “modify-and-assign” operators exist for all arithmetic operators:
8 Increment/Decrement
Increasing or decreasing a number by one is very common, so there are special operators:
- Increment
++
: increases a variable by 1 - Decrement
--
: decreases a variable by 1
Increment/decrement can only be applied to variables. Using it on a value like 5++
will give an error.
8.1 Prefix vs Postfix
The operators ++
and --
can be placed either before or after a variable:
- Postfix form:
counter++
(operator after variable) - Prefix form:
++counter
(operator before variable)
Both increase counter
by 1, but there’s a difference in the returned value.
8.2 Prefix Form
The prefix form returns the new value:
8.3 Postfix Form
The postfix form returns the old value (prior to increment):
8.4 When to Use Which
- If you don’t use the returned value, both forms are the same:
- Use prefix if you want to increase and immediately use the new value:
- Use postfix if you want to use the old value:
9 Operator Precedence
If an expression has more than one operator, the execution order is defined by their precedence.
From highest to lowest:
Precedence | Operator | Description |
---|---|---|
14 | ++ , -- |
Increment/Decrement (prefix) |
13 | ** |
Exponentiation |
12 | * , / , % |
Multiplication, Division, Remainder |
11 | + , - |
Addition, Subtraction |
2 | = |
Assignment |
9.1 Example
10 Type Conversions
JavaScript automatically converts types when needed. This is called implicit type conversion or type coercion.
10.1 String Conversion
Occurs when we output something. Can be performed explicitly with String(value)
:
10.2 Numeric Conversion
Happens automatically in mathematical operations. Can be performed explicitly with Number(value)
:
Explicit conversion:
10.3 Numeric Conversion Rules
Value | Becomes |
---|---|
undefined |
NaN |
null |
0 |
true |
1 |
false |
0 |
"" (empty string) |
0 |
" " (whitespace) |
0 |
"123" |
123 |
"123abc" |
NaN |
10.4 Boolean Conversion
Happens in logical operations. Can be performed explicitly with Boolean(value)
:
Rules: - Values that are intuitively “empty” (0
, ""
, null
, undefined
, NaN
) become false
- Other values become true
11 Comparison Operators
Comparison operators return a boolean value:
- Greater/less than:
a > b
,a < b
- Greater/less than or equals:
a >= b
,a <= b
- Equality:
a == b
(with type coercion) - Strict equality:
a === b
(without type coercion) - Not equal:
a != b
- Strict not equal:
a !== b
11.1 String Comparison
Strings are compared letter-by-letter in “dictionary” order:
11.2 Different Types Comparison
When comparing values of different types, JavaScript converts them to numbers:
11.3 Strict Equality ===
The regular equality check ==
has a problem – it cannot differentiate 0
from false
:
The strict equality operator ===
checks equality without type conversion:
12 Examples
12.1 Example 1: Calculator
let a = 10;
let b = 5;
console.log(`${a} + ${b} = ${a + b}`); // 10 + 5 = 15
console.log(`${a} - ${b} = ${a - b}`); // 10 - 5 = 5
console.log(`${a} * ${b} = ${a * b}`); // 10 * 5 = 50
console.log(`${a} / ${b} = ${a / b}`); // 10 / 5 = 2
console.log(`${a} % ${b} = ${a % b}`); // 10 % 5 = 0
console.log(`${a} ** ${b} = ${a ** b}`); // 10 ** 5 = 100000
12.2 Example 2: Type Coercion
console.log("5" + 3); // "53" (string concatenation)
console.log("5" - 3); // 2 (numeric subtraction)
console.log("5" * "2"); // 10 (numeric multiplication)
console.log(true + 1); // 2 (true becomes 1)
console.log(false + 1); // 1 (false becomes 0)
console.log(null + 1); // 1 (null becomes 0)
console.log(undefined + 1); // NaN (undefined becomes NaN)
12.3 Example 3: Increment/Decrement
13 Summary
- JavaScript supports standard arithmetic operators:
+
,-
,*
,/
,%
,**
- Binary
+
concatenates strings if any operand is a string - Unary
+
converts values to numbers - Modify-in-place operators:
+=
,-=
,*=
,/=
, etc. - Increment
++
and decrement--
have prefix and postfix forms - Prefix returns new value, postfix returns old value
- Type conversions happen automatically or can be done explicitly
- Use
===
for strict equality (no type conversion) - Use
==
for equality with type conversion