TypeScript if – Conditional Statement

TypeScript if conditional statement is used to execute a block of code conditionally. If block has an expression which when evaluated returns boolean value. Based on the return value, a decision is taken if the following code block has to be executed or not.

If the return value of if expression is true, then the code block is executed, otherwise the code block is not executed.


Following is the syntax of TypeScript if statement :

if(expression) {
   /* block of statements */
  • expression should return a boolean value.
  • block of statements enclosed between curly braces are executed only if the expression evaluates to true.

Example – TypeScript if

Following is an example TypeScript code to demonstrate if conditional statement.


var a:number = 1
var b:number = 3

if(a == 1){
    console.log("value of a is 1.")

if(a == b){
    console.log("a and b are equal.")

When the above code is compiled using typescript compiler, tsc ifExample.ts , following JavaScript code is generated.


var a = 1;
var b = 3;
if (a == 1) {
    console.log("value of a is 1.");
if (a == b) {
    console.log("a and b are equal.");

For this example, you might notice that there is no difference between TypeScript and JavaScript if code blocks.


In this TypeScript Tutorial, we have learnt about if conditional statement in TypeScript, its syntax and also working examples. In our next tutorial, we shall learn if-else conditional block in TypeScript.