Namespacse

Hello, I have some questions regarding namespaces.

What exactly is a namespace?
What is scope pollution and what are some examples?

Thanks!

Hey!
A namespace is simply a technique employed to avoid collisions with other variables in the global namespace. For example, a variable may be used multiple times in code but in different contexts. Using namespaces in such a scenario will isolate these contexts so that the identifier can be used in different namespaces. One use of namespaces is so that you don’t accidentally re-assign a value to a variable when you didn’t need to! Namespaces aren’t that easy for me to explain, so attached are some links that helped me understand them when I was learning about them.
What is a namespace? - Quora
Namespaces Link 2

**Question 2: - **What is scope pollution and what are some examples?
Let’s have a look at what is said in the lesson:

  • Scope pollution is when we have too many global variables that exist in the global namespace, or when we reuse variables across different scopes. Scope pollution makes it difficult to keep track of our different variables and sets us up for potential accidents. For example, globally scoped variables can collide with other variables that are more locally scoped, causing unexpected behavior in our code.*

Now bare that in mind while I explain the code given in the lesson.

let num = 50;

const logNum = () => {
  num = 100; // Take note of this line of code
  console.log(num);
};

logNum(); // Prints 100
console.log(num); // Prints 100

Now we need to have a look at what happens:

  1. num is declared and given the value 50
  2. We create a new function called logNum
  3. Inside this function, we reassign the global variable num to 100
  4. Now when we invoke (another word for call) the function, we get 100
  5. We also get 100 when num is console logged.

There was no error and so as far as we are aware, everything is working perfectly. If we were to continue coding and we hadn’t called the function to test the value of num, we would have unknowingly used 100 as the value for num when we didn’t even realise it!

That is an example of scope pollution. We accidentally reassigned the value. To prevent this, we would use let. See the improved code below:

let num = 50;

const logNum = () => {
  let num = 100; // Take note of this line of code
  console.log(num);
};

logNum(); // Prints 100
console.log(num); // Prints 50

Now when we console.log(num) we get 50 because we used let when we reassigned the value for num in the function. Using let meant that the global variable will not be affected and num only equals 100 within that function. It’s a lot to process mentally but you’ll get it. I’ll provide some links so that you can do some further research if this still doesn’t make sense.
Scoping link 1
Scoping link 2

Hopefully this makes some sense. I’m positive others could explain this better than I have, but I hope this helps nevertheless.

Have a good day! :smiley:

In the second code, do we use “let” in order to create a new local variable of the same name in order to avoid re-assigning the value for the global “num” variable?

Yep you’re exactly right :slight_smile: