We build your ideas. Contact us for Mobile/Web App Development. Write us @ contact@milanjeet.com

Difference Between var, let and const Keywords in JavaScript

Javascript var, let and const

JavaScript is a popular client side programming language. New module or features are released over the period of time. ES6 (ES2015) introduces a new keywords – let and const to use side by var. We will take a closer look at ‘let‘, ‘const‘, ‘var‘ in this article. So let’s get started.

VAR

var is the most used declarative part in JavaScript to declare any variable and it ruled like a King before ES6. Though, there are some issues with variable respecting to its scope. Hence, it was necessary to build new ways to declare variables. As we talked about scope, let’s get in touch with Scope for more details before we dive into example.

SCOPE OF VARIABLE – VAR

Normally, scope means accessibility, where an all we can access declared variables with var. Var declarations are globally scoped as well as function/locally scoped. Visit this link for more about globally and locally scoped variables if you don’t know already.

var declared outside of the function block is global and can be accessed anywhere in the program whereas var declared in function and method block is limited to corresponding function only i.e. it can only be accessed within the function not outside of it. Let’s understand it by below example.

var a = 10;
console.log(a); //output 10
(function b(){
    a = 20;
    console.log(a); //output 20
})();
console.log(a); //output 20

Output:

Let

let variable are blocked scope that means the life cycle of let variable if limited to the method or function in which they are defined.

 

 

 

 

 

 

 

 

i is declared outside of the function block and accessible everywhere in the program. Inside the function block i is reassigned with some other value and this i value is accessible to this this block only. Accessing  outside of this block will return 10 as in above example.

So let is “block scoped”.

Const

As we all aware, const value once assigned, can not be changed during the program. It restricts over writing / reassigning of the const variables.

jQuery Pro - const example output

 

 

 

 

 

 

 

 

const declared in function block, can not be accessed outside.

5 Comments

  1. I am sure this article has touched all the internet people,
    its really really nice piece of writing on building
    up new web site.

  2. Its like you read my thoughts! You appear to understand a lot about this,
    like you wrote the ebook in it or something. I think that you just can do with
    some p.c. to drive the message home a little bit, however instead of that,
    that is excellent blog. An excellent read. I will definitely
    be back. http://cria.link/3tAA

  3. Thanks for the marvelous posting! I actually enjoyed reading it,
    you can be a great author.I will be sure to bookmark your blog and will eventually come back later
    on. I want to encourage yourself to continue your great job, have a nice holiday weekend!

  4. Thanks for the marvelous posting! I actually enjoyed reading it,
    you can be a great author.I will be sure to bookmark your blog and
    will eventually come back later on. I want to encourage yourself to
    continue your great job, have a nice holiday weekend!

  5. Excellent goods from you, man. I have keep in mind your stuff previous to and you’re just extremely excellent.
    I actually like what you’ve obtained right here, really like what you’re saying and the way in which through which you are
    saying it. You make it enjoyable and you still care for to
    stay it wise. I can not wait to read much more from you.
    This is really a tremendous web site.

Leave a Reply

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

Enjoy this blog? Please spread the word :)