JAVASCRIPT interview question (Basic interview question)

  1. What are closures?

a) Function along with the outer environment together form a closure. Closures is a  combination of function and its lexical scope bundled together to form a closure.

b) each and every function of javascript having access to its outer lexical environment, that means access to the variable and function which is present in the environment of its parent so each and every function is access to them .

c) even when this function is executed in some other scope it still remembers its outer lexical environment where it was originally present in the code that is what closure is .


Example:- 

function outer() {

 var a = 10;

 function inner() {

   console.log(a);

 }

 inner();

}

outer();



  1. How the javascript working 

Everything in javascript happens inside the execution context having two components: the first memory component and second is code component

Memory components having every key and their value are stored inside it. Functions are also stored inside the memory component. Memory component is also known as variable component.

Code component is the place where code is executed this component is also known as execution component

Javascript is a single thread synchronous programming language. Js only execute one component at a time in specific order.


Global execution context :-

Memory component 

Code component 

Store key and value , also store variable a=10 . and also store function:{...}.

Each and every line of code is stored inside the code component.

Example:- 

function outer() {

 var a = 10;

 function inner() {

   console.log(a);

 }

 inner();

}

outer();



Memory component

Code component

Function outer() { …

Var a =10;

}

 All the functions and variables are stored inside the memory component.

Function outer(){

Var a = 10;

Function inner(){

console.log(a)

}

inner();

}

outer();

Each and every line of code are stored inside the code component.


  1. Call stack :- 

Call stack is the stack where all the global execution contexts are called .


  1. Hoisting :- 

In the hoisting we can use the variable before it has been declared 



console.log(x)

Function getName(){

console.log(‘hello world’)

}

getName()

Var x = 10



  1. Global Scope :-

Global scope is nothing but code of javascript which is not inside a function called global scope. 

If we create any variable and function inside the function so the variable is not a function scope or global variable. Else the variable is not inside the function so that is called global variable. 



Var a = 1 // this is global variable 

a()

b()

Function a(){

Var x = 10 // this is not a global variable

}


  1. Undefine , not define , null

Undefine means a variable has not been assigned the value.

Not defining a variable is not defined at a given point of time.

Null signifies no value and non-existence of value.


  1. Scope :-

Scoe is nothing but where you can access variable and function inside are code ,

Javascript has 3 types of scope .

  1. Block scope :- block scope is nothing but a variable inside { ..}. Block scope variables can not be accessible by the outside of scope. Let and const block scope variables.

Before ES6 javascript had only functions and Global scope.

  1. Function scope:- Each function creates a new block scope. It mens variables defined inside function scope are not accessible by the outside of function .let , var and const are function scope.

  2. Global scope:-  global scope is nothing but variables and functions declared globally are called global scope. This scope of variable can be accessed anywhere in code.


8) Lexical scope :- 

Wherever the execution context created a lexical environment is also created.

Lexical scope is a local memory along with there lexical memory of parent or lexical environment of parent.

Lexical is a term of sequence you can say c() function is lexically siting into the a() function.


function a(){

Var b = 10;

c()

Function c(){

console.log(b)

}

}

a();

console.log(b);


Here function c is lexically sitting into the function a and function a is dependent on global execution context.


9) scope chain :-


Scope chain is nothing but the chain of all this lexical environment. This chain of lexical environments is also known as scope chain.


10)  Let , var and const:-


Let:- this type of variable we cannot access before they initialized.

Let type of variable store in different memory locations inside the global declaration.

We cannot duplicate the let type of variable. Temporel-ded-zone is a time where the let and const type of variable can be hoisted.

You can declare the let type of variable in one place and initialize it in different places.


Const :- const having similar behavior like let but it is even more strict. It should  be initialized and declared together. 


Var:- this type of variable is not strict, we can hoist the variable , we can duplicate the variable . This type of variable is very much flexible.


11) datatype in javascript:- 

Javascript provides different types of data types to store different types of data. 

There are two types of data type provided by javascript: first is primitive and second is non primitive.

Primitive data types are having string, number , bullan , undefined and null type of data.

Non-primitive having object, array and regexp types of data .

 


12) function statement :-

Way to create a function is called a function statement. Function statement is nothing but a simple function.


function a(){

console.log(‘hello world’)

}

a()



13) function expression:-

In the function expression function is initialized as a variable, it is nothing but a simple function but it is created by assigning a variable .

The major difference between these two functions is function expression can not be hoisting but the function statement can be hoisting. 


Var b = function(){

console.log(‘hello world’)

}

b()


In function statement during hoisting phase a() is creating memory in global execution context and this function is assign to a but in the case of function expression 

b() function treated as a variable, it is assigned undefine initially until the code hits this line itself. 


14) function declaration :-

Function declaration and the function statement both are the same thing.


15) Anonymous Function:-

Anonymous function is nothing but a function without a name. Anonymous functions do not have their own identity. This will result in a syntax error. 

An anonymous function similarly looks like a function statement but it has no name a, but according to the acma script  specification function having its own name so that's why it is a invilad syntax.

So when you can't create a function like this, what is the use of an anonymous function? So this function is used in a place where functions are used as values .

You can use it to assign it to some variable like function expression. So the function expression is nothing but the anonymous function


16) Named function Expression:-

Named function expression is nothing but function expression having a name of it function also it sounds weird but is possible in javascript 

If you try to access the function outside of the function it gives a reference error , because this hello() function is assigned by the variable . 

So if you try to use the hello() function inside the function this does not give you an error because the hello() function is present locally in memory of the variable b not in global memory of execution context.


Var b = function hello(){

console.log(‘hello world’)

}

b()



17) Different between Parameter and argument:- 

The values which we pass inside the function are known as arguments .

And the label and variable are inside the round brackets are known as parameter



Var b = function hello(hello1, hello2){

console.log(‘hello world’)

}

b(1, 2)


So in this case hello1 and hello2 are parameters , 1 and 2 are arguments.

18) first class function:- 

The ability to use functions as values is known as a first class function. So the ability of a function to use those values can be passed as an argument to another function and can be returned from the function this is known as first class function.


19) arrow function:- 

Arrow functions are introduced in ES6. arrow function are shortest way to wright function in our function component. 


Befor:


Const hello(){

console.log(‘hello world’)

}


After:


Hello = () => {

console.log(‘hello’)

}


Post a Comment

0 Comments