IF ELSE IN TYPESCRIPT | BASIC

Share the post

if else statement

Hello All,

In this topic we will learn about if statement, if else in TypeScript. These are the most basic statement useful to develop any web application. We will see this statement in details with example. If you want to learn about if & if else in JavaScript then click here.

if :

Example:

In if statement, conditions is true then only the code inside statement will execute, otherwise not. In below example, If the condition inside of if statement is true then only it will console the log “condition is true” & if statement is false then it will not console the log “condition is false”.

testStatement() {
    if (true) {
      console.log("condition is true");
    }

    if (false) {
      console.log("condition is false ")
    }
  }

if-else statement:

If you have the situation like if the statement is true then will execute some code & if it’s not then also execute some code, then at this situation if-else statement will help you. How? Please see below example.

Example:

testStatement() {
    let tempStringA: string = "stringA";
    let tempStringB: string = "stringB";
    if (tempStringA == tempStringB) {
      console.log("string is matched");
    } else {
      console.log("string not matched");
    }
  }

So, In above example will give you the output like “string not matched”, as the condition inside if statement is false then it will execute the code inside else statement. If you want to learn more about TypeScript then visit this page.

Leave a Comment