Javascript Interview Cheatsheet

Javascript Interview Cheatsheet

·

4 min read

Hello there, Today I am presenting you some of the Javascript Interview questions, which are important, so let's start it.

Scope

The Scope is a code block in which values are accessible, let's understand it with an example.

function myFunction() {
    let myName = "abc";

    console.log(myName); // we can access it
}

see here, I created the function, and it has one variable myName, so the scope of the variable will be limited to the block, so if we want to access that variable outside of the block, we can't do that. it will throw ReferenceError: variable not defined

Global Scope

declaration without any curly braces/block, that values or expressions can be accessed anywhere in the file.

Let's understand it with an Example.

const country = "INDIA";

function myFunction() {
    let myName = "abc";

    console.log(myName); // ✅
    console.log(country); // ✅
}

myFunction();

console.log(country); // ✅
console.log(myName); // 🔴

See here, the variable country is defined in Global Scope, so we can access it anywhere in the file.

Scope Chaining

The inner function can access the outer function's scope, and if the inner function has the same named values, then the priority will start from the self function, then the outer function.

let a = 10;

function sum(b) { // <- can access outer scope
    return function(c) { // <- can access outer scope
        return function (d) { // <- can access outer scope
            return a + b + c + d;
        }
    }
}

console.log(sum(5)(4)(3));

so if we see here, the innermost function can access its own value variable d, and can access the outer function's value c, similarly for b, a.

Single Thread

In most JS interviews, the interviewer might ask you this question. Q: is Javascript Single threaded language? A: Yes, it's Single-Threaded and Synchronous language.

Javascript is made Single threaded intentionally. so some complicated scenarios don't happen as it is in Multi-Threaded Languages. A situation like a deadlock.

Many times like 99.99%, your operation on Single Thread will be fast enough. it's not a drawback.

So Javascript executes the operation one by one, that's how it works in a Single thread.

Call Stack

I will explain the call stack with an Example.

function a() {
    console.log("A");
}

function b() {
    console.log("B");

    function b1() {
        console.log("B1");
    }
    function b2() {
        console.log("B2");
    }

    b1();
    b2();
}

function c() {
    console.log("C");
}

a();
b();
c();

Just imagine what will be printed in the console.

It will be like this. A -> B -> B1 -> B2 -> C

so if you see it in the browser with the debugger, you will see a call stack. so let me explain how javascript executes the code.

the first javascript will scan the code and put any global variables, and function definition inside the scope. and after the scanning part, it will start to execute the code.

so in our example, the first execution is of function a() so the function a will be pushed to call stack. the function a will execute, and then function a will be popped out of the call stack.

now function b will be pushed to call stack, and it will execute function b, then it has two more functions to call so, b1, b1 will be pushed to the stack., then first b1 is executed and popped from the call stack. now, b2 will be pushed and executed, then b2 will be popped from the call stack. after that function, b will be popped out from the call stack.

now function c will be pushed into the call stack, and executed. then it will be popped out from the call stack.

Hoisting

Hoisting refers to a process where JS Interpreter moves out functions, variables, classes, etc to the scope. prior to their execution.

so during the scanning part, all variables are hoisted and made undefined and for function, the function code is entirely copied to scope, so we can execute it even before the declaration.

hoisting works differently for let and const variable declaration. in the case of var, the variable will be initialized with a default value.

but in the case of let and const, they are hoisted but not initialized, the initialized values are in the temporal dead zone before the execution starts. during execution, the values are assigned. so we can't access the variables defined with let and const before the initialization.


That's all for now if you enjoyed reading do 👍🏻, more such interview questions and concepts guides are coming, my tip: don't fall into just tips, understand the concept that I am sharing, or else the interviewer will confuse you with just names 😂