1. How Js Works?

    1. js is an synchronous(in a specific order → go to next line after executing the current one) single-threaded (execute only one command at a time) language
    2. in Js everything happens inside the “execution context” which has two parts → memory component and code component
    3. there are 2 phases → memory creation phase and code execution phase
  2. how js code is executed?

    1. when we execute a code, a global execution context is created
    2. now JS will scan the code in 2 phases
      1. memory creation phase
      2. code execution phase
    3. in memory creation phase:
      1. it scan the code line by line
      2. when it see a variable → it stores it in key value pair in the memory component of global execution context as undefined
      3. when it see a function → it stores the whole code in key value pair in the memory component
    4. In code execution phase:
      1. it again scan the code line by line
      2. Now when it see a variable n = 4 → it updates the value of n : undefined to n : 4 in the memory component
      3. when it see’s a function call → it create a new execution block for that function as a temporary/local one
      4. In this new execution block → it does similar things as in the GEC
      5. when a function block appears → it does nothing
      6. once function is done executing → it’s local execution context is deleted and values are returned to the global execution context
    5. after complete execution → whole GEC is deleted
  3. what is call stack?

    1. call stack maintains the order of execution for execution context
    2. it’s a stack that holds execution contexts → [GEC, E1, E2] ← top
  4. hoisting in js?

    1. hoisting → execessing functions and variables before initializing them
  5. Not defined vs undefined

    1. undefined → a keyword in JS it denotes that for this perticular variable some value is allocated but no value is defined
    2. not defined → this perticular variable is not defiend
  6. Types of functions in js

  7. window in js

    1. even if the JS file is completly empty → JS still create a global execution context and allocate memory
    2. JS engine also creates something called Window
    3. Window is an object with a lot of functions and methods which are created by JS engine and into the global space
    4. JS engine also creates a “This“for us → in the global level → this points to this window object
    5. Everytime we run JS code →
      1. object, global objects, which is window in case of browsers is created
      2. global execution context is created
      3. this variable in created
  8. the scope chain

    1. Scope: Scope means where you can access a specific variable or function in our code

      1. scope is directly dependent on the lexical environment
      2. Scope chain is the chain of lexical environment
    2. 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
      
  9. lexical environment:

    1. Whenever execution context is created → lexical environment is also created

    2. lexical environment is local memory along with the lexical environment of it’s parent

    3. 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();
      
  10. let, const and var:

  11. let and const are hoisted in JS

    1. 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;
      
    2. 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

  12. temporal dead zone:

    1. time since when this let variable was hoisted and till it is initialized with some value
    2. from being undefined to having some value is temporal dead zone
    3. when you try to access a varibale in temporal dead zone → it gives you Reference Error
  13. syntax error, reference error and type error

    1. Type Error → assignment of any other value to const give type error as value of const can’t change
    2. Syntax Error → when there is error in syntax
    3. Reference Error → when JS engine tries to find out perticular variable in memory space and it can not find it
  14. block scope and shadowing:

    1. block → compound statements : combining multiple statement is a block

      1. eg → { …. }
    2. block scope → what are the variable and functions we can access inside this block scope

      1. let, const b and c are hoisted in seperate block space, but a is hoisted in global space → thus a is accessible even outside of this block but b and c aren’t
      {
      	var a = 10;
      	let b = 20;
      	const c = 30;
      }
      
    3. Shadowing :

      1. 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

      2. 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 
        
      3. 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 
        
      4. illegal shadowing

        VALID
        var a = 10;
        {let a = 20}
        
        INVALID 
        let a = 20;
        {
        var a = 100
        }
        
  15. Closures, give example to demonstrate closures:

    1. CLOSURES:
      1. 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.

      2. a closure is a function bind together with it’s lexical environment

      3. a function along with it’s lexical scope is closure

      4. 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
        
      5. 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

      6. give an example of closures

        1. major part of closure is that even if it is executed outside of it’s lexical scope, it still remembers it’s lexical scope
        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
        
      7. Advantages of Closures:

      8. Disadvantages of closures:

  16. relation of scope chain and closures

  17. setTimeout:

  18. garbage collectors and how they are related to closures?

  19. First class function and anonymous function:

  20. difference between parameters and arguments