Monday, 4 Aug 2025
  • My Interests
  • My Saves
  • Try Intents
Subscribe
Focus - Code Reveals
  • Home
  • HTML
    What is Symentic HTML

    What is Symentic HTML?

    By Chief Editor

    What are the async and defer attributes in the “script” tag?

    By Chief Editor

    What are the different types of HTML tags?

    By Chief Editor

    What is a Meta Tag in HTML?

    By Chief Editor

    What is Block level Element and Inline Level Element?

    By Chief Editor

    What is Doctype HTML in HTML?

    By Chief Editor
  • JavaScript

    Difference between document.createElement and document.createElementFragement in JavaScript?

    By Chief Editor

    Explain Deep Copy and Shallow Copy in JavaScript.

    By Chief Editor

    What is a Closure in JavaScript?

    By Chief Editor
    What are the Lexical Scope in JavaScript

    What are the Lexical Scope in JavaScript?

    By Chief Editor

    What is one-way data binding in React?

    By Chief Editor

    What is hoisting in JavaScript with an example?

    By Chief Editor
  • Frontend Interview

    Explain Deep Copy and Shallow Copy in JavaScript.

    By Chief Editor

    Difference Between position: relative and position: absolute in CSS

    By Chief Editor

    What is Block level Element and Inline Level Element?

    By Chief Editor

    What is Position in CSS?

    By Chief Editor

    What are the Lexical Scope in JavaScript?

    By Chief Editor

    How to Reverse a String in JavaScript: Two Essential Methods

    By Chief Editor
  • Backend Interview

    What is localization in React?

    By Chief Editor

    Difference Between position: relative and position: absolute in CSS

    By Chief Editor

    What are the drawbacks of React?

    By Chief Editor

    What is React Router Dom in React?

    By Chief Editor

    What is Scope in JavaScript?

    By Chief Editor

    What is the this Keyword in JavaScript?

    By Chief Editor
  • Nodejs
  • JavaScript Interview
  • React Interview
  • Frontend Interview
  • Backend Interview
  • Contact Us
  • Advertise with Us
  • Complaint
  • Cookies Policy
  • Privacy Policy
  • Donate
  • 🔥
  • ReactJS
  • JavaScript
  • JavaScript Interview
  • React Interview
  • HTML
  • Frontend Interview
  • CSS
  • Redux
  • Javascript
  • System Design
Font ResizerAa
Focus - Code RevealsFocus - Code Reveals
  • My Saves
  • My Interests
  • My Feed
  • History
  • Technology
Search
  • Homepage
  • ReactJS
  • JavaScript
  • JavaScript Interview
  • HTML
  • CSS
  • Backend Interview
Have an existing account? Sign In
Follow US
© 2022 Code Reveals Inc. All Rights Reserved.
Home Blog What is Scope in JavaScript?
JavaScriptJavaScript Interview

What is Scope in JavaScript?

Chief Editor
Last updated: February 14, 2025 7:35 am
Chief Editor
Share
SHARE

Scope in JavaScript refers to the context in which variables are accessible. It determines which part of the code can access and manipulate a particular variable.

Contents
Types of Scope in JavaScript:Scope Chain:Summary Table:

Types of Scope in JavaScript:

1. Global Scope:

  • Variables declared outside any function or block.
  • Accessible anywhere in the code.
javascriptCopyEditlet globalVar = 'I am global';

function example() {
  console.log(globalVar); // Accessible here
}

console.log(globalVar); // Accessible here too

2. Local/Function Scope:

  • Variables declared inside a function using var, let, or const.
  • Accessible only within that function.
javascriptCopyEditfunction example() {
  let localVar = 'I am local';
  console.log(localVar); // Accessible here
}

console.log(localVar); // Error: localVar is not defined

3. Block Scope (ES6):

  • Introduced with let and const.
  • Variables declared inside a block {} are only accessible within that block.
javascriptCopyEdit{
  let blockVar = 'I am block-scoped';
  console.log(blockVar); // Accessible here
}

console.log(blockVar); // Error: blockVar is not defined

Note: var does not have block scope. It is function-scoped.


4. Lexical Scope (Closures):

  • A function can access variables from its outer scope where it was defined.
  • Inner functions have access to variables of outer functions.
javascriptCopyEditfunction outer() {
  let outerVar = 'I am outer';

  function inner() {
    console.log(outerVar); // Accessible due to lexical scope
  }

  inner();
}

outer();

Scope Chain:

  • When a variable is accessed, JavaScript looks for it in the current scope.
  • If not found, it moves up to the outer scope, continuing until it reaches the global scope.
  • If still not found, it throws an error.
javascriptCopyEditfunction example() {
  let a = 'local';

  function inner() {
    console.log(a); // Looks for 'a' in inner scope -> not found, moves to outer scope -> found
  }

  inner();
}

example();

Summary Table:

Scope TypeDeclared withAccessible from
Global Scopevar, let, constAnywhere in the program
Function Scopevar, let, constInside the function
Block Scopelet, constInside the block {}
Lexical Scope(Any)Inner functions can access outer variables

Understanding scope is crucial to avoid variable conflicts, manage data properly, and write clean, bug-free JavaScript.

Share This Article
Email Copy Link Print
Previous Article What are the map, filter, and reduce methods in JavaScript?
Next Article What is the this Keyword in JavaScript?
Leave a Comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Your Trusted Source for Accurate and Timely Updates!

Our commitment to accuracy, impartiality, and delivering breaking news as it happens has earned us the trust of a vast audience. Stay ahead with real-time updates on the latest events, trends.
FacebookLike
XFollow
InstagramFollow
YoutubeSubscribe
LinkedInFollow
QuoraFollow
- Advertisement -
Ad imageAd image

Popular Posts

How can you delay the dispatch in React?

To delay a dispatch in React, especially when using something like Redux or React state…

By Chief Editor

What are the Rest and Spread operators in JavaScript?

Rest and Spread Operators in JavaScript (...) The Rest Operator and Spread Operator both use…

By Chief Editor

Explain Call, Apply and Bind in JavaScript.

Call, Apply, and Bind in JavaScript In JavaScript, call(), apply(), and bind() are methods that…

By Chief Editor

You Might Also Like

JavaScriptJavaScript Interview

Difference between document.createElement and document.createElementFragement in JavaScript?

By Chief Editor
JavaScriptJavaScript Interview

What is the this Keyword in JavaScript?

By Chief Editor
JavaScript

Explain var let and const in JavaScript with Example.

By Chief Editor
JavaScriptJavaScript Interview

What is memoization in JavaScript?

By Chief Editor

Focus by CodeReveals is your dedicated platform for mastering tech interviews and advancing your development skills. Whether you’re aiming to become a Frontend Developer, Backend Developer, or simply preparing for your next big interview, we’re here to guide you every step of the way.

Our mission is to help aspiring developers gain real-world knowledge, build confidence, and succeed in technical interviews. We offer structured content, curated interview questions, coding challenges, and practical guidance — all designed by experienced professionals who understand what top tech companies are looking for.

At Improve, we don’t just teach — we prepare you to think like a developer, solve like an engineer, and present like a pro.

Join us and start improving today — because your dream tech job is within reach

Most Famous
  • HTML
  • CSS
  • JavaScript
  • NodeJS
Top Categories
  • JavaScript Interview
  • React Interview
  • Frontend Interview
  • Backend Interview
Usefull Links
  • Contact Us
  • Advertise with Us
  • Complaint
  • Cookies Policy
  • Privacy Policy
  • Donate

©2025  Code Reveals Inc. All Rights Reserved.

Welcome Back!

Sign in to your account

Username or Email Address
Password

Lost your password?