DEV Community

Matthew Cullen
Matthew Cullen

Posted on

2 2

Scope in JavaScript

Global Scope

Contains variables declared outside of any functions

let myCat = "Akira"

function feedCat(){
  console.log(`${myCat} was fed.`) //Akira was fed
}
Enter fullscreen mode Exit fullscreen mode

the function feedCat has access to the variable myCat because it was declared in global scope.

Local Scope

Variables defined within a function

function printName(){
  let name = "Matt"
    console.log(name) //Matt 
}

console.log(name) //Uncaught ReferenceError: name is not defined
Enter fullscreen mode Exit fullscreen mode

name is declared in the function printName and cannot be accessed outside of it.

Block Statements

let and const declared in blocks(if,switch,for,while) support the declaration of local scope
vars do not.

function myFunction() {
    var a = "first Value"
    let b = "first Value"
    const c = "first Value"
    d = "first Value"
    if (true) {
      var a = "second Value"
      let b = "second Value"
      const c = "second Value"
      d = "second Value"
    }
    // what will each statement log to the console?
    console.log(a) //second value
    console.log(b) //first value
    console.log(c) //first value
    console.log(d) //second value
}
Enter fullscreen mode Exit fullscreen mode

let and const support local scope in block statements, console.log is called outside of the if block so it has no idea what the values of b and c are within that block.

var does not support local scope in a block statement so when if (true) is evaluated and var a is assigned "second value" it overwrites var a = "first value"

d is a global variable and so it can be accessed and changed anywhere in our code.

Image of Datadog

Master Mobile Monitoring for iOS Apps

Monitor your app’s health with real-time insights into crash-free rates, start times, and more. Optimize performance and prevent user churn by addressing critical issues like app hangs, and ANRs. Learn how to keep your iOS app running smoothly across all devices by downloading this eBook.

Get The eBook

Top comments (0)

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay