Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 

README.md

Reading Material JavaScript2 Week 3

Agenda

These are the topics for week 3:

  1. Scope
    • Global vs. local (function and block)
    • Const and let
  2. Hoisting
    • What is it
    • When does it happen
    • Why do we need to know it?
  3. Closures
    • Execution context
    • Defining a function within a function
  4. Thinking like a programmer II

1. Scope

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:

Global vs. local (function and block)

In any given application, there is usually one global scope. But there can be many local scopes

Const and let

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!)

2. Hoisting

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.

What is it

In JavaScript, hoisting refers to

When does it happen

Why do we need to know it?

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.

3. Closures

Simply put, a closure is a function that is defined inside another function.

Execution context

In your programming journey you'll learn that many concepts overlap one another.

An execution context is

Defining a function within a function

For further study please check the following resources:

4. Thinking like a programmer II

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.

Finished?

Are you finished with going through the materials? High five! If you feel ready to get practical, click here.