ProgramGuru

if Statement

Learn if Statement

An if statement runs a block only when its condition is truthy.

In this lesson, you will study the rule, run a complete TypeScript example, explain its output, and test a small change.

What you’ll learn

  • Explain the main rule behind if statement.
  • Predict the result of the complete runnable TypeScript example.
  • Recognize one common mistake and correct it.

Understand the concept

Place a condition in parentheses followed by a block in braces. Conditions often use comparisons that produce booleans, but JavaScript also accepts truthy or falsy values. TypeScript uses checks such as typeof and equality inside conditions to narrow variable types.

See the concept visually

Use the diagram to locate the checks, values or branches involved in the example.

if Statement: illustrated flowEvaluate condition; True → run body; False → skip body; Continue afterwardEvaluate conditionTrue → run bodyFalse → skip bodyContinue afterward
if Statement: illustrated flow. Evaluate condition; True → run body; False → skip body; Continue afterward.

Run a complete TypeScript example

Save this code as main.ts or paste it into the TypeScript Playground. The example uses fixed values so it runs without prompting for input.

TypeScript code

const temperature: number = 34;
 if (temperature > 30) {
   console.log("Hot day");
 }
 console.log("Check complete");

Verified output

Hot day
Check complete

How to run locally: npx tsc --strict --target es2022 --module commonjs main.ts, then node main.js. When using a project configuration, run the matching project build instead.

Trace the execution

34 > 30 is true, so Hot day prints. Execution then continues after the if block and prints Check complete.

Predict the output before you run the sample. Compare each line with the source expression that produced it, and distinguish the type checker’s work from what happens in JavaScript at runtime.

Common mistakes and how to avoid them

Do not put a semicolon immediately after if (condition); because that creates an empty statement and can detach the intended block.

Debugging habit: when a change is rejected, read the exact compiler diagnostic and inspect the operand types. When it compiles but behaves unexpectedly, follow the values through the JavaScript expressions.

Practice it yourself

Change temperature to 25. Explain why Check complete still prints even though Hot day does not.

Challenge: write down your prediction first, run the changed source, and explain any difference. Return to the verified version before continuing.

Check your understanding

When does an if body run?
Choose one answer. You can try again.

Answer and explanation

When its condition evaluates as truthy.

The condition controls only its associated branch.

Review the key idea

if Statement: Place a condition in parentheses followed by a block in braces.

The condition controls only its associated branch.

Before continuing, be able to explain what is checked before execution and what actually changes at runtime in the example above.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: if...else Statement →