JavaScript Operators
JavaScript Operators
Section titled “JavaScript Operators”JavaScript uses operators to assign values, compare values, perform arithmetic operations, and more.
const x = 5; // Assign the value 5 to xconst y = 2; // Assign the value 2 to yconst z = x + y; // Assign the sum of x and y (7) to zJavaScript Arithmetic Operators
Section titled “JavaScript Arithmetic Operators”Arithmetic operators perform mathematical calculations on numbers (literals or variables).
| Operator | Description | Example |
|---|---|---|
| + | Addition | 10 + 5 results in 15 |
| - | Subtraction | 10 - 5 results in 5 |
| * | Multiplication | 10 * 5 results in 50 |
| / | Division | 10 / 5 results in 2 |
| % | Modulus (Remainder) | 10 % 3 results in 1 |
| ** | Exponentiation (ES2016) | 10 ** 2 results in 100 (10 to the power of 2) |
| ++ | Increment | let a = 5; a++; now a is 6 |
| — | Decrement | let b = 5; b--; now b is 4 |
Increment (++) and decrement (--) operators can be postfix (x++) or prefix (++x). Postfix returns the value before incrementing/decrementing, while prefix returns the value after.
JavaScript Assignment Operators
Section titled “JavaScript Assignment Operators”Assignment operators assign values to JavaScript variables.
| Operator | Example | Same As |
|---|---|---|
| = | x = y | x = y |
| += | x += y | x = x + y |
| -= | x -= y | x = x - y |
| *= | x *= y | x = x * y |
| /= | x /= y | x = x / y |
| %= | x %= y | x = x % y |
| **= | x **= y | x = x ** y (ES2016) |
The simple assignment operator (=) assigns a value to a variable:
let score = 100;The addition assignment operator (+=) adds a value to a variable and assigns the result back to the variable:
let currentScore = 50;currentScore += 10; // currentScore is now 60JavaScript String Operators
Section titled “JavaScript String Operators”The + operator can also be used to concatenate (join) strings.
When used with strings, the + operator is called the concatenation operator. |
|---|
const firstName = "John";const lastName = "Doe";const fullName = firstName + " " + lastName; // fullName is "John Doe"The += assignment operator can also be used for string concatenation:
let greeting = "Hello ";greeting += "World!"; // greeting is "Hello World!"Modern JavaScript (ES6+) offers template literals (backticks `) for easier and more powerful string interpolation:
const name = "Alice";const age = 30;const message = `My name is ${name} and I am ${age} years old.`;// message is "My name is Alice and I am 30 years old."Adding Strings and Numbers
Section titled “Adding Strings and Numbers”JavaScript’s type coercion rules apply when using the + operator with numbers and strings:
const sumNumbers = 5 + 5; // Result is 10 (number)const concatStringNum = "5" + 5; // Result is "55" (string)const concatHelloNum = "Hello" + 5; // Result is "Hello5" (string)The general rule: If one of the operands of + is a string, JavaScript will convert the other operand to a string and perform concatenation.
Other arithmetic operators (-, *, /, %) will attempt to convert strings to numbers before performing the operation:
const subtractStrNum = "10" - 5; // Result is 5 (number, "10" converted to 10)const multiplyStr = "5" * "2"; // Result is 10 (number, both converted)JavaScript Comparison and Logical Operators
Section titled “JavaScript Comparison and Logical Operators”Comparison operators are used in logical statements to determine equality or difference between variables or values. Logical operators are used to combine conditional statements.
Comparison Operators:
Section titled “Comparison Operators:”| Operator | Description | Details & Example |
|---|---|---|
| == | Equal to | Compares values after type coercion. 5 == "5" is true. |
| === | Strictly equal to | Compares values AND types, no type coercion. 5 === "5" is false. Recommended for most equality checks. |
| != | Not equal to | Compares values after type coercion. 5 != "5" is false. |
| !== | Strictly not equal to | Compares values AND types, no type coercion. 5 !== "5" is true. Recommended for most inequality checks. |
| > | Greater than | 10 > 5 is true. |
| < | Less than | 10 < 5 is false. |
| >= | Greater than or equal to | 10 >= 10 is true. |
| <= | Less than or equal to | 5 <= 10 is true. |
Logical Operators:
Section titled “Logical Operators:”| Operator | Description | Behavior |
|---|---|---|
| && | Logical AND | expr1 && expr2 returns expr1 if it can be converted to false; otherwise, returns expr2. |
| || | Logical OR | expr1 || expr2 returns expr1 if it can be converted to true; otherwise, returns expr2. |
| ! | Logical NOT | !expr returns false if expr can be converted to true; otherwise, returns true. |
Ternary Operator (Conditional Operator):
Section titled “Ternary Operator (Conditional Operator):”condition ? valueIfTrue : valueIfFalse
Example: const access = age >= 18 ? 'granted' : 'denied';
| Comparison and logical operators are fundamental for controlling program flow and are described in more detail in the JS Comparisons and Conditions chapters. |
|---|