JavaScript interview cheat sheet

JavaScript interview cheat sheet

why we need JavaScript?

  • JavaScript is a very useful and necessary programming language for any web developer as most of the web works on js web stack like React.js,Angular.js,Vue.js for frontend and Node.js for backend is a very popular choice for Today modern web application so understanding the fundamentals concepts are very much necessary for making good making web application and making them optimized with the proper use of javascript

What is Scope?

  • The scope is the current context of execution in which values and expressions are "visible" or can be referenced.
  1. Global Scope

any variable that is declared in the global context generally belongs to the Global Scope and the function is available in the whole function body and can be changed or accessed from any location

let sayHi="Hi";
function inner(){
console.log(sayHi); //Hi
}
inner();
console.log(sayHi); //Hi
  1. Function Scope

variables declared in a function will exclusively stay within the function and cannot be accessed from outside the function or within other functions.

function example1() {
  const a= 10;
  console.log(`inside example1 ${a}`);
}
function example2(){
  console.log(`inside example2 ${a}`); // ReferenceError: a is not defined

}
example1();
  console.log(`outside example1 ${a}`); // ReferenceError: a is not defined
  1. Block Scope

any variable declared in a block is called a block scope a block generally means a pair of parenthesis like {}. so if any variable is declared in a block that also can't be accessed outside.

{
  var a = 1;
  let b=2;
  const c=3; 
console.log (b) // 2
}

console.log(a); // 1
console.log(b) //ReferenceError
console.log(c)//ReferanceError

what is call-stack

A call stack is a mechanism for an interpreter (like the JavaScript interpreter in a web browser) to keep track of its place in a script that calls multiple functions — what function is currently being run and what functions are called from within that function, etc.

When a script calls a function, the interpreter adds it to the 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 greeting() {
  sayHi();
}
function sayHi() {
  return "Hi!";
}

// Invoke the `greeting` function
greeting();

Single thread

JavaScript is known as a single-threaded language means it has only one single call stack for the execution of the program.

  • Since Javascript is a single-thread language it is synchronous.

  • Javascript is called a single-threaded language because while running the code on the single thread it is easy to implement.

  • In synchronous calls, each work is done line by line which means it will not move to the next task until the present task completion irrespective of time and memory. Hence there will be a wastage of time and memory.

  • The above scenario can be overcome by using asynchronous calls where one task will not wait till another task to complete, and both the tasks run parallelly.

Hoisting in JavaScript

Hoisting is a concept in JavaScript where we will be access to the values of variables and functions even before assigning.

  1. Function Hoisting

By using the Function hoisting we will be able to use the function without declaring it.

firstName('Hemanth');

function firstName(name) {
    console.log(`The first name of the user is  ${name} `);
}

// output is The first name of the user is Hemanth

2 Var Hoisting

In var we first declare and then initialize the value. The default initialization is undefined.

console.log(name); // Returns 'undefined' from hoisted var declaration (not Hemanth)
var name; // Declaration
name = 'Hemanth'; // Initialization
console.log(name); // Returns Hemanth after the line with initialization is executed.
  1. Let and Const Hoistings

Variables with let and const can also be hoisted, but there will be no default initialization like var key word. An exception will be thrown if a variable declared with let or const is read before it is initialized.

console.log(name); // Throws ReferenceError exception as the variable value is uninitialized
let name = 'Hemanth'; // Initialization