TypeScript Tutorial

TypeScript If Else

if runs a block when a condition is true. else and else if cover the other paths.

if runs a block

Write if, a condition in parentheses, then a brace-wrapped block. If the condition is true, the block runs. If it is false, TypeScript skips the block and continues after the closing brace.

Always use braces, even for one statement. A later edit that adds a second line without braces will not do what it looks like. This tutorial never omits them.

Example

const score: number = 72;
if (score >= 50) {
  console.log("pass");
}

Change score and compile in /typescript/try. At 49 the program prints nothing. That is a reason to add else.

else covers the rest

Pair else with an if when you need exactly one of two paths. One block runs. The other does not. There is no third option in a plain if/else.

Example

const score: number = 41;
if (score >= 50) {
  console.log("pass");
} else {
  console.log("fail");
}

else if chains more tests

else if means: the previous tests were false, so try this one. TypeScript walks the chain from the top and runs the first block whose condition is true. A final else catches everything that did not match.

Order matters. Put the strictest test first. If you check score >= 50 beforescore >= 90, a 95 never reaches the A branch.

Example

const score: number = 82;
if (score >= 90) {
  console.log("A");
} else if (score >= 80) {
  console.log("B");
} else if (score >= 70) {
  console.log("C");
} else {
  console.log("below C");
}

Nested if

An if inside another if is a nested if. Use it when the second question only makes sense after the first one is true. Indent the inner block so a person can see the shape.

Example

const age: number = 20;
const hasTicket: boolean = true;
if (age >= 18) {
  if (hasTicket) {
    console.log("enter");
  } else {
    console.log("buy a ticket");
  }
} else {
  console.log("too young");
}

Two independent facts can often be one condition: if (age >= 18 && hasTicket). Nest when the inner else needs a different message.

Conditions you will write

NeedWrite
Equalif (a === b) { ... }
Not equalif (a !== b) { ... }
Both trueif (a && b) { ... }
Either trueif (a || b) { ... }

Do not write if (x = 1). That assigns 1 to x and then treats it as true. Compare with ===. TypeScript’s usual equality is three equals, not two. == coerces types; this course uses ===.

Braces are not optional here

JavaScript lets you skip braces when the body is one statement. Skip them and a later line that looks nested is actually outside the if. Always open and close { }. Match them. Indent the body.

There is no elif keyword. The spelling is two words: else if. The hour-of-day chain in your head is the same shape as the grade chain above: test the smallest bound first, then the next, thenelse.

When if is the wrong tool

If you are picking among many exact labels — a menu choice, a day number, a string command — aswitch is often clearer. That is the next chapter. For ranges such as scores, stay withelse if.

FAQ: TypeScript If Else

Common questions about this page.

What is the StudyGrid TypeScript tutorial?

The StudyGrid TypeScript tutorial follows the same chapter rhythm as C++: syntax, types, input, loops, functions, classes, generics, maps, and lambdas. Each chapter has copy-and-run examples.

Should I run typescript if else examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn typescript if else in this TypeScript TypeScript lesson (TypeScript If Else).

Is the TypeScript editor the same as Try Python or Try C++?

No. Try TypeScript type-checks with tsc at /typescript/try and shows stdout plus compiler messages. Try Python stays at /try. Try C++ stays at /cpp/try. TypeScript lessons never open those editors.

Do I need to install a compiler to learn TypeScript?

No. Open a chapter, click Try it in TypeScript, and compile in the browser. You can also download a .ts file and compile locally with tsc.

Where should I start the TypeScript tutorial?

Start at TypeScript Intro, then Get Started and Syntax. After the first program, continue to output, variables, and if-else. After classes, open TypeScript Examples, then generics, Map, and arrow functions. Use Next at the bottom of each chapter.

Is the TypeScript tutorial free?

Yes. The TypeScript workshop on StudyGrid (studygrid.in) is free: dashboard, chapters, and the compile-and-run editor.