Namaste Javascript
Namaste Javascript
Namaste Javascript
JavaScript
Notes
-Dhruv Dalal
Namaste JavaScript
a : 10
function : { ….. }
1|Page
Namaste JavaScript
var n = 2;
function square(num) {
var ans = num * num;
return ans;
}
var square2 = square(n);
var square4 = square(4);
2|Page
Namaste JavaScript
n: 2 Memory code
square: {…} num : 2
square2 : return 4
ans : 4
undefined
square4 :
undefined
3|Page
Namaste JavaScript
n: 2 Memory code
square: {…} num : 2
square2 : 4 return 4
ans : 4
square4 : 16
Memory code
num : 4
return 16
ans : 16
.
→Call Stack: -
After executing program GEC will also be deleted from the stack and
call stack will be empty.
4|Page
Namaste JavaScript
5|Page
Namaste JavaScript
Now let's observe a different example and try to understand the output.
6|Page
Namaste JavaScript
var x = 1;
a();
b(); // we are calling the functions before defining them. This will
work properly, as seen in Hoisting.
console.log(x);
function a() {
var x = 10; // local scope because of separate execution
context
console.log(x);
}
function b() {
var x = 100;
console.log(x);
}
/*
output:
10
100
1
Code Flow in terms of Execution Context
*/
➢ The Global Execution Context (GEC) is created (the big box with
Memory and Code subparts). Also GEC is pushed into Call Stack
Call Stack : GEC
7|Page
Namaste JavaScript
8|Page
Namaste JavaScript
If we create any variable in the global scope, then the variables get
attached to the global object.
Ex.
var x = 10;
console.log(x); // 10
console.log(this.x); // 10
console.log(window.x); // 10
9|Page
Namaste JavaScript
console.log(x); // undefined
var x = 25;
console.log(x); // 25
console.log(a); // Uncaught ReferenceError: a is not defined
var a;
console.log(a);
a = 11;
/*output:
console.log(a);
undefined
a = "Hello";
11
console.log(a);
Hello
a = true;
true
console.log(a);
11.87
a = 11.87;
*/
console.log(a);
10 | P a g e
Namaste JavaScript
// CASE 1
function a() {
console.log(b); // 10
// Instead of printing undefined it prints 10, So somehow this a function
could access the variable b outside the function scope.
}
var b = 10;
a();
// CASE 2
function a() {
c();
function c() {
console.log(b); // 10
}
}
var b = 10;
a();
// CASE 3
function a() {
c();
function c() {
var b = 100;
console.log(b); // 100
}
}
var b = 10;
a();
11 | P a g e
Namaste JavaScript
// CASE 4
function a() {
var b = 10;
c();
function c() {
console.log(b); // 10
}
}
a();
console.log(b); // Error, Not Defined
Call Stack
Memory code
C()
Memory code
b: 10
a() c : {…}
Memory code
a : {…}
Global EC
12 | P a g e
Namaste JavaScript
function a() {
function c() {
// logic here
}
c(); // c is lexically inside a
} // a is lexically inside global execution
Global {
Outer {
Inner
}
}
// Inner is surrounded by lexical scope of Outer
13 | P a g e
Namaste JavaScript
It looks like let isn't hoisted, but it is hoisted ,yes let and const in also
hoisted let's understand.
Temporal Dead Zone: Time since when the let/const variable was
hoisted (assigned memory) until it is initialized some value.
14 | P a g e
Namaste JavaScript
let a = 10;
let a = 100; //this code is rejected upfront as SyntaxError. (duplicate
declaration)
------------------
let a = 10;
var a = 100; // this code also rejected upfront as SyntaxError. (can't
use same name in same scope)
→Let is a stricter version of var. Now, const is even stricter than let.
let a;
a = 10;
console.log(a) // 10. Note declaration and assigning of a is in different
lines.
------------------
const b;
b = 10;
console.log(b); //SyntaxError: Missing initializer in const declaration.
(This type of declaration won't work with const. const b = 10 only will
work)
------------------
const b = 100;
b = 1000;//this gives us TypeError: Assignment to constant variable.
This Error signifies that x has never been in the scope of the program.
This literally means that x was never defined/declared and is being tried
to be accesed.
15 | P a g e
Namaste JavaScript
16 | P a g e
Namaste JavaScript
→What is a Block?
Ans. Block aka compound statement is used to group JS statements
together into 1 group. We group them within {...}.
{
var a = 10;
let b = 20;
const c = 30;
// Here let and const are hoisted in Block scope,
// While, var is hoisted in Global scope.
}
{
var a = 10;
let b = 20;
const c = 30;
}
console.log(a); // 10
console.log(b); // Uncaught ReferenceError: b is not defined
* Reason?
17 | P a g e
Namaste JavaScript
→What is Shadowing?
var a = 100;
{
var a = 10; // same name as global var
let b = 20;
const c = 30;
console.log(a); // 10
console.log(b); // 20
console.log(c); // 30
}
console.log(a); // 10, instead of the 100 we were expecting. So block "a" modified
val of global "a" as well. In console, only b and c are in block space. a initially is in
global space(a = 100), and when a = 10 line is run, a is not created in block
space, but replaces 100 with 10 in global space itself.
So, If one has same named variable outside the block, the variable
inside the block shadows the outside variable. This happens only for
var.
Let's observe the behaviour in case of let and const and understand it's
reason.
let b = 100;
{
var a = 10;
let b = 20;
const c = 30;
console.log(b); // 20
}
console.log(b); // 100, Both b's are in separate spaces (one in Block(20)
and one in Script(another arbitrary mem space)(100)). Same is also true
for *const* declarations.
18 | P a g e
Namaste JavaScript
let a = 20;
{
var a = 20;
}
// Uncaught SyntaxError: Identifier 'a' has already been declared
We cannot shadow let with var. But it is valid to shadow a let using
a let. However, we can shadow var with let.
All scope rules that work in function are same in arrow functions too.
Since var is function scoped, it is not a problem with the code below.
let a = 20;
function x() {
var a = 20;
}
19 | P a g e
Namaste JavaScript
In above code, when y is returned, not only is the function returned but
the entire closure (fun y + its lexical scope) is returned and put
inside z. So when z is used somewhere else in program, it still
remembers var a inside x()
→Advantages of Closure:
Module Design Pattern
Currying
Memoize
Data hiding and encapsulation
setTimeouts etc.
→Disadvantages of Closure:
Over consumption of memory
Memory Leak, Freeze browser
20 | P a g e
Namaste JavaScript
function x() {
var i = 1;
setTimeout(function() {
console.log(i);
}, 3000);
console.log("Namaste Javascript");
}
x();
// Output:
// Namaste Javascript
// 1 // after waiting 3 seconds
21 | P a g e
Namaste JavaScript
→Reason?
This happens because of closures. When setTimeout stores the
function somewhere and attaches timer to it, the function
remembers its reference to i, not value of i. All 5 copies of function
point to same reference of i. JS stores these 5 functions, prints
string and then comes back to the functions. By then the timer has
run fully. And due to looping, the i value became 6. And when the
callback fun runs the variable i = 6. So same 6 is printed in each
log.
To avoid this, we can use let instead of var as let has Block scope.
For each iteration, the i is a new variable altogether (new copy of
i). Every time setTimeout is run, the inside function forms closure
with new variable i.
22 | P a g e
Namaste JavaScript
23 | P a g e
Namaste JavaScript
function outer() {
function inner() {
console.log(a);
}
var a = 10;
return inner;
}
outer()(); // 10
Ans: Yes, because inner function forms a closure with its outer
environment so sequence doesn't matter.
24 | P a g e
Namaste JavaScript
function outer(b) {
let a = 10;
function inner () {
console.log (a, b);
}
return inner;
}
outer("Hello There")(); // 10 "Hello There"
Ans: Inner function will now form closure and will have access to both a
and b.
function outest() {
var c = 20;
function outer(b) {
let a = 10;
function inner() {
console.log(a, c, b);
}
return inner;
}
return outer;
}
outest()("Hello There")(); // 10 20 "Hello There"
Ans: Yes, inner will have access to all its outer environment.
25 | P a g e
Namaste JavaScript
function outest() {
var c = 20;
function outer(b) {
let a = 10;
function inner() {
console.log(a, c, b);
}
return inner;
}
return outer;
}
Let a = 100;
outest()("Hello There")(); // 10 20 "Hello There"
Ans: Still the same output, the inner function will have reference to
inner a, so conflicting name won't matter here. If it wouldn't have found
a inside outer function then it would have gone more outer to find a and
thus have printed 100. So, it tries to resolve variable in scope chain and
if a wouldn't have been found it would have given reference error.
26 | P a g e
Namaste JavaScript
// without closures
var count = 0;
function increment(){
count++;
}
// in the above code, anyone can access count and change it.
------------------------------------------------------------------
------------------------------------------------------------------
*************************
// Above code is not good and scalable for say, when you plan to implement
decrement counter at a later stage.
// To address this issue, we use *constructors*
27 | P a g e
Namaste JavaScript
28 | P a g e
Namaste JavaScript
function a() {
var x = 0;
return function b() {
console.log(x);
}
}
29 | P a g e
Namaste JavaScript
30 | P a g e
Namaste JavaScript
function () {
31 | P a g e
Namaste JavaScript
var b = function(param1) {
console.log(param1); // prints " f() {} "
}
b(function(){});
32 | P a g e
Namaste JavaScript
setTimeout(function () {
console.log("timer");
}, 5000);
function x(y) {
console.log("x");
y();
}
x(function y() {
console.log("y");
});
// x y timer
In the call stack, first x and y are present. After code execution,
they go away and stack is empty. Then after 5 seconds (from
beginning) anonymous suddenly appear up in stack i.e.
setTimeout
All 3 functions are executed through call stack. If any operation
blocks the call stack, it’s called blocking the main thread.
Say if x() takes 30 sec to run, then JS has to wait for it to finish as
it has only 1 call stack/1 main thread. Never block main thread.
Always use async for functions that take time e.g., setTimeout
33 | P a g e
Namaste JavaScript
// index.html
<button id="clickMe">Click Me!</button>
// in index.js
document.getElementById("clickMe")
.addEventListener("click", function xyz(){
//when event click occurs, this callback function (xyz) is called into
callstack
console.log("Button clicked");
});
let count = 0;
document.getElementById("clickMe").addEventListener("click",
function xyz(){
console.log("Button clicked", ++count);
});
34 | P a g e
Namaste JavaScript
35 | P a g e
Namaste JavaScript
→Browser has JS Engine which has Call Stack which has Global
execution context, local execution context etc.
JS Engine Browser
Call Stack
https://
GEC
36 | P a g e
Namaste JavaScript
WebAPIs
None of the below are part of Javascript! These are extra
superpowers that browser has. Browser gives access to JS
callstack to use these powers.
Web APIs
Window
Call Stack
• setTimeout()
• DOM APIs
• fetch()
• LocalStorage
• console
• Location
console
>Hello World
https://www
→setTimeout(), DOM APIs, fetch(), localstorage, console (yes, even
console.log is not JS!!), location and so many more.
37 | P a g e
Namaste JavaScript
→As window is global obj, and all the above functions are present
in global object, we don't explicity write window but it is implied.
console.log("start");
setTimeout(function cb() {
console.log("timer");
}, 5000);
console.log("end");
// start end timer
First a GEC is created and put inside call stack.
console.log("Start"); // this calls the console web api (through window)
which in turn actually modifies values in console.
setTimeout(function cb() { //this calls the setTimeout web api which
gives access to timer feature. It stores the callback cb() and starts timer.
console.log("Callback");}, 5000);
console.log("End"); // calls console api and logs in console window. After
this GEC pops from call stack.
While all this is happening, the timer is constantly ticking. After it
becomes 0, the callback cb() has to run.
Now we need this cb to go into call stack. Only then will it be executed.
For this we need event loop and Callback queue.
38 | P a g e
Namaste JavaScript
→Eventloop has just one job to keep checking callback queue and if
found something push it to call stack and delete from callback queue.
39 | P a g e
Namaste JavaScript
Code Explaination:-
* Same steps for everything before fetch() in above code.
* fetch registers cbF into webapi environment along with existing cbT.
* cbT is waiting for 5000ms to end so that it can be put inside callback
queue. cbF is waiting for data to be returned from Netflix servers
gonna take 2 seconds.
* After this millions of lines of code is running, by the time millions line
of code will execute, 5 seconds has finished and now the timer has
expired and response from Netflix server is ready.
* Data back from cbF ready to be executed gets stored into something
called a Microtask Queue.
* Also after expiration of timer, cbT is ready to execute in Callback
Queue.
* Microtask Queue is exactly same as Callback Queue, but it has higher
priority. Functions in Microtask Queue are executed earlier than Callback
Queue.
* In console, first Start and End are printed in console. First cbF goes in
callstack and "CB Netflix" is printed. cbF popped from callstack. Next
cbT is removed from callback Queue, put in Call Stack, "CB Timeout" is
printed, and cbT removed from callstack.
* See below Image for more understanding
40 | P a g e
Namaste JavaScript
41 | P a g e
Namaste JavaScript
42 | P a g e
Namaste JavaScript
43 | P a g e
Namaste JavaScript
44 | P a g e
Namaste JavaScript
45 | P a g e
Namaste JavaScript
JS Engine
Code
JIT Compilation
Parsing
Interpreter
Compiler
Compilation Garbage
Collector
O
Execution Memory Heap CS
46 | P a g e
Namaste JavaScript
Companies use different JS engines and each try to make theirs the
best.
• v8 of Google has Interpreter called Ignition, a compiler called
Turbo Fan and garbage collector called Orinoco
• v8 architecture:
47 | P a g e
Namaste JavaScript
console.log("Start");
setTimeout(function cb() {
console.log("Callback");
}, 5000);
console.log("End");
// Millions of lines of code to execute
Reason?
• First GEC is created and pushed in callstack.
• Start is printed in console
• When setTimeout is seen, callback function is registered into
webapi's env. And timer is attached to it and started. callback
waits for its turn to be execeuted once timer expires. But JS waits
for none. Goes to next line.
• End is printed in console.
• After "End", we have 1 million lines of code that takes 10 sec(say)
to finish execution. So GEC won't pop out of stack. It runs all the
code for 10 sec.
• But in the background, the timer runs for 5s. While callstack runs
the 1M line of code, this timer has already expired and callback
fun has been pushed to Callback queue and waiting to pushed to
callstack to get executed.
• Event loop keeps checking if callstack is empty or not. But here
GEC is still in stack so cb can't be popped from callback Queue and
pushed to CallStack. Though setTimeout is only for 5s, it
48 | P a g e
Namaste JavaScript
The First rule of JavaScript: Do not block the main thread (as JS is a
single threaded(only 1 callstack) language).
// million
let startDate = new Date().getTime () ;
let endDate = startDate;
while (endDate < startDate + 10000) {
endDate = new Date().getTime () ;
}
console. log(while expires");
//Output:-
Start
End
While expires
Callback
49 | P a g e
Namaste JavaScript
console.log("Start");
setTimeout(function cb() {
console.log("Callback");
}, 0);
console.log("End");
// Even though timer = 0s, the cb() has to go through the queue.
Registers calback in webapi's env , moves to callback queue, and
execute once callstack is empty.
// O/p - Start End Callback
// This method of putting timer = 0, can be used to defer a less imp
function by a little so the more important function(here printing
"End") can take place
50 | P a g e
Namaste JavaScript
function x() {
console.log("Hi)";
};
function y(x) {
x();
};
y(); // Hi
// y is a higher order function
// x is a callback function
First Approach:
The above solution works perfectly fine but what if we have now
requirement to calculate array of circumference.
51 | P a g e
Namaste JavaScript
But over here we are violating some principle like DRY Principle, now
let’s observe the better approach.
52 | P a g e
Namaste JavaScript
Polyfill of map
→Over here calculate is nothing but polyfill of map function
→console.log(radiusArr.map(area)) == console.log(calculate(radiusArr,
area));
***************************************************
Let’s convert above calculate function as map function and try to use.
So,
Array.prototype.calculate = function(operation) {
const output = [];
for (let i = 0; i < this.length; i++) {
output.push(operation(this[i]));
}
return output;
}
console.log(radiusArr.calculate(area))
53 | P a g e
Namaste JavaScript
54 | P a g e
Namaste JavaScript
Filter function
Filter function is basically used to filter the value inside an array. The
arr.filter() method is used to create a new array from a given array
consisting of only those elements from the given array which satisfy a
condition set by the argument method.
Filter function creates an array and store only those values which
evaluates to true.
Reduce function
It is a function which take all the values of array and gives a single
output of it. It reduces the array to give a single output.
55 | P a g e
Namaste JavaScript
console.log(sumOfElem); // 17
// using reduce
const output = arr.reduce((acc, current) => {
(current > acc )?acc = current:null
return acc;
}, 0);
console.log(output); // 6
// acc is just a label which represent the accumulated value till now,
// so we can also label it as max in this case
const output = arr.reduce((max, current) => {
(current > max)?max= current:null
return max;
}, 0);
console.log(output); // 6
56 | P a g e
Namaste JavaScript
Tricky MAP
const users = [
{ firstName: "Alok", lastName: "Raj", age: 23 },
{ firstName: "Ashish", lastName: "Kumar", age: 29 },
{ firstName: "Ankit", lastName: "Roy", age: 29 },
{ firstName: "Pranav", lastName: "Mukherjee", age: 50 },
];
----------------------------------------------------------
// like: {29 : 2, 75 : 1, 50 : 1}
57 | P a g e
Namaste JavaScript
Function Chaining
// function chaining
const output = users.filter((user) => user.age <
30).map(user => user.firstName);
console.log(output); // ["Alok", "Ashish", "Ankit"]
58 | P a g e