TypeScript - If Statement



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
Advertisements