Javascript Interview cheat sheet

Javascript Interview cheat sheet

Table of contents

No heading

No headings in the article.

Table of content
1.Hoisting
2.Single thread
3.Call stack

Hoisting

JavaScript Hoisting refers to the process whereby the interpreter appears to move the declaration of functions, variables or classes to the top of their scope, prior to execution of the code.Hoisting in JavaScript is a behavior in which a function or a variable can be used before declaration.

Example

// using test before declaring
console.log(add);   // undefined
var add;

The above program works and the output will be undefined.

Variable Hoisting

Hoisting works with variables too, so you can use a variable in code before it is declared and/or initialized. In terms of variables and constants, keyword var is hoisted and let and const does not allow hoisting. Example

// program to display value
a = 5;
console.log(a);
var a; // 5

In the above example, variable a is used before declaring it. And the program works and displays the output 5.

Function Hoisting

A function can be called before declaring it. Example

// program to print the text
display();

function display() {
    console.log('My name is kavita');  //My name is kavita
}

Single- Thread

JavaScript Thread is single-threaded by default used to run a script in the web application, perform layout and garbage collection. Being single-threaded is to execute only a single set of instructions at any time in the process.

Example

console.log("one");
console.log("two");
console.log("three");
//one
//two
//three

Call stack The call stack is used by JavaScript to keep track of multiple function calls. We use call stack for memorizing which function is running right now.

Example

var n=2;
function cube(num ) {
var ans=num*num*num;
 return ans;
}
var cube2=cube(n);
var cube8=cube(8);