HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to JavaScriptVariables (let / const)Data TypesOperatorsStringsArraysObjectsConditions (if / else)Loops (for / while)FunctionsArrow Functions (=>)Array Methods (map / filter / reduce)
REFERENCE
📖 Overview
🖥️ Output & Console
📦 Variables
🔢 Numbers & Math
✍️ String
📋 Array
🗂️ Object
🔁 Condition & Loop
if / else? : (ternary operator)switchforfor...ofwhile
🧩 Function
🔄 Type & Conversion
JavaScript›REFERENCE›Condition & Loop
🔁

Condition & Loop

Decision-making and repetition structures.

CommandDescription
if / elseRuns different code based on a condition.
? : (ternary operator)Single-line if/else.
switchCompares a value against multiple options.
forLoops a specific number of times.
for...ofIterates over each element of an array.
whileLoops as long as the condition is true.

if / else

Try it yourself →

Runs different code based on a condition.

Syntax

if (condition) { ... } else { ... }

Description

The condition is written inside parentheses, and blocks are written inside curly braces { }. Intermediate conditions are added with else if.

Example

const score = 75;
if (score >= 50) {
  console.log("Passed");
} else {
  console.log("Failed");
}

Expected output

Passed

When to use?

Access control, flow branching.

Common mistakes

  • ⚠️ Use === for equality (== can do type coercion and cause unexpected behavior).

? : (ternary operator)

Try it yourself →

Single-line if/else.

Syntax

condition ? trueValue : falseValue

Description

Returns the first value if the condition is true, and the second if false. Ideal for short assignments/displays.

Returns

One of two values.

Example

const age = 20;
const status = age >= 18 ? "adult" : "child";
console.log(status);

Expected output

adult

When to use?

Short conditional value assignment, in JSX/templates.

switch

Try it yourself →

Compares a value against multiple options.

Syntax

switch (value) { case x: ...; break; default: ... }

Description

A clean way to write numerous equality checks. Don't forget to put break after each case.

Example

const day = 2;
switch (day) {
  case 1: console.log("Mon"); break;
  case 2: console.log("Tue"); break;
  default: console.log("?");
}

Expected output

Tue

When to use?

Menu selection, state machines, multi-branch control.

Common mistakes

  • ⚠️ If break is forgotten, subsequent cases will also execute (fall-through).

for

Try it yourself →

Loops a specific number of times.

Syntax

for (let i = 0; i < n; i++) { ... }

Description

Three parts: initialization, condition, increment. Repeats a specific number of times using the i counter.

Example

for (let i = 1; i <= 3; i++) {
  console.log(i);
}

Expected output

1
2
3

When to use?

Indexed loop, specific repetition.

for...of

Try it yourself →

Iterates over each element of an array.

Syntax

for (const e of arr) { ... }

Description

A modern way to iterate directly over elements without dealing with indices.

Example

for (const color of ["red", "purple"]) {
  console.log(color);
}

Expected output

red
purple

When to use?

Processing array/text elements sequentially.

while

Try it yourself →

Loops as long as the condition is true.

Syntax

while (condition) { ... }

Description

Used when the number of repetitions is unknown. Don't forget to break the condition at some point, or you'll get an infinite loop.

Example

let s = 0;
while (s < 3) {
  console.log(s);
  s++;
}

Expected output

0
1
2

When to use?

Indefinite repetition, waiting 'until correct input is entered'.

Common mistakes

  • ⚠️ If you forget to increment like s++, it results in an infinite loop.
HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code