# Javascript Interview Preparation Cheat Sheet

##### In this article, we will discuss some important topics for javascript interview preparation.

## Scope:

#### A scope can be defined as the region of the execution, a region where the expressions and values can be referenced. Scope simply means anything written inside { }. It always comes up with que i.e. "What it is that we are looking for things". Javascript has the following types of scope:

* Block scope
    
* Function scope
    
* Global scope
    
* Local scope
    
* Lexical scope
    
    ### Block scope:
    
    ##### Before ES6 (2015), JavaScript had only **Global Scope** and **Function Scop.**
    
    ES2015 introduced the let and const keywords that allow us to create block-scoped variables that can be accessed only inside those block.
    
    ##### Variables declared inside a { } block cannot be accessed from outside the block:
    
    ```javascript
    {
    let/const x = 3;
    }
    //x cannot be accessed,it will give you error.
    ```
    
    ##### Variables declared with the `var` keyword can not have block scope, which means variables declared inside a { } block can be accessed from outside the block.
    
    ```javascript
    {
    let x = 4;
    }
    //x can be accessed.
    ```
    
    ### Function scope:
    
    ##### Variables defined inside a function are not accessible (visible) from outside the function. Variables declared with var, let and const are similar when declared inside a function.
    
    ```javascript
    function myFunction() {
      var carName = "priti";   // Function Scope
    }
    ```
    
    ### Global scope:
    
    ##### Variables declared globally i.e outside any function have global scope. Variables declared with var, let and const are similar when declared inside a function.
    
    ```javascript
    var x = 2;
    let x = 2;
    const x = 2;
    ```
    
    ### Local scope:
    
    Variables declared within a javascript function, become local to the function.
    
    It is similar to function scope.
    

```javascript
function myName() {
  let car = "BMW";
}
```

### Lexical scope:

##### Lexical scope is the definition area of an expression. Lexical scope is also known as static scope. In other words, an item's lexical scope is the place in which the item got *created*. The place an item got invoked is not necessarily the item's lexical scope. Instead, an item's *definition space* is its lexical scope.

```javascript
let fruit = "apple";

function getMyFruit(){
    console.log(fruit);
}
function changeMyFruit(){
    let name = "banana";
    getMyFruit();//it will print apple
}
changeMyFruit ();
```

## Scope chain :

##### The scope chain in javascript is **lexically defined**, which means that we can see what the scope chain will be by looking at the code. At the top of the scope chain is the global scope, which is the window object in the browser. Besides the global scope, functions have their own scoping of variables.

```javascript
let a = 2;
function test (bar){
    return function innerTest(){
        let a = 4;
        console.log(a);
    }
}
const innerTest1 = test(2); //it will give 4
innerTest1();
```

## Call stack:

##### The JS call stack is a data structure that keeps track of information on the functions being called and executed. When a function is called from the script it gets added to the call stack. If another function is called from inside that function it gets added on top of the current function. This stacking of functions goes on until there is no function call left. Once the function is executed then it is popped out from the stack and execution continues from where it was left. It follows the LIFO, i.e.(Last In First Out)principle. It is used to maintain the `Order of execution` contexts.

#### Different names of call stack:

* Execution Context Stack
    
* Program Stack
    
* Control Stack
    
* Runtime Stack
    
* Machine Stack
    

```javascript
function name(){
    console.log("Hello!");
};
function nam1(){
    console.log("Hiii!");
    name();
    console.log("Namastey!");
};
function nam2(){
    console.log("Heyy!")
};

nam1();
nam2();
//It will print Hiii!
//Hello!
//Namastey!
//Heyy!
```

## Is javascript a single-threaded language?

##### Now we understand call stack, therefore we can say that javascript is a single-threaded language. Execution in javascript will go line-by-line. It means,it executes functions in order and must finish the current function before moving on to the next function. Simply means a statement has to wait for the earlier statement to get executed.

## Hoisting:

##### When you can access anything before it is declared. Hoisting is a way of how javascript behaves and it is a way how it collects references about declared variables.

```javascript
console.log(name);
sum();
var name = "priti";
function sum(){
    console.log("hello world");
} ;
//it will print undefined,hello world
```

#### Variable Hoisting:

##### As you see in the above ex. when the variable was first accessed before its declaration it will give an undefined value. This will work in the case of var only, not in the case of let and const. Hence when the variable is used before its declaration it has an undefined value and after its declaration, the value assigned is used.

#### Function Hoisting:

##### As you see in the above ex. when the function was accessed before its declaration, it will print the assigned value. In function hoisting, the entire function body is stored in the memory and hence we can invoke the function before the actual declaration.

Thank you for reading!!

Resources:

* MDN docs
    
* W3 schools
    
* Google
