Skip to main content

Namaste JavaScript Quick Notes

Note: Akshay Saini's Namaste JavaScript is probably the best course for JavaScript developers out there. These are my personal notes that I made while watching the course; they serve more of as an online quick reference for my understanding and revision, and I hope it benefits anyone reading it too!

Everything in JS happens inside an Execution Context.

Before a JS code is run, memory is allocated and variables are set as undefined , and functions are set as their exact code in the scope within the Execution Context.

The global execution context hosts all the global variables and function definitions. An Execution Context has 2 components: Memory, that stores variables and functions; and Code, that reads and executes the code.

Call Stack maintains the order of execution contexts. Since JS is single threaded and asynchronous, at one point of time, only one function is executed which is at the top of the call stack. For each function, an execution context is created before executing it. 

Hoisting

Before a code is run, an execution context is created which allocates memory to variables and stores functions defined inside it. The variables are set as undefined. This is known as hoisting.

Lexical Environment of an Execution Context/a Function

It is the local memory along with the lexical environment of the parent.
function a () {
    // some variables
    function b() {
        // some variables
    }
    b()
}
b has an execution context that will contain variables of b inside the memory component of the execution context. However, it (memory component) will also contain a reference to the lexical environment of a, it's parent execution context (function). Practically, this makes sense since you can access variables defined in the scope of function a in function b

Lexical Scope

Since each execution context's memory will contain variables and functions defined in itself and a reference to the lexical environment of the parent, even if we try to access a variable that is defined in the global scope of the JS code, it becomes possible to do that. 
The JS Engine would scan a chain of lexical environments in an hierarchical fashion to find the variable that is being accessed. This is known as lexical scope.

let & const

let & const declarations are hoisted. But they are in the temporal dead zone.

Temporal dead zone - phase from hoisting to actual point of initialization. Variable accessed in this phase gives a reference error.
let and const variables are stored in a separate memory space, not in global execution context. But they are hoisted with an undefined value, and they cannot be accessed until they are initialized. 

'let' is more strict than 'var':
  • 'let' cannot access variable before its initialization. It gives a reference error.
  • 'let' cannot re-declare variables 

Comments

  1. As its name suggests, 3D Hubs is not so much its own printing service as it is a way to connect you with 3D printing services native to your area. The 3D Hubs network is CNC machining international and currently consists of shut to 5,one hundred manufacturing services across more than a hundred and forty international locations. 3D Printing is the method of changing a 3D Model right into a strong, three dimensional object.

    ReplyDelete

Post a Comment

Popular posts from this blog

"Hey Google" get me a new T-shirt

Everyone loves Google for its amazing technology and creative workspaces! Guess what? Google loves its developers as much as the world loves it too! And yeah, you don't need to be an amazing programmer to be a developer in Google's community. All you need to do is to spare 30 minutes, just once, and maybe have some creativity! That's it! Oh, and you should be really checking your mails periodically, although if you don't currently have this habit, your excitement would develop that for you. What do we want? So you arrived here to know about getting a T-shirt. Would you also like having a Google Home ? Yup, that is also something you could get through this. And of course, as I mentioned earlier, an entry to Google's Developers Community Program! There're a lot of perks for it but let's first talk business. What do we have to do? In a nutshell: Make an Action for Google Assistant .  But what's an Action ? Action is a feature, or a sub-applicat...

My JEE Story that taught me Life

It was May 2015. I'd come out of school and finished what looked like the final hurdle to life at that time - the Board Examinations! Now it was time to rock the world & achieve everything that I used to dream of as a kid. Time to think what's next. I was always ambitious and understood the close link between struggle and success, and I was ready to take trouble for it. There was just one problem though, I didn't know what field to struggle in. So after many advices and discussions, I found out about a tag called 'IIT'  and how it can differentiate a person from any other normal engineer. Great, I'll be an IITian, I exclaimed! So what do I need to do to get into IIT? You need to give an exam called the Joint Entrance Examination ( JEE) . "So that's what I'm going to do." I must say that today, when I look back, I feel extremely grateful for not being forced down a profession which my parents thought was better for me. I've seen ...