JavaScript Operators
Operators perform actions on values, called operands. JavaScript groups operators into categories: arithmetic, assignment, comparison, logical, and more.
Understanding operator precedence โ the order operations are evaluated โ helps avoid unexpected results in complex expressions.
Categories
Arithmetic (`+ - * / % **`), assignment (`= += -=`), comparison (`== === < >`), logical (`&& || !`), and the ternary operator (`? :`) cover most needs.
Operator precedence
Multiplication and division run before addition and subtraction, similar to math class. Parentheses `()` override the default order.
let result = 2 + 3 * 4;
console.log(result);14Multiplication happens before addition.
let age = 20;
let canVote = age >= 18 ? "Yes" : "No";
console.log(canVote);YesThe ternary operator is a compact if/else.
Key points
- Operators act on operands to produce a value.
- Precedence determines evaluation order; use () to be explicit.
- The ternary operator `cond ? a : b` is a compact conditional.
- Logical operators `&&`, `||`, `!` combine boolean expressions.
