Javascript Interview Questions
Javascript Interview Questions
1. What is js
JS is single threaded programming language basically for interactive and dynamic
content. It run each and every line one by one. Used in both client and server side.
Js modifies the code in dom
2. DOM
Dom represents web pages as tree like structure
html allows javascript to dynamically render the content and structure of a web page.
3. Event loop in js
Functions in JavaScript are executed in a synchronous manner, one after the
other.
To perform asynchronous operations without freezing the application
Item is added to the call stack and executed
The eventloop detects that the call stack is empty so it takes the callback from
the queue and pushes it to the call stack
4. Scope
Block Scope: reference error block outside
Function Scope:
5. Event delegation
Handling events on parent elements for multiple child elements. Instead of
adding an event listener to each and every child, add the event listener to the
parent. Efficiency
6. Event Bubbling
Event bubbling is the process when a mouse click or keypress happens on div element
in a webpage and then moves up through its parent elements. Parent to root.
To avoid event bubbling use event.stoppropagation.
7. Event capturing
Event capturing is the process when a mouse click the browser starts from top of dom
tree and move downwards to the target element.
8. Diff b/w DOM and Virtual DOM
DOM Virtual DOM
Refresh whole layout Creates dom copy at backside and virtual dom at
front shows the result
9. Hoisting in javascript
Access the variables and function even before initialization without any error.
10. How Hoisting works
Memory is allocated for each and every variable and function before the code start
executing. Memory allocate as undefined.
Global execution context is created and runs the program and context is deleted.
Hoisting occurs only in var. Let and const hoisting can be in temporal dead zone.
11. Function naming
● Named Function
● Anonymous function
● Arrow function
● Function expression
12. Return statement
Return statement stops the execution of a function and returns a value.
Makes the function reusable we use this in multiple
13. Not Defined and undefined
Not Defined undefined
Shallow copy creates new object, but it does Deep copy creates entirely separate copy
not create copies of nested object. object and nested objects
Changes in nested object will affect original.
…… json.parse,json.stringify
Spread operator, object.assign
check the two variables check the two variables and their data types
20. Inheritance:
Inheritance allows us to create child objects share common features from parent object.
Inherit properties from other object
21. Promise:
Promise represents the eventual completion or failure of an asynchronous operation.
It can be handled by then and catch then for resolved and catch for rejection
22. async/await:
Async is a keyword used to create async function and await ia also a keyword that
cannot be accessed without the async keyword await act like suspending the code
within the function.
23. Fetch Api
Fetch api is making http requests to servers, fetching data asynchronously. alternate to
xmlhttprequest(XHR)
24. Axios
Axios is js library used for making http request in web browsers. Support features like
interceptors, promise based requests, automatic transformation of request and
response data.
25. Interceptors
Interceptors act as middleware functions that intercept HTTP requests or responses
before they reach your application or server. They provide a way to inject custom logic
at various stages of the HTTP request/response lifecycle.
26. Closures:
A function along with its lexical scope, hierarchy of the scope keep maintaining it.
settimeout(),currying, iterators,
27. setTimeout
Execute a function in a specified amount of time i.e., This function calls function after
duration milliseconds from now. This goes for one execution.
Syntax:
setTimeout(function, delay)
setTimeout(alert(“Hello”), 1000);
28. setinterval
Repeatedly execute function at specified intervals. This function calls function after
every duration milliseconds. This goes for unlimited times.
Syntax:
setInterval(function, delay)
setInterval(alert(“Hello”), 1000);
29. diff b/w promise and callback
Callback Promise
Pass 2 parameters 1 for result and for error Built in error handling then and catch for
result and error
29. Memoization
Technique to make function faster by remembering the previous results.
Stores the results and return them
Instead of recalculating every time a function is called with same inputs.
Tips:
● Try to create function in variables eg: let ram = func(){}
● Use eg: log(“name:” name + age) => log(`name: ${name} ${age})
let carfun = function(name,age){
console.log("name:" + name , "age:" + age);
console.log(`name: ${name}, age: ${age}`);
return `name: ${name}, age: ${age}`
}
let result = carfun("Ramya",45)
console.log(result);