Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 1514

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 62k
Author
Asked: November 25, 20242024-11-25T08:53:08+00:00 2024-11-25T08:53:08+00:00

Understanding scope in JavaScript

  • 62k

In this article, we are going to talk about an important concept of JavaScript called scope. You will learn about how scope in JavaScript works, and global variables and how they relate to scope.

Let's dive in!

What is scope in JavaScript?

Scope simply means variable access, that is “What variable do I have access to when my code runs?”. By default in JavaScript,
we are in the root scope, alias the window object. Let's visualize what we are talking about here.

Assume we write and run a function in your browser console, call it func as shown below;

 function func() {   console.log('func'); } 
Enter fullscreen mode Exit fullscreen mode

…and then call the window object, by just typing window on the console, you see that your function func has been added to the window object. As shown in the image below:

Window Object
So, we see now that while working in the browser, the window is the root scope, the parent scope.

Now, what if we define a function func, then define a variable a inside it and try to console.log(a) outside the function, what do we get? Well, let's see for ourselves…

scope of variable a

Hmnnnn… so we got an error which says a is not defined, let's get a hang of what's happening here. Variable a is defined, but within the function func, right. Variable 'a' therefore is limited to that function's scope(within the curly braces of the function). That is, it cannot be used outside of that scope. The only way to correct the given error is to console.log(a) inside the function's scope.

a is logged on the console

We see from the illustration above that a is now logged on the console.

Global variables and Scope

Global variables are variables that are declared in the global scope. That is, they are declared in the root scope, which means they are present in the window object like we saw previously.

What does this mean?

This means that they can be accessed from anywhere in the JavaScript program, even if they were not declared in the scope of an entirely new function(a child scope).

Suppose we have a variable a declared outside of a function func, the variable a can be used as a variable, and even modified in a child scope function func.
Let's visualize what we have just learned.

Accessing global variables
We see how that the global variable can be accessed and used by child scopes in the above illustration.

Next, let's see how the global variable can be modified by child scopes.

Modifying global variables

We see how that variable a has been modified globally, not just in the function, after the function was called, and from that instant, a is equal to 13.

Conclusion

We do have many more scenarios of using parent scopes inside of child scopes, like functions inside of functions, but that is beyond the scope of this article.

Recap

In this article, you read about

  1. Root scope and how that by default we are in the root scope, the window object in JavaScript.

  2. How that declaring variables inside of a function is limited to that function.

  3. Global variables and how you can use them in child scopes.

If you have any questions, feel free to ask in the comments section. Until next time, XOXO.

beginnersjavascriptprogrammingwebdev
  • 0 0 Answers
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 2k
  • Popular
  • Answers
  • Author

    ES6 - A beginners guide - Template Literals

    • 0 Answers
  • Author

    Understanding Higher Order Functions in JavaScript.

    • 0 Answers
  • Author

    Build a custom video chat app with Daily and Vue.js

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.