⭐Top 5 important Interview questions of Javascript ⭐

Text

Q1: What is the nature of javascript?

Javascript is a single-threaded synchronous programming language. It means that it runs in a sequence such that when one line of code gets executed, then it moves to the next line. we can also say it executes one line of code at a time. single-threaded means it has only one call stack which works in FILO(first in last out ) fashion, so whatever is on the top is executed first and then pops out, then the next one is executed.

Q2: What is scope, functional, global, block scope, scope chaining, and lexical environment?

In simple words, the scope is where we can access the variables inside our code. In js, we have basically three types of scopes.

1. Global Scope

A variable in the global scope can be accessed anywhere inside the code. Ex:

var yourName= "Ram";

function details(input){
    console.log(input); //Ram
    console.log(yourName); // Ram
}

details(yourName);
console.log(yourName); // Ram

variables declared with var is the global scope.

2. Functional Scope(Local scope)

If any variable is declared inside that function, so it has the functional scope and is only accessible inside that function.


function function1(){
    const v1 = "I am inside function";
    var variable1 = "I am var inside function"
    console.log(v1); // I am inside function
    console.log(v2); // error
 }

 function function2(){
     const v2 = "I am inside function2"
     // console.log(v2);

 }
 function1();
 function2();
 console.log(variable1); // error

3. Block Scope

This scope is only for let and const. Variables declared with let and const inside block {} can only be accessed in the block, so that is block scope.

{
    let greeting="Have a good day";
    var response="Good Morning, you also have a great day"
}
console.log(response);//Good Morning, you also have a great day
console.log(greeting);//error

Scope Chaining

When js engine does not find the variable inside the current scope, it moves to the parent scope. It keeps on going to the outer scope(global scope) until the variable is found, if not in strict mode, it creates a variable in the global scope. This is called scope chaining.

Ex: let greet= "hi";

function greeting()}{
    let message= "nice day";
    console.log(message);// 


    console.log(greet);// "hi"
    myNumber=10;
    console.log(myNumber);//10 stored in global scope memory
}

Lexical Environment

function outer(){
    inner();
    function inner(){
        console.log(outest);
    }
}


outer();
console.log(outest); // 10

When this code will run, then inside the call stack three execution context is created.

  1. global execution context
  2. outer function execution context
  3. inner function execution context

When the function inner will run, it will try to search for the value of outest in its own local memory, if it does not find then it moves to the lexical environment of its parent i.e outer function, and search in its memory space if it does not find then moves to the lexical parent of outer scope or global scope then prints the value of outest.

c-- local memory+ lexical env of parent (outer fun) outer (local memory) + lexical env of parent (global space memory) at global space memory- got the value of b. this is called scope chaining and the place where variables are stored in our lexical environment.

Q3: What is asynchronous and synchronous in javascript?

In very easy words, future code will not work if current code is not executed, this is called synchronous which gets executed in sequential order. While asynchronous is a technique by which the program is started but will be get delayed in ending without affecting the other thing in the code. The program which is taking more time to execute will be handled asynchronously while normal code execution is synchronous (single-threaded).

Q4: Javascript is statically typed or dynamically typed language?

Javascript is a dynamically typed language because we don't need to declare the variable type while declaring or initializing that it is a string, boolean, number, etc. We can achieve this thing if we use typescript.

Q5: What is undefined and undeclared in Javascript?

Undeclared means he is not born and we are giving him an opportunity, undefined means he is born but not given responsibility. Undefined is a reserved keyword/ placeholder used by javascript when any variable is declared but not initialized. Undeclared means the variable does not exist in the code, and we are trying to access it.

Text

Hope you like the answers, if you want to add something, it will be great. Thanks for reading and catch you in the next 5 important questions of js. 😎😎😎