Rehman Shahid
Difference between var and let
By Rehman Shahid in JavaScript on Jul 22 2017
  • Bidyasagar Mishra
    Aug, 2019 2

    if we will declare a let inside function then we cant access that outside of the fucntion .

    • 1
  • Neeraj Negi
    Oct, 2018 8

    let keyword have a scope of block level where the var keyword have a function level scope.

    • 1
  • Rehman Shahid
    Jul, 2017 22

    difference between var and let lies in their scope i.e var scope lies to the nearest of function block and let scope lies to the nearest of enclosing blockExample:function demoVarAndLet() {// var variable scopefor (var i = 0; i < 5; i++) {}if (typeof i === 'undefined') {console.log('i(var) is undefiend and not accessible after the loop')} else {console.log('i is accessible after for loop i:' + i + ' type: var ');}for (let j = 0; j < 5; j++) {// let variable scope}if (typeof j === 'undefined') {console.log('j(let) is undefiend and not accessible after the loop');} else {console.log('j is accessible after for loop i:' + i + ' type: let')} }For Live Demo: https://jsfiddle.net/rehmanshahid/3Lnyzd49/

    • 1
  • Jin Necesario
    Oct, 2020 2

    The var keyword is called function-scoped while let is block-scoped variables.

    When JavaScript reads your variable declarations that uses the var are lifted to the top of local scope (if declared inside a function). Otherwise lifted to the top of the global scope (if declared outside a function).

    Lastly, let are accessible inside the block to where it was defined.

    • 0


Most Popular Job Functions


MOST LIKED QUESTIONS