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
🧩 Function
🔄 Type & Conversion
JavaScript›Conditions (if / else)

Conditions (if / else)

Decision making with if-else, comparisons, logical operators, and the ternary operator (? :).

Simple enough for a 5-year-old to understand

if / else

The condition is written inside parentheses, and the code to execute is inside curly braces { }.

Example code
1
const score = 75;
Value.
VariableValue.
2
if (score >= 50) {
Is it 50 or above?
ConditionIs it 50 or above?
3
console.log("Passed");
If yes, print this.
Output / InputIf yes, print this.
4
} else {
If not...
ConditionIf not...
5
console.log("Failed");
...write this.
Output / Input...write this.
6
}
End of block.
ConditionEnd of block.
VariableConditionOutput / Input
💡 Hint: Equality check uses ===; a single = is for assignment and is not used in conditions.

🎯 Exercise & Practice

Test what you've learned — answer, write code, check instantly. (1 left)

1.Which one is the safe equality check in JavaScript?

About this topic

Conditions give a program the ability to make decisions: with if you say "if this is true, do that". In JavaScript, the scope of a condition is determined by curly braces, and indentation is only for readability — a difference that those coming from Python should note.

In JavaScript, every value can be evaluated in a condition. Zero, empty text, null, undefined, and NaN are considered false; everything else is considered true. The point to watch out for is that an EMPTY ARRAY and an EMPTY OBJECT are considered true — this is different from many languages and frequently produces bugs.

What you will learn

  • branching the flow with if, else if, and else
  • the switch structure and why break is necessary
  • making choices in a single line using the conditional operator
  • Which values are considered false
  • why an empty array and object are considered true

When to use?

You use conditions in input validation, permission checks, and showing different content based on UI state. For short two-choice situations, the conditional operator; for multi-branch situations, switch or a mapping object is more readable.

Common mistakes

Wrong

const items = [];
if (items) {
  console.log("full");
}

The empty array is considered TRUE in JavaScript; this condition always runs and prints "full". To check if the array is full, you need to look at its length.

Correct

const items = [];
if (items.length > 0) {
  console.log("full");
}

Wrong

switch (role) {
  case "admin":
    console.log("administrator");
  case "user":
    console.log("user");
}

When break is not written, switch CONTINUES to the next case; when the role is admin, both lines are printed. This behavior is called fall-through and is very frequently encountered even when unwanted.

Correct

switch (role) {
  case "admin":
    console.log("administrator");
    break;
  case "user":
    console.log("user");
    break;
}

Wrong

if (value = 10) {
  console.log("ten");
}

A single equals sign performs assignment, and since the assigned value is considered true, the block always runs. JavaScript does not consider this an error, only some linters warn — which is why it is a hard-to-spot logic error.

Correct

if (value === 10) {
  console.log("ten");
}

Frequently asked questions

Which values are considered false?

Exactly six values: false, zero, empty text, null, undefined, and NaN. EVERYTHING else outside of these is considered true — including an empty array and an empty object. This list is short and worth memorizing, because most condition bugs come from here.

switch or else if chain?

If you are comparing a single value against many constants, switch is more readable. If conditions involve ranges or multiple variables, else if is needed. Using a mapping object instead of very long switch structures is a common alternative in modern JavaScript.

When should I use the conditional operator?

When making a quick choice between two options, especially when assigning a value or choosing text in the UI. Nested conditional operators quickly ruin readability; do not go beyond two levels.

Summary

In JavaScript, only six values are considered false; an empty array and empty object are TRUE, and this is the most common source of bugs. Forgetting to write break when using switch leads to unwanted fall-through behavior.

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