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 Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read full post →

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs