JavaScript Interview Cheatsheet – Essentials

intro.png Hi Friends, Looking for JavaScript Interview Cheatsheet 🙂 you are at the right place. It is commonly seen while interview process many of us know how to code, what should be the workflow of application, we have practised much, but still not able to crack the interview process because of our concept explaining ability.
While giving interview weather it’s online remotely or offline some common questions are being asked. Before going deep dive into big long cheat sheet’s available on the internet, just go through below common essentials to cover-up basic but most important frequently asked queries.
Unlike common Cheatsheets, I am not writing questions and answers here, because they are being asked in many ways, I am just explaining core concept, read this carefully so that you would be able to answer all common queries confidently.
First of all there is an important point you must keep in mind that, rather explaining too much technically if you would explain with some real life examples it will be more impressive, and shows that you have the core knowledge of the things.

Scope

It’s a very basic and common fact that in almost every programming language we use variable, functions and objects. Scope is the core concept of JavaScript which is about accessibility and availability of variable, functions and objects used in our program.
It defines how a variable or a function will be available to other variable or function. Security is the main advantage of such type of restrictions and permissions of accessibility. So, by using Scope in JavaScript we can control what should be available for all and how we can limit the access. It is also called Scope Chaining.
So, we have understood the concept of scope till here. Now, it is the time to nail down to types of Scope with some interesting examples.

Global Scope

It is clear with the name itself something which is accessible and has a wide area of scope should be kept in this category. So, in the context of JavaScript a variable which is accessible anywhere in the program.

//Global Scope
var siteName = "ineurone.ai";

function getSiteName(name) {
    console.log(siteName);
    //we can access this global variable inside function
}

getSiteName(); //Output : ineurone.ai
//we can also access this variable in global context
console.log(`Thanks to visit ${siteName}`); //Output : ineurone.ai

Above example will thow a reference error because at line number 5, we are trying to fetch the value of a variable named “siteIP” which is declared in inner function (child function).
Also, there is a small similar determination about the local scope i.e. function scope and block scope, it is simply mean that when we declare a variable inside block of code using let or const keyword it is said that it has a block scope, and when we have a scenario where a function has another function inside then the variable declared in parent function is accessible by its child functions, is called function scope.

{ //Block starts
    let user = "Amit";
    const password = "123456";
} //Block ends

console.log(user); //ReferenceError: user is not defined
console.log(password);

In above example scope of variables declared inside block is within the block only. It is not accessible outside of the block. It will throw a reference error when we try to access it from outside of the block. This was introduced with ES6 (ECMAScript 6, it is also known as ECMAScript 2015)

Lexical Scoping

When there is a function inside a function, we can call it parent and child. This child function can access the variable declared in parent function. Whenever such type of scenario occurs it is called “lexical scoping”. Inner function can access variable declared in outer function is also called function scope.

Single Threaded

Javascript is a single threaded programming language means it reads our program line by line and executes it. Before understanding single threaded execution process we need to understand two things one is “Call Stack” and another “Memory Heap”.
Call Stack : It is a place where our code is being placed and executed line by line.
Memory Heap : It’s a place where memory space is allocated to variables and functions used by our program.
Javascript has only one call stack to execute our program, that’s why we call it a single threaded. Javascript puts our line of codes one by one in call stack and executes it, and popes it out. It is synchronous in nature. But there is an amazing thing in javascript that async calls can be used in javascript.
We all know that mostly javascript code runs inside browser and browsers have event loops, callback queue and webAPI’s that is also used by javascript to run the program. This is not the part of javascript but it helps to run it.

Hoisting

In Javascript variable, functions and classes can be accessed or called before assigning values or declaration. Actually, Javascript only hoist the declarations not initializes it. When our program starts execution Javascript allocates memory for all variables, functions and classes. Suppose we have declared a variable without assigning value to it, it will be kept in the memory as undefined. Let’s consider below examples:

Variable hoisting

While assigning variable using “var” it is treated as a global variable by default and kept in the memory as undefined. When our program starts it is kept in global execution context as undefined.

console.log(username); // 👉 undefined

var username = "Kirit";

Using Functions

When using function javascript keep functions with declarations in the heap memory when execution starts. That’s why we can call function in javascript before defining it.

console.log(sum(5, 6));

function sum(a, b){
    return a + b //👉 11
}

Hope, above interview essential will help you to crack the interview.... Best of Luck ✌

#iwritecode