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

How Kafka replaced Zookeeper with the (K)Raft algorithm?

Back in 2021, when I first came across Kafka, I remember the DevOps engineer in my team using terms like Zookeeper, broker configs, etc on our team standup calls. I remember not caring about those terms, and simply focusing on learning about the producer, partitions, topics and consumer groups, and how they could be used in the product my team was developing. While platforms like Kafka were built to abstract certain aspects of distributed computing (replication, consistency) while storing & processing logs, it's a pity how so many of us miss out on knowing the amazing engineering that went behind to build the different parts of a platform such as this one. 4 years later, I'm hungry enough to reverse engineer one of my favourite distributed platforms - Kafka! What did the Zookeeper do? To quote the 1st  Kafka paper from 2011, Kafka uses Zookeeper for the following tasks:  (1) detecting the addition and the removal of brokers and consumers,  (2) triggering a rebalance ...

i3wm essentials - I (Brightness)

So you have started using i3 and somehow managed to open your browser and almost resumed your normal work.  But wait, the brightness is too much isn't it? Or is it too low? The mousepad used to work fine, but now all of a sudden tapping does not equal click?!  Don't worry.  This blog series will tell you all about the essential setup commands and common shortcuts that I use to navigate my work in i3, and how you can too. Changing the brightness So you just started i3 and you just can't take this brightness setting. You go for your function keys, and damn! They aren't working. Quick fix: Run the following command if you need to change the brightness ASAP. xrandr -q | grep ' connected' | head -n 1 | cut -d ' ' -f1 This will give an ouput that's the name of your monitor.  Use that monitor name here and change the values of brightness to suit your needs. xrandr --output <monitor-name> --brightness 0.7 Now that your eyes are comfortable, let me show...