Results 1 to 7 of 7
  1. #1
    Senior Member dennis123's Avatar
    Join Date
    Apr 2013
    Location
    Bangalore
    Posts
    2,492

    What is the main difference between var and let in JavaScript?

    What is the main difference between var and let in JavaScript?

  2. #2
    Registered User
    Join Date
    Sep 2017
    Posts
    1,201
    let enables you to pronounce factors that are restricted in degree to the piece, explanation, or articulation on which it is utilized. This is not at all like the var catchphrase, which characterizes a variable comprehensively, or locally to a whole capacity paying little mind to piece scope. A clarification of why the name "let" was picked can be found here.

  3. #3
    Member
    Join Date
    Nov 2017
    Posts
    37
    The difference is scoping. var is scoped to the nearest function block and let is scoped to the nearest enclosing block, which can be smaller than a function block. Both are global if outside any block.

    Also, variables declared with let are not accessible before they are declared in their enclosing block. As seen in the demo, this will throw a ReferenceError exception.
    Check out our new Online Email Validator @ Zero Bounce
    Our unique email validation service eliminates high bounce rates.

  4. #4
    let creates a variable that is only accessible in the block that surrounds it

    var - creates a variable whose scope is accessed through the function that contains it.

  5. #5
    Senior Member
    Join Date
    May 2015
    Location
    UK
    Posts
    243
    Scoping makes the difference. var's scope is function level but let is scoped in its enclosing block, that is ({}).

  6. #6
    Registered User
    Join Date
    Mar 2019
    Location
    Ahemdabad
    Posts
    17
    Var and let both are used for function declaration. The difference between Var and Let is that Var is function scope and Let is the block scope.Variable declared with var is defined throughout the program while Let is not
    .NET Developer At iFour Technolab Pvt Ltd
    http://www.ifourtechnolab.com/

  7. #7
    Registered User
    Join Date
    Mar 2019
    Location
    Ahmedabad
    Posts
    34
    var and let are both used for function declaration in javascript.
    Main difference between them is the scope difference.That is var is function scoped and let is block scoped.
    Example::
    1)let
    Code:
    for(let i=0;i<10;i++){
    console.log(i); //i is visible thus is logged in the console as 0,1,2,....,9
    }
    console.log(i); //throws an error as "i is not defined" because i is not visible
    2)var
    Code:
    for(var i=0; i<10; i++){
    console.log(i); //i is visible thus is logged in the console as 0,1,2,....,9
    }
    console.log(i); //i is visible here too. thus is logged as 10.
    Last edited by Sherin; 06-27-2019 at 03:32 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

  Find Web Hosting      
  Shared Web Hosting UNIX & Linux Web Hosting Windows Web Hosting Adult Web Hosting
  ASP ASP.NET Web Hosting Reseller Web Hosting VPS Web Hosting Managed Web Hosting
  Cloud Web Hosting Dedicated Server E-commerce Web Hosting Cheap Web Hosting


Premium Partners:


Visit forums.thewebhostbiz.com: to discuss the web hosting business, buy and sell websites and domain names, and discuss current web hosting tools and software.