TypeScript - If Statement


Advertisements

The ‘if…else’ construct evaluates a condition before a block of code is executed.

Syntax

if(boolean_expression) {
   // statement(s) will execute if the boolean expression is true  
} 

If the Boolean expression evaluates to true, then the block of code inside the if statement will be executed. If the Boolean expression evaluates to false, then the first set of code after the end of the if statement (after the closing curly brace) will be executed.

Flowchart

If Statement

Example

var  num:number = 5
if (num > 0) { 
   console.log("number is positive") 
}

On compiling, it will generate following JavaScript code.

//Generated by typescript 1.8.10
var num = 5;
if (num > 0) {
   console.log("number is positive");
}

The above example will print “number is positive” as the condition specified by the if block is true.

number is positive
typescript_decision_making.htm

Useful Video Courses


Video

Angular Essentials - Admin App, Typescript, Docker, c3.js

45 Lectures 4 hours

Antonio Papa

Video

NestJs: Modern ways to build APIs with Typescript and NestJs

41 Lectures 7 hours

Haider Malik

Video

Master Typescript : Learn Typescript from scratch

60 Lectures 2.5 hours

Skillbakerystudios

Video

Three.js and TypeScript

77 Lectures 8 hours

Sean Bradley

Video

TypeScript from Scratch: Go from zero to hero in TypeScript

77 Lectures 3.5 hours

TELCOMA Global

Video

Advanced TypeScript: Generic Search, Sorting and Filtering

19 Lectures 3 hours

Christopher Frewin

Advertisements