Javascript Interview Cheatsheet

Javascript Interview Cheatsheet

Hello Everyone, Today I will go through some important topic you might know, but it is always asked in interview. Let's Discuss them one by one...

Scope

The Scope is a part of the code block, it refers to the part of a program that is available where we can access a variable.

There are two types of scope.

  • Local Scope (Declared Inside a Block)
  • Global Scope (Declared Outside a Block)

Local Scope

The Local Scope occurs when you create a variable inside a function and the visibility & accessibility of the variable are only allowed within that function. Moreover, there are two types of Local Scope.

  • Block Scope
  • Function Scope

Block Scope

This Block scope restricts the variable that is declared inside a specific block, from access by the outside of the block. The area of the code which is within {curley brackets} is called a block. It is an area within conditions (if or switch) or loops (for and while).

Example

if (true) {
    // "if" block scope
    let print = 'Deep';
    console.log(print); // It prints 'Deep'
  }
//console.log(print); //  It shows ReferenceError: message is not defined

Output

Deep

Function Scope

In Function Scope Variables defined inside a function are not accessible (visible) from outside the function. Variables declared with var, let and const are quite similar when declared inside a function.

Example

let a = "Hello";

function Namaste() {
    let b = "World"
    console.log(a + " " + b);
}

Namaste();

Output

Hello World

Single Thread

In Javascript Single thread means the execution of instructions Processes in a single sequence. It means One command is processed at a particular time. We call javascript a single-threaded language because It has one call stack and one memory heap. It executes one code till the end without moving on to the next.

Example

const userOne = () => {
    console.log("hello i am one");
}
const usertwo = () => {
    setTimeout( () => {
        console.log("hello i am inside two");
    }, 2000)
    console.log("hello i am two");
}
const userthree = () => {
    console.log("hello i am three");
}

userOne();
usertwo();
userthree();

Output

hello i am one
hello i am two
hello i am three
hello i am inside two

Here in the above output, hello i am inside two takes 2second(2000 Miliseconds) more than the other outputs. As we know, it proved our single-threaded concept, which means once a function execution starts, it ends when the fulfilling process of execution.

Call Stack

Here, the name suggests Call Stack to keep track of multiple function calls in the stack data structure, where data can be pushed and popped followed by execution context.

Example

function calls_1(){
    console.log("Your Name is Hitesh");
}

function call_2(){
    calls_1();
    console.log("My Name is Deep");
}
call_2();

Output

Your Name is Hitesh
My Name is Deep

Hosting

In Hosting Javascript Engine Scans the whole element present inside a program before initializing it. It is a phenomenon in Javascript by which you can access the variables and functions even before initializing without any error.

Example-1

var x = 5;
function getName(){
    console.log("Learncodeonline")
}
getName();
console.log(x);

Output-1

Learncodeonline
5

Example-2

getName();
console.log(x);
var x = 5;
function getName(){
    console.log("learncodeonline")
}

Output-2

Learncodeonline
Undefind

In Both cases, we get the output, but in the second case we print the value even before initializing it so, it shows undefined which means the javascript engine scans the whole program before initializing and prints Learncodeonline after execution.