Scope: What does it exactly mean?

Scope: What does it exactly mean?

Hello everyone,

This is Sougandika. Do you want to know what is scope? Be with me till the end and I am sure that this will get into your head very clearly.

Let's start. Where you can access a specific variable or function in a code is nothing but a scope.

For example

1 function x(){
2 console.log(a);
3 }
4 var a=10;
5 a();

When I try to do console log then javascript engine tries to search inside local memory space which means inside the execution context of x. It is not available now as nowhere we initialize 'a' inside x. what will be the output ? Will it print undefined or not defined or something else. Let's see

output:

> 10

Yes in this case output will be 10. This means somehow it is able to access 'a' which is outside x. If we try to make it little more complex. For example

6 function x(){
7   y()
8  function y(){
9  console.log(a);
10   }
11 }
12 var a=10;
13 x();

Now, what will be the output? Will it be undefined or null or 10 or something else.

output
> 10

This means it can access 'a' which is inside a function, which is inside another function, which is in global scope.

For suppose, consider variable a is inside function x,

14 function x(){
15 var a=10;
16 y();  
17 function y(){
18    console.log(a);
19   }
20 }
21 x();

Now, what will be the output? Give it a guess.

output
> 10

But if we try to access a outside the function, how does it works?

No alt text provided for this image

It means that when we try to access 'a' outside the function, it is showing an error as a is not defined.

Here in the example, what is the scope of variable a? It means where I can access the variable a. In another way, we can say like " Is a inside the scope of function y?" It means "Can we access a inside function y?"

This is all about scope. Hope you got what scope is. That's all in this article. See you in my next article.

Thank you for your valuable time,

Sougandika L

要查看或添加评论,请登录

Sougandika L的更多文章

  • Lifecycle Methods in Functional components

    Lifecycle Methods in Functional components

    Lifecycle refers to the series of phases that a React component goes through from its creation, rendering and eventual…

  • Are you excited to know about Event Flow in Javascript ??

    Are you excited to know about Event Flow in Javascript ??

    Order in which event triggered on DOM nodes is called event flow. There are 2 types of events: 1.

  • Angular 18 feature - Zoneless Change Detection

    Angular 18 feature - Zoneless Change Detection

    One of the most significant changes is the introduction of zoneless change detection. Reason : To avoid unnecessary…

  • Difference between Subject and Observable

    Difference between Subject and Observable

    1.Subject can be provider as well as consumer.

  • Why do we need package.json

    Why do we need package.json

    Most of us are concerned or focused on the place where we write our code, whether it might be styling (css) or…

  • SIGNAL in Angular

    SIGNAL in Angular

    With Change detection, incase of any change in one of the components, it check for all the components and then render…

    1 条评论
  • Creating Reusable Components in Angular

    Creating Reusable Components in Angular

    Lets start with a basic button Here in the image we could see 3 different buttons with different names, different…

    3 条评论
  • Reactive forms in Angular

    Reactive forms in Angular

    Angular provides 2 ways to work with forms – 1. Template driven forms 2.

  • Angular -Download Data in Excel format

    Angular -Download Data in Excel format

    Hello everyone, One of the most common usecases now a days is printing the data. In this article we will be…

  • Why Array ? Its declaration in Javascript

    Why Array ? Its declaration in Javascript

    Array is a special variable which holds more than one value at a time. It stores the homogenous elements.

社区洞察

其他会员也浏览了