TypeScript Variable Declarations


TypeScript has introduced two new types of variable declarations let and const. Here let is similar to var in some respects and const is an augmentation of let and prevents re-assignment to a variable.

Here you will learn both types of variable declarations and why they're preferable over to var.

var Declarations

This is similar to JavaScript variable declarations using var keyword. For Example:

var a = 210;
var message = "Some string;
Scoping rules

The var declarations are accessible anywhere within their containing function, module, namespace, or global scope.

function Display(setValue: boolean) {
    if (setValue) {
        var counter = 10;
    }
    return counter; //Accessible outside if block
}
Display(true);  // returns '10'
Display(false); // returns 'undefined'

Here, variable counter was declared within the if block, but still accessible outside the if block.

let Declarations

This variable declarations are similar to var variable declarations. But still has some semantics key difference from var. For Example:

let a = 210;
let message = "Some string;
Scoping rules

The let variables are block-scoped variables and are not visible outside of their nearest containing block or for-loop.

function Display(setValue: boolean) {
	let x = 10;
    if (setValue) {
        let counter = 10;
    }
    return counter; //Not Accessible, Error : counter not exist here
}

Here, variable counter was declared within the if block and not accessible outside the if block. But x declared in function block and is accessible anywhere within function block.

const Declarations

The const variable declarations are another way of variable declaration and similar to let but, const value can not be changed. For Example:

const x = 9;
const Customer = {
    fname: "Jimi",
    lname: "Scott",
}
Scoping rules

The const follow the same scoping rules as let, but you can't re-assign to them.

const x = 9;
const Customer = {
    fname: "Jimi",
    lname: "Scott",
}

// Error : can not change
Customer = {
    fname: "John",
    lname: "Wood"
};

See Also

AngularJS Built-in Filters
AngularJS Built-in Directives
AngularJS Expression
AngularJS Built-in Events
AngularJS Scope Methods
AngularJS Built-in Services
AngularJS Controller Inheritance
AngularJS Custom Directives



 
SHARE THIS IF YOU LIKE!
submit to reddit

TYPESCRIPT LANGUAGE TECHNIQUES

SUBSCRIBE FOR LATEST UPDATES
HAVE WE CONNECTED ON FACEBOOK?
TOP TUTORIALSTOP CODE EXAMPLESTOP LIVE DEMOLINKSSTAY CONNECTED
HTML Tutorial
HTML5 Tutorial
Bootstrap3 Tutorial
Javascript Tutorial
TypeScript Tutorial
AngularJS Tutorial
CSharp Tutorial
PHP Tutorial
Developer Tools
Javascript
AngularJS
Google MAP API V3
ASP.NET
Javascript
AngularJS
Google MAP API V3
Contact Us
Advertise with Us
Privacy Policy
Disclaimer
Stay connected. We actually like you. If you want to get up to the minute updates, then connect with us on your fav below.
 
Twitter   Linkedin   Facebook   Google+    RSS   Pinterest
 
Protected by Copyscape Original Content Checker
 
 
Copyright ©2017 www.techstrikers.com Unauthorized reproduction/replication of any part of this site is prohibited.