Research Scope - mikehov/Dating-app GitHub Wiki

Scope Research

Mike Hovenier | Tech-4

What do they mean by Scope

Scope is like a feature in Javascript. Scope is what is accessible of the functions, objects, variables in specific parts of your code. So what is visible for you variables and whatnot. You can see it as boxes, what box is in what box and when is it visible.

Scope is useful because you don't make everything accessible, this looks like a downside but it really isn't. You can use for example variable names multiple times instead of just once, so it really has its upsides. If you are scoping correctly, it will also improve you structure of your code what makes it more accessible, reusable, and readable for you and for other people. It can also help to prevent bugs.

There are two different type of scope:

  • Global scopes
  • Local scopes

All the functions inside the other functions are Local scopes. All the function that is getting owned by another function is the Global scope. See it as a street with houses with people that are living in those houses. Your document is the street, the Global scope are all the houses (functions for example) and the Local scope are the people that are living inside those houses. Global scoped information is accessible anywhere in the document, local scoped information isn't.

Also an important to know is there are three different types of variables:

  • var
  • let
  • const They almost doing everything the same, but not exactly. var is something you use if you want to make it more accessible. It's something you type globally. let and const work differently, they are not always accessible, they can only be used in the block code they are declared. The difference between let and const, you are not allowed to change the value of const but for let you are. So always use const, if you want to change the value use let, if you want to use it anywhere you want, you might want to be using var.

A few examples how Global scope and Local scopes work:

for(let x = 0; x < 5; x++) {
  console.log(x); // 1, 2, 3, 4
}

This will work, cause let is being used in it's localscope. So the console will count to 4. But what if you do the following code.

for(let x = 0; x < 5; x++) {
}

console.log(x); // ERROR

This will not work, because the console isn't locally called where the let is. But if you use var instead off let, this will work. This is because a var is able to look into scopes. But the console log will not show the same result, the console log will only show 5, this is because it can't see its counting.

How can I use Scope for my project?

Scope is something to keep in my mind, you want to keep your structure in the code and make information accessiable if you need to (so not always accesssiable). You can then keep the code short and using the same name over and over again if you want to. Try to use const if you can, let if you need to and var if you really need to.

Source

Ahmed, H. (2019, July 8). Understanding Scope in JavaScript. Retrieved May 29, 2020, from https://scotch.io/tutorials/understanding-scope-in-javascript#toc-what-is-scope-

freeCodeCamp.org. (2017a, February 9). Var vs Const vs Let (ES6) - Beau teaches JavaScript. Retrieved June 21, 2020, from https://www.youtube.com/watch?v=1mgLWu69ijU

mmtuts. (2018a, May 23). 16: JavaScript Scopes | Local Scope and Global Scope in JavaScript | JavaScript Tutorial | mmtuts [Video file]. YouTube. Retrieved from https://www.youtube.com/watch?v=hTU1OSbnov8

Web Dev Simplified. (2019, December 3). Learn Closures In 7 Minutes [Video file]. YouTube. Retrieved from https://www.youtube.com/watch?v=3a0I8ICR1Vg