Web Development - JavaScript Operators
JavaScript Operators
Operators are symbols that perform actions on values. You use them to calculate, compare, or assign data in your JavaScript programs.
In the example below, we use the + operator to add together two values:
The + operator can also be used to add a variable and a value, or even two variables:
Example
let x = 10;
let y = 5;
let sum1 = x + 2; // adds a variable and a value
let sum2 = x + y; // adds two variables
Try it Yourself »
Types of Operators
JavaScript has several categories of operators:
- Arithmetic Operators
- Assignment Operators
- Comparison Operators
- Logical Operators
- String Operators
- Type Operators
Arithmetic Operators
Arithmetic operators perform math between numbers.
| Operator | Description | Example | Try it |
|---|---|---|---|
| + | Addition | 5 + 2 | Try it » |
| - | Subtraction | 5 - 2 | Try it » |
| * | Multiplication | 5 * 2 | Try it » |
| / | Division | 10 / 2 | Try it » |
| % | Modulus (Remainder) | 5 % 2 | Try it » |
| ** | Exponentiation | 3 ** 2 | Try it » |
| ++ | Increment | x++ | Try it » |
| -- | Decrement | x-- | Try it » |
Assignment Operators
Assignment operators assign values to variables, often combining assignment with another operation.
| Operator | Example | Same As | Try it |
|---|---|---|---|
| = | x = 5 | x = 5 | Try it » |
| += | x += 2 | x = x + 2 | Try it » |
| -= | x -= 2 | x = x - 2 | Try it » |
| *= | x *= 2 | x = x * 2 | Try it » |
| /= | x /= 2 | x = x / 2 | Try it » |
| %= | x %= 2 | x = x % 2 | Try it » |
Comparison Operators
Comparison operators compare values and return a Boolean (true or false).
| Operator | Description | Example | Try it |
|---|---|---|---|
| == | Equal to (loose) | 5 == "5" | Try it » |
| === | Strict equal (same type & value) | 5 === "5" | Try it » |
| != | Not equal | 5 != 8 | Try it » |
| !== | Strict not equal | 5 !== "5" | Try it » |
| > | Greater than | 8 > 5 | Try it » |
| < | Less than | 3 < 5 | Try it » |
| >= | Greater or equal | 5 >= 5 | Try it » |
| <= | Less or equal | 2 <= 3 | Try it » |
In the example below, JavaScript compares the value of age with 18.
It checks if age is greater than or equal to 18:
Example
Comparing values:
let age = 18;
document.getElementById("demo").innerHTML = (age >= 18); // true
Try it Yourself »
You can also compare two variables.
In the example below, JavaScript checks whether the value of a is greater than the value of b:
Example
Comparing two variables:
let a = 10;
let b = 15;
document.getElementById("demo").innerHTML = (a > b); // false
Try it Yourself »
Logical Operators
Logical operators are used to combine or modify Boolean values (true and false).
| Operator | Description | Example | Try it |
|---|---|---|---|
| && | AND - true only if both are true | (x > 5 && x < 10) | Try it » |
| || | OR - true if either is true | (x < 5 || y > 10) | Try it » |
| ! | NOT - reverses the result | !(x == y) | Try it » |
Note: Logical operators return true or false depending on the result of the combined conditions.
Operator Precedence
Operator precedence decides which operations run first. Multiplication and division have higher precedence than addition and subtraction.
Summary
- Operators perform calculations, comparisons, and assignments.
- Use arithmetic, comparison, logical, and string operators to manipulate data.
- Operator precedence controls the order of evaluation.
Next, you will learn about JavaScript Booleans - how Boolean values control your program's flow..