Javascript Interview Preparing Cheatsheet

Javascript Interview Preparing Cheatsheet

Javascript is a programming language that is one of the core technologies of the WWW ie. World Wide Web along with HTML & CSS. As of 2022, around 98% of the websites uses the Javascript on the client side for web pages. Javascript is a high-level language that conforms to the ECMAScript Standard. It has application programming interfaces(APIs) for working with text, dates, regular expressions, standard data structures, and the Document Object Model (DOM).

So, this article I have very excited to share some of the interview questions that are related with Javascript. So, lets start with something knows as scope inside Javascript.

Types of Scope

  1. Block Scope
  2. Function Scope
  3. Global Scope

The scope is the anything which is written inside this {.....} curly bracket. If i declared anything just like { let a = 2 } ; and that i declare outside console.log(a) it will give me an ERROR because that a is simple doesn't have any value to pass in it.

Screenshot from 2022-09-11 17-25-28.png

Let us understand clearly with some another concept and example

1. Block Scope

Before ES6, Javascript had only Global scope and Function scope. ES6 introduced two important new Javascript keywords let and const. These two keywords provide Block Scope in Javascript. Variables which are declared inside a { } block cannot be accessed from outside the block.

let a = 8 ;
function blockScope( ){
console.log(a);
}

blockScope();

//  return 8

2. Function Scope

Javascript has function scope. Each function creates a new scope. Variable inside a function is not accessible (visible) from outside the function. Variable declared with var , let and const are quite similar when declared inside a function. These all are the example of Function Scope

function myFun(){
let fruits = "Mango"    // Function scope
}
function myFun(){
let fruits = "Banana"    // Function scope
}
function myFun(){
let fruits = "Apple"    // Function scope
}

3. Global Scope

When a variable declared outside a function, is known as GLOBAL.

let greeting = 'Hello Javascript!!' ;

function greet () {
console.log (greeting) ;
}

greet();

// return Hello Javascript!!

Single Thread

In normal languages the single thread means the computing the execution of an entire task from beginning to end without interruption. While Javascript is an asynchorous single threaded programming languages, here asynchorous means everything happens one by one line by line..

console.log (Apple) ;
console.log (Mango) ;
console.log (Banana) ;
console.log (Watermelon) ;
console.log (Pineapple) ;


// Apple
// Mango
// Banana
// Watermelon
// Pineapple
// Every line executed one-by-one....

Call Stack

A call stack is a mechanism for Javascript interpreter in a web browser to keep track of its place in a script that calls multiple function.

  • When a script calls a functions the interpreter adds to it call stack and then starts carrying out the function.
  • Any functions that are called by that function are added to the call stack further up, and run where their calls are reached.
  • When the current function is finished, the interpreter takes it off the stack and resumes execution where it left off in the last code listing.
  • If the stack takes up more space than it had assigned to it, it results in a "stack overflow" error.
function a(){
     function insidea(){
      return true;
   }
  insidea(); 
}
a();

From the above example, function a is created, and a function inside b that return the value true. Let's see the running process of this code below

Screenshot from 2022-09-11 19-15-07.png

Screenshot from 2022-09-11 19-25-03.png

    • js created a global executiob context,
    • After the function is called, a new execution context (second) is created
    • The code executes the second execution context is to call the function Inside a
    • Then it create a new execution content ie. (third),
    • Begin to destroy the global execution context , second and third in turn.

Hoisting

Javascript Hoisting refers to the process whereby the interpreter access to move the declaration of functions, variable or classes to the top of their scope. Hoisting allows all the functions to be safely used in code before they are declared.

console.log (number);   // return undefined

let number = 8;
  • Function Hoisting

    One of the biggest advantages of hoisting is you can use a function before it's declaring in a code.
animalsName ( "Lion");

function animalsName (name) {
    console.log (`${name} is the King of the Jungle`);

/* return
Lion is the King of the Jungle
*/

So, guys if you like my article do like, share and comment