TypeScript if - Example - Tutorial Kart

TypeScript if ? Example

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.

Syntax

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.

ifExample.ts

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.

ifExample.js

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.

Conclusion

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.

TypeScript

TypeScript Tutorial TypeScript Variables TypeScript if TypeScript if-else TypeScript Switch TypeScript For Loop TypeScript While Loop TypeScript Do-While Loop TypeScript Arrays TypeScript Tuples TypeScript Unions TypeScript Functions TypeScript Anonymous Functions

TypeScript Object Oriented Concepts

TypeScript Class TypeScript Inheritance TypeScript Method Overriding TypeScript Interface

TypeScript String Operations

TypeScript String Length TypeScript String Split

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download