40+ JavaScript Interview Questions and Answers (2024) - Copy
40+ JavaScript Interview Questions and Answers (2024) - Copy
Below, we have compiled the Top 45+ JavaScript Interview Questions and
Answers tailored for both freshers and experienced developers with 3, 5,
and 8 years of experience. Here, we will cover everything, including Core
JavaScript Concepts, ES6+ features, DOM manipulation, asynchronous
JavaScript, error handling, JavaScript frameworks and libraries, and more,
that will surely help you crack your next JavaScript interview.
Open In App
Table of Content
JavaScript Interview Questions for Freshers
JavaScript Intermediate Interview Questions
JavaScript Interview Questions for Experienced
JavaScript data types are categorized into two parts i.e. primitive and non-
primitive types.
Open In App
Primitive Data Type: The predefined data types provided by JavaScript
language are known as primitive data type. Primitive data types are
also known as in-built data types.
Numbers
Strings
Boolean
Symbol
Undefined
Null
BigInt
Non-Premitive Data Type: The data types that are derived from
primitive data types are known as non-primitive data types. It is also
known as derived data types or reference data types.
Objects
Functions
Arrays
/*
Multi-line comments
. . .
*/
Open In App
4. What would be the result of 3+2+”7″?
Here, 3 and 2 behave like an integer, and “7” behaves like a string. So 3
plus 2 will be 5. Then the output will be 5+”7″ = 57.
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.
Yes, it is possible to break the JavaScript code into several lines in a string
statement. It can be broken by using the backslash n ‘\n’.
For example:
Open In App
The code-breaking line is avoid by JavaScript which is not preferable.
html
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
</head>
<body>
<button onclick="create()">
Click Here!
</button>
<script>
function create() {
let geeks =Open In App
document.createElement('geeks');
geeks.textContent = "Geeksforgeeks";
geeks.setAttribute('class', 'note');
document.body.appendChild(geeks);
}
</script>
</body>
</html>
12. What are global variables? How are these variables declared, and
what are the problems associated with them?
Example:
javascript
function myFunction() {
console.log("Inside myFunction - Type of petName:", typeof
petName);
console.log("Inside myFunction - petName:", petName);
}
Output
The delete keyword deletes the whole property and all the values at once
like
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.
Open In App
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.
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 and increment/decrement in one line thereby
providing a shorter, easy to debug structure of looping.
Open In App
do while: A do-while loop is similar to while loop with the only
difference that it checks the condition after executing the statements,
and therefore is an example of Exit Control Loop.
document.getElementById("myText").style.fontSize = "16px;
document.getElementById("myText").className = "class";
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";
Open In App
In JavaScript, parseInt() function is used to convert the string to an
integer. This function returns an integer of base which is specified in
second argument of parseInt() function. The parseInt() function returns
Nan (not a number) when the string doesn’t contain number.
26. Explain how to detect the operating system on the client machine?
To detect the operating system on the client machine, one can simply use
navigator.appVersion or navigator.userAgent 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
28. What is the difference between an alert box and a confirmation box?
An alert box will display only one button which is the OK button. It is used
to inform the user about the agreement has to agree. But a Confirmation
box displays two buttons OK and cancel, where the user can decide to
agree or not.
31. What is the ‘Strict’ mode in JavaScript and how can it be enabled?
The DOM Input Checkbox Property is used to set or return the checked
status of a checkbox field. This property is used to reflect the HTML
Checked attribute.
document.getElementById("GFG").checked;
Open In App
JavaScript
// Explanation of closure
function foo() {
let b = 1;
function inner() {
return b;
}
return inner;
}
let get_func_inner = foo();
console.log(get_func_inner());
console.log(get_func_inner());
console.log(get_func_inner());
Output
1
1
1
This can be done by using the target attribute in the hyperlink. Like
Open In App
<a href="/geeksforgeeks.htm" target="newframe">New Page</a>
JavaScript
Jscript
Open In App
There are four possible ways to access HTML elements in JavaScript
which are:
The innerText property sets or returns the text content as plain text of the
specified node, and all its descendants whereas the innerHTML property
sets or returns the plain text or HTML contents in the elements. Unlike
innerText, inner HTML lets you work with HTML rich text and doesn’t
automatically encode and decode text.
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:
2. true, false
3. false, true
4. false, false
Answer:
Explanation:
const obj1 = { a: 1 };
const obj2 = { a: 1 };
console.log(obj1 == obj2);
console.log(obj1 === obj2);
Options:
1. true, true
2. false, false
3. false, true
4. True, false
Answer:
Open In App
2
Explanation:
let x = 10;
let y = (x++, x + 1, x * 2);
console.log(y);
1. 11
2. 22
3. 12
4. 20
Answer:
Explanation:
1. A D B C
2. A B C D
3. A D C B
4. A C D B
Answer:
Explanation:
function foo(num) {
if (num === 0) return 1;
return num + foo(num - 1);
}
console.log(foo(3));
1. 3
2. 6
3. 7
4. 10
Open In App
Answer:
3
Explanation:
foo(2) →2 + foo(1)
foo(1) →1 + foo(0)
foo(0) →1
So, the total is 3 + 2 + 1 + 1 = 7.
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:
2
Open In App
Explanation:
function test() {
console.log(this);
}
test.call(null);
1. null
2. undefined
3. Window or global object
4. TypeError
Answer:
Explanation:
There are six: number, string, boolean, null, undefined, and symbol.
Open In App
How do you explain ‘hoisting’ in JavaScript?
=== checks for strict equality (value and type), while == performs type
coercion before comparison.
Use a for loop or a forEach method to iterate over each item in the
array.
Are you feeling lost in OS, DBMS, CN, SQL, and DSA chaos? Our Complete
Interview Preparation Course is your solution! Trusted by over 100,000+
Geeks, it covers all essential topics, ensuring you're well-prepared for
technical challenges. Join the ranks of successful candidates and unlock
your placement potential. Enroll now and start your journey to a
successful career!
Open In App
S Saby… Follow 79
Similar Reads
+3 More
Open In App
Company Explore
About Us Job-A-Thon Hiring Challenge
Legal Hack-A-Thon
Careers GfG Weekly Contest
In Media Offline Classes (Delhi/NCR)
Contact Us DSA in JAVA/C++
Advertise with us Master System Design
GFG Corporate Solution Master CP
Placement Training Program GeeksforGeeks Videos
Geeks Community
Languages DSA
Python Data Structures
Java Algorithms
C++ DSA for Beginners
PHP Basic DSA Problems
GoLang DSA Roadmap
SQL DSA Interview Questions
R Language Competitive Programming
Android Tutorial