DEV Community

Sarman Khurshid Alam
Sarman Khurshid Alam

Posted on • Updated on

Important Questions and answer of javascript

1.What is javascript

javascript is a programming language for the web. It is a lightweight, interpreted, or just in time compiled, single-threaded programming language.JavaScript can update and change both HTML and CSS. Different browsers use different runtime environments to run javascript. One of the most popular run time engines is V8 which is used in chrome. Modified v8 engine is used to run javascript on the server-side.

2.Difference between call bind and apply

In javascript call bind and apply is used when we want to use a method that is defined in another object. Among the three methods bind is less efficient.
To understand this method properly let us consider a situation. Suppose there is an object called normalPerson which contains a method named chargeBill which takes two parameter. Now we create another object called heroPerson and we want to use the chargeBill method which is in the normalPerson. We can use this method by using bind, call, and apply.

Bind: const heroChargeBill = normalPerson.chargeBill(heroPerson)
heroCahargeBill(100,200);

Call:normalPerson.chargeBill(heroPerson,100,200)

Apply: Apply is similar to call but the main difference is function parameters are passed through an array.
normalPerson.chargeBill(heroPerson,[100,200])

3.Difference between '==' and '===' in javascript

In javascript == does not check the type for example,
if(2=='2'){
console.log('condition is true');
}
the above example will print condition is true, but if we use === then it will not execute the if block code because 2 is a number and '2' is a string.

4.What is the scope in javascript.

In javascript, if the variable is declared using let and const it can be accessed within this block. For example,
function add(a,b){
let result=a+b;
return result;
}
here the variable result is declared using let. So let we can only access the result inside add function, but if we want to access the result variable outside the add function it will throw an error.

5.What is variable hoisting

When a variable is declared using var keyword in javascript. Javascript moves the declaration to its parent level. For example,

function divisio(a,b){
if(b!==0){
var result = a/b;
return result;
}
return;
}
if we want to access the result outside the if block it will execute without error because it will set the result variable to the parent level but if result is declared using let or const it will throw an error. This is called variable hoisting.

6.Closure in javascript.

A closure is a feature of Javascript where inner function has access to the outer (enclosing) function’s variables—scope chain. This scope chains means inner function has:
access to its own scope (variables defined between its curly brackets and its parameters),
access to the outer function’s variables and parameters,
access to the global variables.
For example,
function counting()
{
let count=0;
return function(){
return count++;
}
}

let clock1=counting();
let clock2=counting();
console.log(clock1())//prints 1;
console.log(clock1())//prints 2;
console.log(clock2())//prints 1;
console.log(clock1())//prints 3;
for each function call each function store the reference of the variable.

7.Null vs undefined
let a;
let b=null;
console.log(a);//prints undefined
console.log(b);//prints null
we get undefined when a variable's value is not set and we get null if we set the value of a variable as null; We also get undefined if a function does not return an explicit value;

8.Event loop

The event loop is the secret behind JavaScript’s asynchronous programming. JS executes all operations on a single thread, but using a few smart data structures, it gives us the illusion of multi-threading.

The call stack is responsible for keeping track of all the operations in line to be executed. Whenever a function is finished, it is popped from the stack.

The event queue is responsible for sending new functions to the track for processing. It follows the queue data structure to maintain the correct sequence in which all operations should be sent for execution.

Whenever an async function is called, it is sent to a browser API. These are APIs built into the browser. Based on the command received from the call stack, the API starts its own single-threaded operation.

An example of this is the setTimeout method. When a setTimeout operation is processed in the stack, it is sent to the corresponding API which waits till the specified time to send this operation back in for processing.

Where does it send the operation? The event queue. Hence, we have a cyclic system for running async operations in JavaScript. The language itself is single-threaded, but the browser APIs act as separate threads.

The event loop facilitates this process; it constantly checks whether or not the call stack is empty. If it is empty, new functions are added from the event queue. If it is not, then the current function call is processed.

9.Event bubbling

When an event occurs the event goes down from the parent element to the smaller called target element where the event occurred. Then the event bubbles up from the target element executing all the events. For example,

if we click paragraph it execute the click event of the paragraph and also click event of the div.

10.Event deligation

If we have to add an event to each child element. It is efficient to set the event to the parent element. Though both do something but if we add too many events it consumes more memory.

Top comments (0)