Global Scope and Functions

Hi there,

I am currently doing JS Data Structures and Algorithms → Global Scope and Functions

It says:

“In JavaScript, scope refers to the visibility of variables. Variables which are defined outside of a function block have Global scope. This means, they can be seen everywhere in your JavaScript code.”

" Variables which are declared without the let or const keywords are automatically created in the global scope. This can create unintended consequences elsewhere in your code or when running a function again. You should always declare your variables with let or const ."

Then the task:

Using let or const, declare a global variable named myGlobal outside of any function. Initialize it with a value of 10.

Inside function fun1, assign 5 to oopsGlobal without using the var, let or const keywords.

The correct code is this:

// Declare the myGlobal variable below this line
const myGlobal = 10;

function fun1() {
  // Assign 5 to oopsGlobal here
  oopsGlobal = 5; 
}
// console output
myGlobal: 10 oopsGlobal: 5

Can someone, please:

  • first, explain this topic a little bit in a different way, somehow that has helped you to understand it better?;
  • second, explain what’s the purpose of this particular task?
  • third, are variables declared inside a function become Global or not? Or it depends on whether they are declared with or without let or const keywords?

Best regards!

Hey!

In the previous challenges, you were introduced to variables and in essence they allow you to store different types of values. You can think of them as same variables you’ve used in grade 8th or 9th level mathematics class.

To understand this concept lets use an example.

You are creating a complex program which requires a lot of values to be processed and In order to do that you need to use a lot of loops and conditional statements. You also need a lot of variables to store those values, process them and finally show the output to the users of your application.

In the above scenario, theres variables that you might want to use again and again (think of a timer in any game you’ve ever played ) for example:

let i = 0;

If (timer < 10){
 do something 
}

.... after a lot of code....

if (playerDead){
   reset timer
} 

.....

In the above example, you need this variable to be accessible in every function or block (a block is the code that you write between an opening and closing brace used commonly with functions, loops and conditionals{ … `}) that you might create in your program.

In contrast, Some of the values that will be used in it, might only be used inside of a block for example:

for (let i = 0; i < 10; i++){
    console.log(i);
}

In the above example, the variable i is only needed temporarily and would never be used again.

The challenge intends to teach you the difference between local variables (variables you only need in a certain block or a function) and global variables (variables you can access everywhere).

Basically when you create your variable using let or const inside of a block ( for loop, if/ else condition or a function) they will only be accessible inside of that block and when you use the same let and const to create a variable outside of a block, they are added to the global scope and can be accessed anywhere inside of that program.

To paraphrase the above line:
“If you don’t use the let or const keyword to define your variable (even though you should and not doing so could lead to unpredictable behavior), javascript would put them in the global scope and make it accessible everywhere”

The code example you provided is just a demonstration of the above concept.

Here’s a video if you want to learn more about it

Hope this helps ! :smile:

Let me know if you have any other questions!

2 Likes

Thank you

Now it’s getting clearer!

I will also watch the video

Best regards!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.