ES6 overview – variables3 min read

ECMA stands for European Computer Manufacturer Association and is responsible for providing ECMAScript which is basically the specification while Javascript is the implementation of ECMAScript. Right now we have the new specification ES6 and Javascript moved, or better to say, transformed into a more powerfull language. While ES6 brings great things, not all frameworks fully implement it. There is a bunch of legacy stuff to support that’s why we have Babel.

Babel is used to transpile ES6 into ES5 which is supported with all older technologies (browsers, frameworks…).

Variables

Javascript is dynamic typed language which means the type of variable is inferred from the value assigned to it.

I.E. if you assign a string to it, it’s automatically defined as string or if you put a number inside variable its automatically assigned as number etc.

Variables cannot be assigned a reserved javascript words.

Variables must start with ‘$‘,’_‘ or with a letter, they cannot be number.

We assign value to a variable by entering variable name which is followed by the assignment operator ‘=’ and than a concrete value.

So far in ES5 we used reserved word ‘var’ for defining a variable. Nowdays, in ES6 we have two more keywords: ‘const’ nad ‘let’.

Preferred style is camelCase.

So what is the difference between VAR, CONST and LET?

In short, difference between LET and VAR is in scoping and declaration.

LET is scoped to block and cannot be redeclared while VAR is scoped to function.

CONST is scoped to block but cannot be re-assigned or redeclared.

– LET –


function foo() {
// iterator is nost visible here - before for()
for ( let iterator = 0; iterator < 10; iterator++) {
// iterator is only visible in here inside for()
// and is also visible as a seperate variable for each iteration of the loop
}
// iterator is not visible here - outside of the for()
}


– VAR –

function bar() {
// iterator is visible here before for()
for ( var iterator = 0; iterator < 10; iterator++) {
// iterator is visible here, inside for()
}
//iterator is visible here also, outside of the for()
}

When using LET or VAR outside a function block, they behave similar but with one main difference.

let foo = 'foo';  // globally scoped 
var bar = 'bar'; // globally scoped

Global variables defined as LET will not be added as properties on the global object WINDOW.

console.log(window.foo); // undefined 
console.log(window.bar); // 'bar'

Re-declaration:

If strict mode, VAR will let you re-declare variable while LET will not.

– VAR –

'use strict'; 
var foo = 'foo';
var foo = 'bar'; // Without error, `foo` is replaced.

– LET –

'use strict'; 
let foo = 'foo';
let foo = 'bar'; // SyntaxError: Identifier 'foo' has already been declared

Additional:

There is a new way of entering variable inside text / template strings:

console.log(`${firstName} ${lastName}`)

which is basically a preferred way over:

var fullName = firstName + ' ' + lastName;
console.log(fullName);

As you can see, Javascript is moving forward each day.

Leave a Reply

Your email address will not be published. Required fields are marked *