JavaScript Interview Preparation Cheat Sheet

JavaScript Interview Preparation Cheat Sheet

1. SCOPE

The values and expressions are visible or can be referenced in any context of execution, that context of execution is called scope. Any value or expression can not be accessed if they are not declared in that scope. All the values and expressions declared in parent scope can be accessed in child scope but vice-versa is not true.

There are following kinds of scopes in JavaScript :-

  • Global Scope :- It is the default scope for all code in the JS file but outside any function.

  • Module Scope :- When the code is imported from any other JS file and can be accessed in current file then we can say that this code is in module scope.

  • Function Scope :- It means that variables and parameters defined in the function are visible everywhere within the function, but are not visible outside the function.

let myFunc() {
  let x = 10;
};

console.log(x);
// Reference Error
  • Block Scope :- It is defined with curly braces and separated by { and }.

Variables declared with let and const can have block scope. They can be accessed inside the function in which they are declared. But variables declared with var do not have block scope.

Lets understand this with examples :-

let x = 10;
{
  let x = 100;
  console.log(x);
  // 100
}

console.log(x);
// 10
var y = 1;
{
  var y = 10;
}

console.log(y);
// 10
  • Lexical Scope :- It is the ability of inner function to access the outer function in which it is defined.
let outer = () => {
    let x = 2;
    let inner = () => {
      console.log(x);
    };

    let secondInner = (fn) => {
      let x = 10;
      fn();
    };
    secondInner(inner);
    // 2
};

Outer is the lexical scope for inner function.

2.SINGLE THREAD

If you are learning JavaScript then you have heard that JavaScript is a Single Threaded language where single threaded means JS executes the code one statement at a time. Suppose, you are making a call to your friend, you can not talk to him until he picks the call up. It has only one call stack.

function first() {
   console.log("Hello !");
};

function second () {
    for(let i=0; i <= 1000000000000000; i++) {
    console.log(i);
    };
};

function third () {
    console.log("Nayak");
};

In above example, Our second () function will take more time to execute because it has to loop through huge numbers which means third() function has to wait for second() to complete its execution.

3. CALL STACK

The call stack is used by JavaScript or any other interpreter to keep track of multiple function calls. It is used to memorize which function is running right now. It is like real stack in data structures where elements can be pushed and popped and follows last in first out (LIFO) rule.

lets take an example :-

function funcOne() {
  console.log(" Hey Bro!");
};

function funcTwo() {
  console.log("How are you ?");
  funcOne();
};

funcTwo();
  • When the code loads in memory, global execution context gets pushed in memory.

global execution context.png

-funcTwo() gets called, the execution context of funcTwo gets pushed in the memory.

global execution context (1).png

  • Now funcOne() gets pushed in the memory.

global execution context (2).png

  • console.log will be pushed in the memory'

global execution context (3).png

  • When function execution completes and they return some values, they started popping out from the call stack and if call stack memory is full it will return "Stack Overflow"

4. HOISTING

Hoisting is the mechanism which moves all functions and variables to the top of their scope. However, variable assignments still happen where they originally were.

console.log(x);       // undefined
var x = 1;
console.log(x);      // 1

we will be using function hoisting mostly :-

myCity();

function myCity() {
  console.log("Jhansi");
};
  • Limitations of Hoisting :-
  1. Initializing a variable can not be Hoisted or Hoists declarations not Initialization. for example -
    var x = 10;
    alert(x + y);
    var y = 1;
    
    It will give output of NaN.Since we are initializing the value of y, Hoisting is happening and the value of y is undefined. JS will consider that y is not yet declared.
  1. Variables declared with let and const are also hoisted but, unlike var, are not initialized with the default value.
console.log(num);
let num = 6;

// Reference Error

#iwritecode @iwritecode

Did you find this article valuable?

Support Adesh Nayak by becoming a sponsor. Any amount is appreciated!