These are the topics for week 3:
- Scope
- Global vs. local (function and block)
- Const and let
- Hoisting
- What is it
- When does it happen
- Why do we need to know it?
- Closures
- Execution context
- Defining a function within a function
- Thinking like a programmer II
One of the central concepts in programming is the idea of context: the meaning of any particular thing is only determined in relation to its direct surroundings. Let's take language for example. If I say the following sentence:
I never said she stole my money.
Reading this it's not obvious how to interpret what the situation is about. Depending on the emphasis of the words it could mean any of the following:
- I never said she stole my money.
- I never said she stole my money.
- I never said she stole my money.
- I never said she stole my money.
- I never said she stole my money.
- I never said she stole my money.
- I never said she stole my money.
It depends on context for me to know what really happened.
Let's draw the line to programming. Simply put, just like context gives meaning to a word, scope gives meaning to a variable/object.
The meaning is defined by whether or not the variable is accessible or not. If the variable is not within the scope
For further research, check out the following:
In any given application, there is usually one global scope. But there can be many local scopes
Quick refresher: Variables are used to store information to be referenced and manipulated in a computer program. They also provide a way of labeling data with a descriptive name, so our programs can be understood more clearly by the reader and ourselves. It is helpful to think of variables as containers that hold information. Their sole purpose is to label and store data in memory. This data can then be used throughout your program.
In JavaScript we used to define variables using the keyword var:
var myName = 'Mohammed';However
In programming, we generally want to keep things as simple as they can be. We even have a name for that: KISS (Keep it Simple, Stupid!)
If you look up the term "hoisting" in any dictionary, you'll find something like this:
"To raise [something] by means of ropes and pulleys"
A simple example of hoisting is the hoisting of a flag on a pole. You pull on the rope and slowly but surely the flag gets raised up.
In JavaScript, hoisting refers to
When you execute your JavaScript code, the interpreter goes through the code twice. The first time is called the compile-time, which is when your code is made ready to be executed: there will be safety checks, small optimizations and making sure the syntax is written correctly.
The second time is called run-time, which is where it actually executes your code by going through it line by line, doing the assignments, calling the functions, etc.
Hoisting happens during compile-time.
Simply put, a closure is a function that is defined inside another function.
In your programming journey you'll learn that many concepts overlap one another.
An execution context is
For further study please check the following resources:
- The Ultimate Guide to Execution Contexts, Hoisting, Scoping and Closures in JavaScript
- Understanding Closures
- Let's Learn JavaScript Closures
Becoming a good developer doesn't mean being good at any particular programming language: as a matter of fact, the language doesn't matter much.
There are only a handful of core concepts
This is the secret behind being a good developer: if you understand the concept, structure and principles of what makes a software program work, it doesn't matter in what way (syntax) it's written.
Are you finished with going through the materials? High five! If you feel ready to get practical, click here.