How Js Works?
how js code is executed?
what is call stack?
hoisting in js?
Not defined vs undefined
Types of functions in js
window in js
the scope chain
Scope: Scope means where you can access a specific variable or function in our code
the output of this code is 10 → as function a is lexically inside global space thus a has access to function and varibales of global space
function a(){
console.log(b);
}
var b = 10;
a();
// as in function a needs b which is not present thus it searches for b in the lexical environment of a which is global space
lexical environment:
Whenever execution context is created → lexical environment is also created
lexical environment is local memory along with the lexical environment of it’s parent
in this code → C is lexically inside A and A is lexically inside the global scope
function a() {
c();
function c(){
console.log(b);
}
}
var b = 10;
a();
let, const and var:
let and const are hoisted in JS
let and cosnt are in temporal dead zone as they are hoisted differently than var declarations
console.log(b); -> undefined
console.log(a); -> error can not access a before initilizing
let a = 10;
var b = 5;
let and cosnt are also allocated memory just like var, but var is attached to global object but let and const are not inside the global object, they are stored in different memory space
temporal dead zone:
syntax error, reference error and type error
block scope and shadowing:
block → compound statements : combining multiple statement is a block
block scope → what are the variable and functions we can access inside this block scope
{
var a = 10;
let b = 20;
const c = 30;
}
Shadowing :
Shadowing in JavaScript occurs when a variable declared in a local scope (e.g., inside a function or block) has the same name as a variable in an outer scope. The inner variable "shadows" the outer one, meaning the outer variable is temporarily inaccessible within that scope
here in the block, value 100 of a shadows the value 10 of a and also modifies the value of a because they are both pointing to the same variable
var a = 10;
{
var a = 100;
console.log(a); // prints 100
}
console.log(a) // prints 100
here 2 different value of b is printed → as let and const are not attached to global space and thus they are pointing to different memory locations
let b = 10;
{
let b = 20;
console.log(b) // prints 20
}
console.log(b) // prints 10
illegal shadowing
VALID
var a = 10;
{let a = 20}
INVALID
let a = 20;
{
var a = 100
}
Closures, give example to demonstrate closures:
A closure is the combination of a function bundled together (enclosed) with references to its surrounding state (the lexical environment). In other words, a closure gives a function access to its outer scope. In JavaScript, closures are created every time a function is created, at function creation time.
a closure is a function bind together with it’s lexical environment
a function along with it’s lexical scope is closure
when a function is returned, they still remember their lexical scope → they remember where they were actually present
function x(){
var a = 7;
function y(){
console.log(a);
}
return y; // not only the code was returned, the closure was returned
}
var z = x();
console.log(z) // this returns the whole code of z
z(); // this prints 7
Each function in JS has access to its outer lexical environment, access to variables and functions which are present in the environment of it’s parent, so even if the function is executed in some other scope other than it’s original one, it still remembers it’s outer lexical environment, i.e. it remembers where it was originally located in the code
give an example of closures
function outer(){
var a = 10;
function inner(){ // inner function along with it's lexical environment is known as closure
console.log(a);
}
return inner;
}
outer()(); // prints 10
Advantages of Closures:
Disadvantages of closures:
relation of scope chain and closures
setTimeout:
garbage collectors and how they are related to closures?
First class function and anonymous function:
difference between parameters and arguments