Coding

Javascript – Scope

Scope

the life-time of a variable;

Global scope – variables declared without a (var) are in the global scope. Their life time is the longest. They are also easy to mutate at any time, thus you should take extra care when declaring variables in the global scope.

Lexical scope – defines the visibility of the variable;

function scope(){
        //function declaration
      var myname = "Comfort Ajala"

     var innerfunction = function(){
           //function expression

         var myname = "Karen Ajala"

          console.log(myname)
       }
}

The inner function, as shown above, will begin looking up the variable myname in its own context. It finds that myname is “Karen Ajala” and uses it as declared.

 

Please follow and like us:
0

Enjoy this blog? Please spread the word :)

error: Content is protected !!