Please enable Javascript to correctly display the contents on Dot Net Tricks!

Statements

Posted On : 09 Mar 2017
Updated On : 09 Mar 2017
Total Views : 750

Statements

TypeScript is a superset of JavaScript which provides static type checking for JavaScript. TypeScript uses JavaScript syntax to define statements.

Here, we will discuss about the TypeScript statements which you can use to write your TypeScript code.

Variable Statement

In TypeScript, a variable statement is extended to include optional type annotations.

Syntax

  :  = ; 
  : ;

Variable Declaration using var, let and const

Before ES6, JavaScript has only var keyword to define a variable. Now, ES6 provides let and const keywords to declare a variable in JavaScript.

  1. var - It supports only function scope. It doesn’t support block scope.

  2. let - It is similar to var but it supports block scope. Unlike var you cannot redeclare it twice in the same scope.

  3. const - It is similar to let but its value cannot be changed. Also, at the time of declaration you have to assign value to it.

variable.js

var x = 4;
let number = 50;
const key = 'xyz123';

//OR
var x: number = 4;
let number: number = 50;
const key: string = 'xyz123';

Block Scoping

let supports block scope. When you define, a variable using let within a block like if statement, for loop, switch statement etc., block-scoped variable is not visible outside of its containing block.

block.js

function foo(flag: boolean) {
 let a = 100;
 
 //if block
 if (flag) { 
 // 'a' exists here
 let b = a + 1;
 return b;
 }
 // Error: 'b' doesn't exist here
 return b;
}

Hoisting

When you use var to declare a variable, you have to deal with hoisting. Hoisting is JavaScript's default behavior of moving variable declarations to the top of the current scope (i.e. top of the current js file or the current function). It means, in JavaScript a variable can be used before it has been declared.

hoisting.js

var x = "Gurukulsight";
function foo() { 
 console.log(x); //Gurukulsight
}

function bar() {
 console.log(x); //undefined
 var x; //x declaration is hoisted, that’s why x is undefined
}
foo();
bar();

In above bar function declaration of x variable will move to the top of bar function variable declaration and within bar function, we have not specified any value to x, hence it’s default value is undefined.

Important Information

  1. JavaScript only hoists variable declarations, not variable initializations.

  2. To avoid issues because of hoisting, always declare all variables at the beginning of every scope.

  3. Use ES6 - let and const to avoid hoisting.

hoistingfixed.js

var x = "Gurukulsight";
function bar() {
 console.log(x); //Error: x is not defined
 let x; //x declaration is not hoisted
}
bar();

Destructuring

Destructuring means breaking up the structure. Destructuring is a way to quickly extract the data from a single object or array and assign it to multiple variables within a single statement. ES6 also supports object destructuring.

TypeScript supports the following forms of Destructuring:

  1. Object Destructuring

  2. Array Destructuring

destructuring.ts

let list = ['one', 'two', 'three'];
let [x, y, z] = list; //destructuring the array values into three variables x,y,z

console.log(x); // 'one'
console.log(y); // 'two'
console.log(z); // 'three'

let obj = {x: 'one', y: 'two', z: 'three'};
let {x, y, z} = obj; // destructuring the object properties x, y, z

console.log(x); // 'one'
console.log(y); // 'two'
console.log(z); // 'three'

Destructuring can also be used for passing objects into a function which allow you to extract specific properties from an object.

destructuring_auguments.ts

function greet({firstName, lastName}): void {
 console.log(`Hello, ${firstName} ${lastName}!`);
}

let p1 = { firstName: 'Shailendra', lastName: 'Chauhan' };
let p2 = { firstName: 'Kanishk', lastName: 'Puri' };

greet(p1) // -> Hello, Shailendra Chauhan!
greet(p2) // -> Hello, Kanishk Puri!

If, do..while, while and for Statements

In TypeScript, if, do..while, while and for statements you can use in same way as you use in JavaScript.

statements.ts

if (true) {
 console.log("if statement");
}

let i: number = 1;
do {
 console.log("do..while loop");
 i++;
} while (i <= 1);

let j: number = 1;
while (j <= 1) {
 console.log("while loop");
 j++;
}

for (let k: number = 1; k < 4; k++) {
 console.log(k);
}

For..In Statement

The for...in loop iterates over the properties of an iterable object i.e. list type. The for...in loop returns a list of keys on the object being iterated and using the keys on that object you can access values.

forin.ts

let list: number = [4, 5, 6];

for (let i in list) {
 console.log(i); //keys: "0", "1", "2"
 console.log(list[i]); //values: "4", "5", "6"
}

For..Of Statement

The for...of loop iterates over the properties of an iterable object i.e. list type. The for...of loop returns a list of values on the object being iterated.

forin.ts

let list: number = [4, 5, 6];

for (let i of list) {
 console.log(i); //values: "4", "5", "6",
}

Operators

TypeScript supports all of the standard JavaScript operators like arithmetic operators, comparison operators, assignment operators, conditional operators, bitwise operators, logical operators and Type Operators.

What do you think?

I hope you will enjoy the Statements in TypeScript while developing your web app. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.



+