<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/scopes-in-js-z0lhwc4zt" -->

---
title: Scopes in JS | daily.dev
description: Discussion about "Scopes in JS" on daily.dev - join the developer community
canonical: https://daily.dev/posts/scopes-in-js-z0lhwc4zt
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Scopes in JS | daily.dev
og:description: Discussion about "Scopes in JS" on daily.dev - join the developer community
og:url: https://daily.dev/posts/scopes-in-js-z0lhwc4zt
og:image: https://api.daily.dev/og/posts/z0lHWc4ZT.png
og:image:alt: Scopes in JS
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Scopes in JS

**[navya](https://daily.dev/sources/dlrmprw88mjnbukqs8t0w)** · [@navyaak](https://daily.dev/navyaak) · 0 upvotes · 0 comments

## Content

Scope refers to where variables, functions, and objects are accessible (visible) and can be used in a program.

1. **Global Scope**

A variable declared outside of all functions, blocks, and modules has global scope.

It can generally be accessed from anywhere in the program.

In JavaScript modules, top-level variables are not automatically global; they have module scope instead.

2 .**Module Scope**

In JavaScript modules, variables declared at the top level are scoped to that module (file).

They are not automatically accessible from other modules.

To make them available to another module, you need to explicitly export them and then import them where needed.

// math.js

export const pi = 3.14;

// app.js

import { pi } from "./math.js";

console.log(pi);

1. **Function Scope**

Variables declared inside a function are local to that function.

var, let, and const declared inside a function are accessible within that function.

However, var is function-scoped, while let and const are block-scoped.

function example() {

var a = 10;

let b = 20;

const c = 30;

console.log(a, b, c);

}

console.log(a); // Error

1. **Block Scope**

Block scope was introduced with ES6.

Variables declared using let and const are block-scoped.

They are accessible only within the block { } where they are declared.

Examples of blocks include if statements, for loops, and while loops.

if (true) {

let x = 10;

const y = 20;

}

console.log(x); // Error

console.log(y); // Error

---

[View this post on daily.dev](https://daily.dev/posts/scopes-in-js-z0lhwc4zt)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"DiscussionForumPosting","mainEntityOfPage":"https://daily.dev/posts/scopes-in-js-z0lhwc4zt","headline":"Scopes in JS","text":"Discussion about \"Scopes in JS\" on daily.dev - join the developer community","url":"https://daily.dev/posts/scopes-in-js-z0lhwc4zt","datePublished":"2026-09-07T04:46:56.404Z","dateModified":"2026-09-07T04:46:56.404Z","author":{"@type":"Person","name":"navya","url":"https://daily.dev/navyaak","image":"https://media.daily.dev/image/upload/s--vM6dTj_h--/f_auto/v1788852968/avatars/avatar_DLRmpRw88MjNBUKqs8T0W?_a=BAMAMicg0","description":"Software Engineer","interactionStatistic":{"@type":"InteractionCounter","interactionType":{"@type":"EndorseAction"},"userInteractionCount":420}},"image":"https://media.daily.dev/image/upload/s--pstyJ2_k--/f_auto/v1788756416/posts/z0lHWc4ZT?_a=BAMAMicg0","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":0},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"isPartOf":{"@type":"WebPage","url":"https://daily.dev/sources/dlrmprw88mjnbukqs8t0w","name":"navya"}}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"navya","item":"https://daily.dev/sources/dlrmprw88mjnbukqs8t0w"},{"@type":"ListItem","position":3,"name":"Scopes in JS"}]}
```

