Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

Expressions and operators

Expressions compute values, while operators specify operations on operands. Learn precedence, assignment, comparisons, type conversion, and short-circuit evaluation.

Version: ECMAScript; feature availability varies by edition. Exponentiation (`**`) was introduced in ECMAScript 2016, and nullish coalescing (`??`) was introduced in ECMAScript 2020.

Syntax

operand1 operator operand2
operator operand
condition ? valueIfTrue : valueIfFalse
(expression)

An expression, such as 2 + 3, computes a value; it can also cause a side effect. An operator acts on operands: ! is unary, and + is unary in +x and binary in a + b. The conditional operator in condition ? a : b selects one of two branches. let x = 1; is a declaration, whereas x = 2 is an assignment expression returning the assigned value.

Operators cover arithmetic (+, -, *, /, %, **), comparisons (<, ===), logic (!, &&, ||), bitwise operations (&, |, ^), and assignments (=, +=). Precedence groups 2 + 3 * 4 as 2 + (3 * 4); parentheses let you change grouping. Associativity resolves grouping at equal precedence: a = b = 1 means a = (b = 1).

= assigns, === compares without type conversion, and == may convert operands. + can concatenate strings: "5" + 1 produces "51". Do not assume every operator performs only numeric operations.

&& and || return a selected operand, not necessarily a Boolean, and skip the right operand when the result is already determined. ?? uses the right operand only for null or undefined; it preserves 0, false, and "". Mixing ?? with && or || requires parentheses. The example demonstrates precedence and the skipped increment of checks.

Examples

let checks = 0;
const total = 2 + 3 * 4;
const label = total === 14 ? "ok" : "error";
const skipped = false && (++checks > 0);
console.log(`${total}|${label}|${skipped}|${checks}|${"5" + 1}|${"5" === 5}`);

Result

14|ok|false|0|51|false

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources