JavaScript Interview Questions and Answers - GeeksforGeeks
JavaScript Interview Questions and Answers - GeeksforGeeks
Search...
In Both Front-end and Back-end Interviews, JavaScript was asked, and its
difficulty depends upon the on your profile and company. Here, we
compiled 70+ JS Interview questions on every difficulty level
we can concatenate two strings using the "+" operator as code below:
Output
Hello World
[Link] 1/39
8/22/25, 3:53 PM JavaScript Interview Questions and Answers - GeeksforGeeks
[Link](result);
Output:
57
No, there names sounds similar but they are not related in any terms,
below are some key differences:
Java JavaScript
Java is an object-oriented
JavaScript is a scripting
programming language primarily
language used for creating
used for developing complex
interactive and dynamic web pages.
enterprise applications.
[Link] 2/39
8/22/25, 3:53 PM JavaScript Interview Questions and Answers - GeeksforGeeks
To learn more you can refer to difference between Java and JavaScript.
[Link] 3/39
8/22/25, 3:53 PM JavaScript Interview Questions and Answers - GeeksforGeeks
The number isNan function determines whether the passed value is NaN
(Not a number) and is of the type "Number". In JavaScript, the value NaN is
considered a type of number. It returns true if the argument is not a
number, else it returns false.
Output:
11
Explanation: When you assign to arr[10] you create empty slots from
index 3 to 9, making the new length one more than the highest index: 10 +
1 = 11.
[Link] 4/39
8/22/25, 3:53 PM JavaScript Interview Questions and Answers - GeeksforGeeks
Yes, it is possible to break the JavaScript code into several lines in a string
statement. It can be broken by using the '\n' (backslash n).
Example:
Undefined: It occurs when a variable is declare but not assign any value.
Undefined is not a keyword.
Undeclared: It occurs when we try to access any variable which is not
initialize or declare earlier using the var or const keyword. If we use
'typeof' operator to get the value of an undeclare variable, we will face
the runtime error with the return value as "undefined". The scope of the
undeclare variables is always global.
[Link] 5/39
8/22/25, 3:53 PM JavaScript Interview Questions and Answers - GeeksforGeeks
[Link](null ?? 'default');
[Link](undefined ?? 'default');
[Link](false ?? 'default');
Output:
default
default
false
Explanation:
The nullish coalescing operator ?? returns the right-hand side only if the
left is null or undefined. So:
<html>
<head>
DSA with JS - Self Paced JS Tutorial JS Exercise JS Interview Questions JS Ar Sign In
</head>
<body>
<button onclick="create()">
Click Here!
</button>
<script>
function create() {
let geeks = [Link]('geeks');
[Link] = "Geeksforgeeks";
[Link]('class', 'note');
[Link](geeks);
}
</script>
</body>
</html>
[Link] 6/39
8/22/25, 3:53 PM JavaScript Interview Questions and Answers - GeeksforGeeks
15. What are global variables? How are these variables declared,
and what are the problems associated with them?
Example:
function myFunction() {
[Link]("Inside myFunction - Type of petName:", typeof
petName);
[Link]("Inside myFunction - petName:", petName);
}
Output
It is difficult to debug and test the code that relies on global variables.
[Link] 7/39
8/22/25, 3:53 PM JavaScript Interview Questions and Answers - GeeksforGeeks
The delete keyword deletes the whole property and all the values at once
like
let x = 0;
[Link](x++);
[Link](++x);
Output:
0
2
Explanation:
undefined:
let x;
[Link](x); // undefined
function foo(a) {
[Link] 8/39
8/22/25, 3:53 PM JavaScript Interview Questions and Answers - GeeksforGeeks
null:
Output:
false
false
The prompt box is a dialog box with an optional message prompting the
user to input some text. It is often used if the user wants to input a value
before entering a page. It returns a string containing the text entered by
the user, or null.
[Link] 9/39
8/22/25, 3:53 PM JavaScript Interview Questions and Answers - GeeksforGeeks
The timer executes some specific code at a specific time or any small
amount of code in repetition to do that you need to use the functions
setTimout, setInterval, and clearInterval. If the JavaScript code sets the
timer to 2 minutes and when the times are up then the page displays an
alert message "times up". The setTimeout() method calls a function or
evaluates an expression after a specified number of milliseconds.
Output:
0 1 2
Explanation: Using let i in the loop gives each callback its own i binding.
The timeouts fire after 0ms, 100ms, and 200ms, logging 0, then 1, then 2.
[Link] 10/39
8/22/25, 3:53 PM JavaScript Interview Questions and Answers - GeeksforGeeks
For example, map() and filter() are higher-order functions because they
take callback functions as arguments.
while loop: A while loop is a control flow statement that allows code to
be executed repeatedly based on a given Boolean condition. The while
loop can be thought of as a repeating if statement.
for loop: A for loop provides a concise way of writing the loop structure.
Unlike a while loop, for statement consumes the initialization, condition
[Link] 11/39
8/22/25, 3:53 PM JavaScript Interview Questions and Answers - GeeksforGeeks
Lexical scope in JavaScript refers to the way variables are resolved based
on their location in the source code. A variable's scope is determined by
the position of the code where it is defined, and it is accessible to any
nested functions or blocks. This means that functions have access to
variables in their own scope and the outer (lexical) scopes, but not to
variables in inner scopes.
In this example, inner() can access the outer variable because of lexical
scoping.
32. How does lexical scoping work with the this keyword in
JavaScript?
const obj = {
name: "JavaScript",
greet: function () {
[Link]([Link]);
[Link] 12/39
8/22/25, 3:53 PM JavaScript Interview Questions and Answers - GeeksforGeeks
}
};
[Link](); // "JavaScript"
Here, this refers to obj because the function is called as a method of the
object. Lexical scoping affects variable lookups but doesn’t alter how this
behaves.
The variable typing is the type of variable used to store a number and
using that same variable to assign a “string”.
Geeks = 42;
Geeks = "GeeksforGeeks";
[Link] 13/39
8/22/25, 3:53 PM JavaScript Interview Questions and Answers - GeeksforGeeks
This means you can reference variables and functions before they are
declared in the code. However, only declarations are hoisted, not
initializations.
[Link](a); // undefined
var a = 5;
In this case, the declaration of a is hoisted, but its value (5) is not assigned
until the code execution reaches that line. Hoisting applies differently for
var, let, const, and function declarations.
To detect the operating system on the client machine, one can simply use
[Link] or [Link] property. The Navigator
appVersion property is a read-only property and it returns the string that
represents the version information of the browser.
Alert
Confirm
Prompt
[Link] 14/39
8/22/25, 3:53 PM JavaScript Interview Questions and Answers - GeeksforGeeks
The void(0) is used to call another method without refreshing the page
during the calling time parameter “zero” will be passed.
JavaScript modules allow you to split your code into smaller, reusable
pieces. They enable the export of variables, functions, or objects from one
file and the import of them into another. To export an element, you use
export (either named or default). To import it, you use import.
// In [Link]
export const greet = () => "Hello";
// In [Link]
import { greet } from './file1';
[Link](greet());
Modules help organize code and avoid global namespace pollution. They
are natively supported in modern JavaScript through import and export
statements.
41. What are WeakMap and WeakSet, and how are they different
from Map and Set?
The main difference from Map and Set is that in Map and Set, entries are
strongly held, meaning they prevent garbage collection, while in
WeakMap and WeakSet, entries can be garbage collected if no other
references to the objects exist.
[Link] 15/39
8/22/25, 3:53 PM JavaScript Interview Questions and Answers - GeeksforGeeks
Advantages of async/await:
[Link] 16/39
8/22/25, 3:53 PM JavaScript Interview Questions and Answers - GeeksforGeeks
Disadvantages of async/await:
function foo() {
let b = 1;
function inner() {
return b;
}
return inner;
}
let get_func_inner = foo();
[Link](get_func_inner());
[Link](get_func_inner());
[Link](get_func_inner());
Output
1
1
[Link] 17/39
8/22/25, 3:53 PM JavaScript Interview Questions and Answers - GeeksforGeeks
call() Method: It calls the method, taking the owner object as argument.
The keyword this refers to the ‘owner’ of the function or the object it
belongs to. We can call a method that can be used on different objects.
apply() Method: The apply() method is used to write methods, which
can be used on different objects. It is different from the function call()
because it takes arguments as an array.
This can be done by using the target attribute in the hyperlink. Like
[Link] 18/39
8/22/25, 3:53 PM JavaScript Interview Questions and Answers - GeeksforGeeks
JavaScript
JScript
There are four possible ways to access HTML elements in JavaScript which
are:
[Link] 19/39
8/22/25, 3:53 PM JavaScript Interview Questions and Answers - GeeksforGeeks
=== is the strict equality operator, which compares both the values and
their types, without performing type conversion.
A shallow copy creates a new object but copies references to the original
nested objects, meaning changes to the nested objects affect both the
original and the copy. A deep copy, on the other hand, creates a new
object and recursively copies all nested objects, ensuring that the original
[Link] 20/39
8/22/25, 3:53 PM JavaScript Interview Questions and Answers - GeeksforGeeks
56. Explain the concept of the event loop and the call stack in
JavaScript. How does JavaScript handle asynchronous code
execution?
In JavaScript, the event loop manages the execution of code, handling both
synchronous and asynchronous operations. The call stack stores function
calls and executes them in a Last In, First Out (LIFO) order. When
asynchronous code (e.g., setTimeout, promises) is encountered, it’s
offloaded to the callback queue once its execution context is ready. The
event loop continuously checks the call stack and moves tasks from the
callback queue to the stack when it’s empty, allowing asynchronous code
to run without blocking the main thread.
57. What are Web Workers, and how do you use them to run
scripts in the background?
Web Workers are JavaScript threads that run in the background, separate
from the main thread, allowing long-running scripts to be executed
without blocking the user interface. You can create a Web Worker using
the Worker constructor, passing a JavaScript file as an argument. Once
created, the worker can perform tasks asynchronously, and you can
communicate with it via postMessage and onmessage events, ensuring the
main thread remains responsive.
[Link] 21/39
8/22/25, 3:53 PM JavaScript Interview Questions and Answers - GeeksforGeeks
Options:
1. number
2. string
3. boolean
4. All of the above
Answer:
Explanation:
JavaScript has several built-in data types, and number, string, and
boolean are all valid types.
A number represents numeric values, a string represents sequences of
characters, and a boolean represents either true or false. All of these
are fundamental data types in JavaScript.
[Link] 22/39
8/22/25, 3:53 PM JavaScript Interview Questions and Answers - GeeksforGeeks
1. [100, 2, 3]
2. [1, 2, 3]
3. [100, 100, 100]
4. undefined
Answer :
Explanation :
The code snippet represents two arrays [100, 2, 3] and [1, 2, 3] being
compared using the equality operator (==).
In JavaScript, arrays are reference types, meaning each array is a
reference to an object in memory.
[Link]([] + []);
Options:
1. null
2. undefined
3. ''
4. []
Answer :
Explanation:
The + operator concatenates two empty arrays, which results in an
empty string '' .
(function() {
[Link] 23/39
8/22/25, 3:53 PM JavaScript Interview Questions and Answers - GeeksforGeeks
var a = b = 5;
})();
[Link](typeof a);
[Link](typeof b);
Options:
1. typeof a: "undefined"
typeof b: "number"
2. typeof a: "number"
typeof b: "number"
3. typeof a: "undefined"
typeof b: "undefined"
4. typeof a: "number"
typeof b: "undefined"
Answer:
Explanation:
Options:
1. true, true
2. true, false
3. false, true
4. false, false
[Link] 24/39
8/22/25, 3:53 PM JavaScript Interview Questions and Answers - GeeksforGeeks
Answer:
Explanation:
const obj1 = { a: 1 };
const obj2 = { a: 1 };
[Link](obj1 == obj2);
[Link](obj1 === obj2);
Options:
1. true, true
2. true, false
3. false, true
4. false, false
Answer:
Explanation:
[Link] 25/39
8/22/25, 3:53 PM JavaScript Interview Questions and Answers - GeeksforGeeks
let x = 10;
let y = (x++, x + 1, x * 2);
[Link](y);
Options :
1. 22
2. 12
3. 21
4. 20
Answer:
22
Explanation:
The comma operator ( , ) evaluates all expressions but returns the value
of the last one.
x++ increments x to 11, but the result of this expression is the original
10.
x + 1 becomes 11 + 1 = 12, and the final expression x * 2 evaluates to
11 * 2 = 22, which is assigned to y.
[Link]('A');
setTimeout(() => [Link]('B'), 0);
[Link]().then(() => [Link]('C'));
[Link]('D');
Options:
1. A D B C
2. A B C D
3. A D C B
4. A C D B
[Link] 26/39
8/22/25, 3:53 PM JavaScript Interview Questions and Answers - GeeksforGeeks
Answer:
Explanation:
function foo(num) {
if (num === 0) return 1;
return num + foo(num - 1);
}
[Link](foo(3));
Options:
1. 3
2. 6
3. 7
4. 10
Answer:
Explanation:
[Link] 27/39
8/22/25, 3:53 PM JavaScript Interview Questions and Answers - GeeksforGeeks
Options:
1. [1, 2, 3]
[1, 2, 3, 4]
2. [1, 2, 3, 4]
[1, 2, 3, 4]
3. [1, 2, 3]
[1, 2, 3]
4. [1, 2, 3, 4]
[1, 2, 3]
Answer:
Explanation:
In JavaScript, arrays are reference types. Both a and b point to the same
array in memory. Modifying b also affects a.
function test() {
[Link](this);
}
[Link](null);
Options:
1. null
[Link] 28/39
8/22/25, 3:53 PM JavaScript Interview Questions and Answers - GeeksforGeeks
2. undefined
3. Window or global object
4. TypeError
Answer:
Explanation:
In non-strict mode, calling a function with this set to null defaults to the
global object (Window in browsers or global in [Link]).
Note: we recommend you should guess first then confirm your output by
hit on run.
70.
let a = 5;
[Link] 29/39
8/22/25, 3:53 PM JavaScript Interview Questions and Answers - GeeksforGeeks
let b = '5';
if (a == b) {
[Link]('Equal');
} else {
[Link]('Not Equal');
}
71.
72.
function arrayFromValue(item) {
return
[item];
}
[Link](arrayFromValue(10)); // ???
[Link] 30/39
8/22/25, 3:53 PM JavaScript Interview Questions and Answers - GeeksforGeeks
73.
const car = {
name: 'Toyota',
getName: function() {
return [Link];
},
};
74.
[Link](a); // ???
[Link](b); // ???
var a = 2;
let b = 2;
75.
[Link] 31/39
8/22/25, 3:53 PM JavaScript Interview Questions and Answers - GeeksforGeeks
76.
Explanation: The fill method fills all elements of the array with the
same reference to the same array. Therefore, when you modify one
element (e.g., arr[0].push(10)), all elements reflect this change
because they all point to the same array in memory.
77.
const obj = { x: 1 };
const { x, x: y } = obj;
[Link](y); // ???
78.
[Link] 32/39
8/22/25, 3:53 PM JavaScript Interview Questions and Answers - GeeksforGeeks
Explanation: The
first [Link] outputs "undefined" because undeclaredVar is not
declared at all. The second [Link] throws
a ReferenceError because y is declared with let and is in a "temporal
dead zone" from the start of the block until the declaration is
encountered. During this period, accessing y results in
a ReferenceError.
Primitive data types: string, number, boolean, undefined, null, symbol, bigint
Non-Primitive data types: objects, arrays and functions.
[Link] 33/39
8/22/25, 3:53 PM JavaScript Interview Questions and Answers - GeeksforGeeks
[Link] 34/39
8/22/25, 3:53 PM JavaScript Interview Questions and Answers - GeeksforGeeks
A closure is a function that retains access to its lexical scope (the scope in
which it was created) even after that scope has finished execution.
Closures allow functions to access variables from an outer function after
the outer function has returned.
function outer() {
let x = 10;
return function inner() {
[Link](x);
}
}
const closureFunc = outer();
closureFunc(); // prints 10
Output
10
The value of this can change depending on how the function is called.
Here are the different scenarios where this behaves differently:
function sayGoodbye() {
[Link]("Goodbye!");
}
greet("Anjali", sayGoodbye);
Output
Hello Anjali
Goodbye!
[Link] 36/39
8/22/25, 3:53 PM JavaScript Interview Questions and Answers - GeeksforGeeks
Apart from these questions you can also practice JavaScript Quiz for better
understanding of every topic to enhance your knowledge and helping you
in the interviews.
1. Basic JavaScript
2. Variables and Data Types
3. Operators
4. Control Flow
5. Functions
6. Objects
7. Arrays
8. DOM and BOM
9. Event Handling
10. Classes and Inheritance
11. Modern JavaScript (ES6+)
12. Advanced JavaScript
13. Regular Expressions and JSON
14. Asynchronous JavaScript
15. Error Handling and Debugging
[Link] 37/39
8/22/25, 3:53 PM JavaScript Interview Questions and Answers - GeeksforGeeks
Registered Address:
K 061, Tower K, Gulshan Vivante
Apartment, Sector 137, Noida, Gautam
Buddh Nagar, Uttar Pradesh, 201305
Advertise with us
[Link] 38/39
8/22/25, 3:53 PM JavaScript Interview Questions and Answers - GeeksforGeeks
Company Explore
About Us POTD
Legal Job-A-Thon
Privacy Policy Connect
Careers Community
Contact Us Videos
Corporate Solution Blogs
Campus Training Program Nation Skill Up
Tutorials Courses
Programming Languages IBM Certification
DSA DSA and Placements
Web Technology Web Development
AI, ML & Data Science Data Science
DevOps Programming Languages
CS Core Subjects DevOps & Cloud
Interview Preparation GATE
GATE Trending Technologies
School Subjects
Software and Tools
[Link] 39/39