Core concepts of Javascript ES6.

Core concepts of Javascript ES6.

Part-1:Variables.

Β·

2 min read

Hello hashnode family πŸ‘‹.It's nice to meet you all again after so many days. Today I am announcing the launch of another tutorial series named Core features of Javascript ES6. I hope that you all will like this tutorial series.

Javascript ES6 also known as ECMAScript 2015 was the major revision to javascript which was published in 2015. You can do the same amount of work with ES6 just as you did with ES5 but with fewer lines of code. Additionally, it also provides you with some awesome features which you would love to try. The only prerequisite for this tutorial series is that you must be comfortable with the basics of javascript(variable, arrays, loops, functions).

Before the advent of ES6, only var was used to define variables but after that, we can also use const and let to define variables apart from using var.

There were many problems with the var variable which were later solved by const and let. Let's explore the problem caused by var in detail.

Can be redeclared

var variables can be redeclared in the same scope.πŸ‘‡πŸ‘‡πŸ‘‡

var a=1;
var a=2;
console.log(a) //prints 2

Although it does not seem that it will cause any issue but if your application has a large codebase then it will create a bigger problem as you can accidentally redeclare and initialize it in the same scope of your application . This will show some other results and you will have a hard time debugging it as it will not show any errors.

var variables are not block-scoped

Not having block-scoped means that you can access variables outside the block in which you previously declared which can create many problems if you can imagineπŸ‘‡πŸ‘‡πŸ‘‡

if(true){
  var a ="hello world";
}
console.log(a);//hello world`

let

Unlike variables in var in let, variables cannot be redeclared as it will give a syntax error πŸ‘‡πŸ‘‡πŸ‘‡

let a="hello world";
let a="hello world again";//SyntaxError: Identifier 'a' has already been declared
console.log(a);

Unlike var let variables are block-scopedπŸ‘‡πŸ‘‡πŸ‘‡

if(true){
  let a ="hello world";
}
console.log(a);//ReferenceError: a is not defined

const

Variables defined with const maintain constant values which means that they can neither be updated nor redeclared.However, the properties of an object declared with const can be updated.πŸ‘‡πŸ‘‡

const name = {
        first_name: "Tanay",
        last_name:"Dwivedi"
}
console.log(name.first_name);//Tanay

name.first_name="Hello world";

console.log(name.first_name);//Hello world

const variables are block-scoped just like let.

Thank you for reading :).

That's all for this blog. The second part of this tutorial will be released soon. If you found my content useful then consider supporting me on "Buy Me a Coffee".

Β