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

An introduction to APIs

API is an acronym for Application Programming Interface. Let's start with first defining some basic terms: Browser: These are browsers. To visit any website on the internet, you need a browser. Server: Hmm, this is tough. In simple words, server is a computer. Yes, just like the laptop, or PC at your home. The only difference is that it does not have a screen. Of course, there are other differences in technical specifications, but at its core, the server is just, simply, a computer. That's it. So why is it called a server? Because it serves . When you go to a website like google.com , your computer connects to the internet and gets you your search result. But your computer's internet connection has to get that result from somewhere, right? If the google search result is giving you some answers, the answers have to come from somewhere. What is that place? The answer to that some place is: a server. When you click on the search button on google, or hit enter after typing, &q

Review: Nestjs - Finally a scalable way to build APIs

I have been thinking about this for a long time. There HAS to be a defined way to build APIs in a scalable way.  If you have used Node, Express, etc in your side projects, you might have felt that after a point in development, debugging truly becomes a pain. Sure, enterprise-level API codes are great. But a lot of times, these configurations are too much, and probably not even needed in other projects. To be honest, I haven't seen a lot of Open-Source API codes either to make a judgement on how experienced developers build their APIs. Anyway, I came across an amazing framework recently, and I think if you are coding a complex API, this should be your way to go. Nest.js Nest.js is a framework for building efficient, reliable and scalable server-side applications.  You essentially break your APIs into controllers, services, and modules, which allow you to modularize the smallest of functionalities in your endpoints or the API as a whole. Why is modularizing important? As I have talk