JavaScript Interview Questions
JavaScript Interview Questions
JAVASCRIPT
INTERVIEW
QUESTIONS
With Example Answers
TOP 50 JAVASCRIPT INTERVIEW QUESTIONS WITH
EXAMPLE ANSWERS
graphicodar
JAVASCRIPT FUNDAMENTALS
1. What is JavaScript?
A high-level, interpreted programming language
called JavaScript makes it possible to create
interactive web pages and online apps with dynamic
functionality. Commonly referred to as the universal
language, Javascript is primarily used by developers
for front-end and back-end work.
graphicodar
3. What is hoisting in JavaScript?
Hoisting is a JavaScript concept that refers to the
process of moving declarations to the top of their
scope. This means that variables and functions can
be used before they are declared, as long as they are
declared before they are used in a function.
hoistedFunction();
var x;
console.log(x); // Output : undefined
var y = null;
console.log(y); // Output : null
graphicodar
5. Why do we use the word “debugger” in JavaScript?
The word “debugger” is used in JavaScript to refer to
a tool that can be used to step through JavaScript
code line by line. This can be helpful for debugging
JavaScript code, which is the process of finding and
fixing errors in JavaScript code.
var x = 10;
debugger;
x = x + 1;
debugger;
console.log(x);
graphicodar
6. What is the purpose of the “this” keyword in
JavaScript?
The this keyword refers to the object that is
executing the current function or method.
const person ={
name : “Jonh”,
greet : funtion(){
console.log(“Hello, “ + this.name);
}
}
{
let x = 10;
console.log(x); // Output : 10
}
graphicodar
9. What are closures in JavaScript?
Closures (closureFn) are functions that have access
to variables from an outer function even after the
outer function has finished executing. They
“remember” the environment in which they were
created.
function outer(){
var outerVar = “Hello”;
function inner(){
console.log(outerVar):
}
return inner;
}
var clouserFn = outer();
clouserFn(); //Output : Hello
graphicodar
11. What is the difference between “let”, “const”, and
“var”?
let and const were introduced in ES6 and have block
scope. let is reassignable, and const is non-
reassignable. var is function-scoped and can be
redeclared and reassigned throughout the function.
let x = 5;
x = 10;
console.log(x); //Output : 10
const y = 5;
y = 10; //Error: Assignment to constant variable
console.log(y);
var x = 5;
var x = 10;
console.log(x); //Output : 10
graphicodar
For example, when you combine the number 5 with
the string '10' using the addition operator, the result
is the string '510'. This occurs because JavaScript
will implicitly convert the number 5 to a string
following the priority of coercion, and then
concatenate it to the string '10'.
var x = 5;
var y = “10";
console.log(x + y); //Output : “510”
function Person(name){
this.name = name;
}
Person.prototype.greet : function(){
console.log(“Hello, ” + this.name);
}
graphicodar
14. What is the output of the following code?
console.log(3 + 2 + “7”);
// Using Object.assign()
const obj2 = Object.assign({},obj1);
graphicodar
INTERMEDIATE CONCEPTS
function multiplybyTwo(num){
num * 2;
}
function applyOperation(num, operation){
return operation(num);
}
const result = applyOperation(5, multiplybyTwo);
console.log(result); //Output: 10
graphicodar
const person ={
name : “Jonh”,
greet : funtion(){
console.log(“Hello, “ + this.name);
}
}
// Function Declaration
function multiply(a, b){
return a * b;
}
// Function Expression
const add = function(a, b){
return a + b;
}
graphicodar
19. What are the different types of errors in JavaScript?
JavaScript can throw a variety of errors, including:
Syntax errors: These errors occur when the
JavaScript code is not syntactically correct.
Runtime errors: These errors occur when the
JavaScript code is executed and there is a
problem.
Logical errors: These errors occur when the
JavaScript code does not do what it is supposed
to do.
graphicodar
function factorial(n){
if(n === 0){
return 1;
}else{
return n * factorial(n - 1);
}
}
function factorial(n){
if(factorialCache[n] !== undefined){
return factorialCache[n];
}else{
factorialCache[n] = n * factorial(n - 1);
return factorialCache[n];
}
}
graphicodar
21. What is recursion in JavaScript?
Recursion is a programming technique that allows a
function to call itself. Recursion can be used to solve
a variety of problems, such as finding the factorial of
a number or calculating the Fibonacci sequence.
function factorial(n){
if(n === 0){
return 1;
}else{
return n * factorial(n - 1);
}
}
graphicodar
function Person(name, age){
this.name = name;
this. age = age;
}
function factorial(n){
if(n === 0){
return 1;
}else{
return n * factorial(n - 1);
}
}
graphicodar
var factorial = function(n){
if(n === 0){
return 1;
}else{
return n * factorial(n - 1);
}
}
function fetchData(callback){
setTimeOut( functio(){
const data = “Some Data”;
callback(data);
}, 2000);
}
function processData(data){
console.log(“Data Received: ” + data);
}
fetchData(processData); //Output: after 2
seconds: Data Received: Some Data
graphicodar
25. What are promises in JavaScript?
function fetchData(){
return new Promise(function(resolve, reject ){
setTimeOut( functio(){
const data = “Some Data”;
callback(data);
}, 2000);
}
}
fetchData()
.then(function(data){
console.log(“Data Received: ” + data);
})
.catch(function(error)){
console.log(“Error Occured: ” + error);
});
)
graphicodar
26. What is the difference between synchronous and
asynchronous programming?
In synchronous programming, the program
execution occurs sequentially, and each statement
blocks the execution until it is completed. In
asynchronous programming, multiple tasks can be
executed concurrently, and the program doesn’t
wait for a task to finish before moving to the next
one.
Synchronous coding example:
console.log(“Statement 1”);
console.log(“Statement 2”);
console.log(“Statement 3”);
console.log(“Statement 1”);
setTimeOut( functio(){
console.log(“Statement 2”);
}, 2000);
console.log(“Statement 3”);
graphicodar
try{
throw new Error(“Something Went Wrong”);
} catch(error){
console.log(“Error Occured : ” + error.message);
}
Example:
<div id=”parent”>
<div id=”Child”>Click Me!</div>
</div>
graphicodar
document.getElementById(“child”).addEventListe
ner(“click”, function(){
console.log(“Child clicked”);
});
document.getElementById(“parent”).addEventLis
te ner(“click”, function(){
console.log(“Parent clicked”);
});
Example:
graphicodar
31. What is the purpose of the setTimeout() function in
JavaScript?
console.log(“Start”);
setTimeOut( functio(){
console.log(“Delayed”);
}, 2000);
console.log(“End”);
Start
End
Delayed
graphicodar
32. What is event delegation and why is it useful?
Event delegation is a technique where you attach a
single event listener to a parent element to handle
events occurring on its child elements. It’s useful for
dynamically created elements or when you have a
large number of elements.
<ul id=”mylist”>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
document.getElementById(“mylist”).addEventList
e ner(“click”, function(event){
if(event.target.nodeName === “LI”){
console.log(event.target.textContent);
}
});
graphicodar
<a href=”#” id=”myLink” >Click Me!<a/>
document.getElementById(“myLink”).addEventLi
ste ner(“click”, function(event){
event.target.preventDefault();
console.log(“Link clicked, but default behavior
prevented”);
});
sessionStorage.setItem(“name”, “Jonh”);
console.log(sessionStorage.getItem(“name”));
graphicodar
35. How can you convert a string to lowercase in
JavaScript?
graphicodar
ADVANCED CONCEPTS
graphicodar
Example of splice():
Example of slice():
graphicodar
39. How can you check if an array includes a certain
value in JavaScript?
You can use the includes() method to check if an
array includes a specific value. It returns true if the
value is found, and false otherwise.
console.log(fruits.includes(“grape”));
//Output: false
graphicodar
41. What is the difference between an array and an
object in JavaScript?
An array is a data structure that can store a
collection of values. An object is a data structure
that can store a collection of properties.
graphicodar
43. What is the purpose of the fetch() function in
JavaScript?
The fetch() function is used to make asynchronous
HTTP requests in JavaScript. It returns a Promise
that resolves to the response from the server.
Example:
fetch(URL)
.then(function(response){
return response.json();
})
.then(function(data){
console.log(data);
})
.catch(function(error){
console.log(“Error Occured: ” + error);
});
graphicodar
function generateNum(){
yield 1;
yield 2;
yield 3;
}
const generator = generateNum();
console.log(generator.next().value); //Output: 1
console.log(generator.next().value); //Output: 2
console.log(generator.next().value); //Output: 3
graphicodar
46. What are the different ways to access an HTML
element in JavaScript?
There are three main ways to access an HTML
element in JavaScript:
graphicodar
48. What are the different ways to create objects in
JavaScript?
In JavaScript, there are several ways to declare or
construct an object.
1. Object.
2. using Class.
3. create Method.
4. Object Literals.
5. using Function.
6. Object Constructor
graphicodar
async function fetchData(){
try{
const response = await fetch(URL);
const data = await response.json();
console.log(data);
}catch(error){
console.log(“Error Occured: ” + error);
};
}
fetchData();
graphicodar
HOW TO PREPARE FOR A
JAVASCRIPT INTERVIEW
graphicodar
1. Review JavaScript Fundamentals
graphicodar
4. Master Debugging Skills
5. Practice Coding
6. Build Projects
graphicodar
7. Mock Interviews
graphicodar