Top 80+ JavaScript Interview Questions (Ultimate List)
Top 80+ JavaScript Interview Questions (Ultimate List)
This article provides you with a comprehensive list of common JavaScript interview questions
and answers that often come up in interviews. It will also help you understand the fundamental
concepts of JavaScript.
EXPLORE PROGRAM
Here are some basic JavaScript interview questions and answers for you to prepare during your
interviews.
JavaScript is a popular web scripting language and is used for client-side and server-side
development. The JavaScript code can be inserted into HTML pages that can be understood
and executed by web browsers while also supporting object-oriented programming abilities.
JavaScript Java
Java is an object-oriented
JavaScript is an object-oriented scripting language.
programming language.
Undefined - For variables that are only declared and not defined or initialized
https://www.simplilearn.com/tutorials/javascript-tutorial/javascript-interview-questions 3/39
4/19/24, 11:29 AM Top 80+ JavaScript Interview Questions [Ultimate List]
Object - For collections or complex values
Cross-platform compatible
Open-source
Object-oriented
Enhanced Interaction
JavaScript adds interaction to otherwise static web pages and makes them react to users’
inputs.
Quick Feedback
There is no need for a web page to reload when running JavaScript. For example, form input
validation.
JavaScript helps in making the UI of web applications look and feel much better.
Frameworks
JavaScript has countless frameworks and libraries that are extensively used for developing web
applications and games of all kinds.
const student = {
name: 'John',
age: 17
var a = [];
REGISTER NOW
https://www.simplilearn.com/tutorials/javascript-tutorial/javascript-interview-questions 5/39
4/19/24, 11:29 AM Top 80+ JavaScript Interview Questions [Ultimate List]
Global Scope
Global variables, having global scope are available everywhere in a JavaScript code.
Local Scope
Local variables are accessible only within a function in which they are defined.
https://www.simplilearn.com/tutorials/javascript-tutorial/javascript-interview-questions 6/39
4/19/24, 11:29 AM Top 80+ JavaScript Interview Questions [Ultimate List]
Variable names cannot be similar to that of reserved keywords. For example, var, let, const,
etc.
Variable names cannot begin with a numeric value. They must only begin with a letter or an
underscore character.
We can also debug a JavaScript code inside a code editor that we use to develop a JavaScript
application—for example, Visual Studio Code, Atom, Sublime Text, etc.
Function expression?
Example: Example:
return 5; return 5;
} }
We can write JavaScript code within the script tag in the same HTML file; this is suitable
when we need just a few lines of scripting within a web page.
https://www.simplilearn.com/tutorials/javascript-tutorial/javascript-interview-questions 8/39
4/19/24, 11:29 AM Top 80+ JavaScript Interview Questions [Ultimate List]
We can import a JavaScript source file into an HTML document; this adds all scripting
capabilities to a web page without cluttering the code.
Here are some intermediate level JavaScript interview questions and answers for you to
prepare during your interviews.
A cookie is generally a small data that is sent from a website and stored on the user’s machine
by a web browser that was used to access the website. Cookies are used to remember
information for later use and also to record the browsing activity on a website.
The document.cookie string keeps a list of name-value pairs separated by semicolons, where
‘name’ is the name of the cookie, and ‘value’ is its value. We can also use the split() method to
break the cookie value into keys and values.
https://www.simplilearn.com/tutorials/javascript-tutorial/javascript-interview-questions 9/39
4/19/24, 11:29 AM Top 80+ JavaScript Interview Questions [Ultimate List]
function delete_cookie(name) {
EXPLORE PROGRAM
function hello(name) {
https://www.simplilearn.com/tutorials/javascript-tutorial/javascript-interview-questions 10/39
4/19/24, 11:29 AM Top 80+ JavaScript Interview Questions [Ultimate List]
console.log(message);
};
//generate closure
//use closure
helloWorld();
console.log("hello world!");
};
getElementByClass(‘classname’): Gets all the HTML elements that have the specified
classname.
getElementbyTagName(‘tagname’): Gets all the HTML elements that have the specified
tagname.
querySelector(): Takes CSS style selector and returns the first selected HTML element.
Var
This is used to declare a variable and the value can be changed at a later time within the
JavaScript code.
Const
We can also use this to declare/define a variable but the value, as the name implies, is constant
throughout the JavaScript program and cannot be modified at a later time.
Let
This mostly implies that the values can be changed at a later time within the JavaScript code.
calc.js
return x * x;
This file exports two functions that calculate the squares and diagonal of the input respectively.
main.js
https://www.simplilearn.com/tutorials/javascript-tutorial/javascript-interview-questions 12/39
4/19/24, 11:29 AM Top 80+ JavaScript Interview Questions [Ultimate List]
console.log(square(4)); // 16
console.log(diag(4, 3)); // 5
Therefore, here we import those functions and pass input to those functions to calculate square
and diagonal.
Document Window
Undefined Undeclared
https://www.simplilearn.com/tutorials/javascript-tutorial/javascript-interview-questions 13/39
4/19/24, 11:29 AM Top 80+ JavaScript Interview Questions [Ultimate List]
Undefined Null
31. What are the various data types that exist in JavaScript?
Javascript consists of two data types, primitive data types, and non-primitive data types.
https://www.simplilearn.com/tutorials/javascript-tutorial/javascript-interview-questions 14/39
4/19/24, 11:29 AM Top 80+ JavaScript Interview Questions [Ultimate List]
Primitive Data types: These data types are used to store a single value. Following are the
sub-data types in the Primitive data type.
Example:
var a = 3;
var b = 4;
var c = 3;
(a == b) // returns false
(a == c) //returns true
Example:
var z = null;
Undefined data Types: It stores variables that are only declared, but not defined or initialized.
Example:
var a; // a is undefined
Example:
Example:
https://www.simplilearn.com/tutorials/javascript-tutorial/javascript-interview-questions 15/39
4/19/24, 11:29 AM Top 80+ JavaScript Interview Questions [Ultimate List]
Example:
BigInt Data Types: It stores the Number data types that are large integers and are above the
limitations of number data types.
Example:
Non-Primitive data types are used to store multiple as well as complex values.
Example:
var obj1 = {
x: 43,
y: "Hello world!",
z: function(){
return this.x;
The Keyword ‘this’ in JavaScript is used to call the current object as a constructor to assign
values to object properties.
Example:
function sayHello()
sayHello.call(obj);
Apply
Example:
function saySomething(message)
https://www.simplilearn.com/tutorials/javascript-tutorial/javascript-interview-questions 17/39
4/19/24, 11:29 AM Top 80+ JavaScript Interview Questions [Ultimate List]
saySomething.apply(person4, ["awesome"]);
Global Scope: It is used to access the variables and functions from anywhere inside the
code.
Example:
function sendMessage(){
function sendMessage2(){
Function scope: It is used to declare the function and variables inside the function itself and
not outside.
Example:
function awesomeFunction()
var a = 3;
{
https://www.simplilearn.com/tutorials/javascript-tutorial/javascript-interview-questions 18/39
4/19/24, 11:29 AM Top 80+ JavaScript Interview Questions [Ultimate List]
console.log(a*3); // Can access variable "a" since a and multiplyBy3 both are written inside
the same function
console.log(a); // a is written in local scope and can't be accessed outside and throws a
reference error
Example:
let x = 45;
console.log(x); // Gives reference error since x cannot be accessed outside of the block
// do something
console.log(i); // Gives reference error since i cannot be accessed outside of the for loop block
https://www.simplilearn.com/tutorials/javascript-tutorial/javascript-interview-questions 19/39
4/19/24, 11:29 AM Top 80+ JavaScript Interview Questions [Ultimate List]
console.log("hello world!");
};
Example:
return a + b;
Example 1:
hoistedFunction(); // " Hi There! " is an output that is declared as function even after it is called
function hoistedFunction(){
Example 2:
hoistedVariable = 5;
https://www.simplilearn.com/tutorials/javascript-tutorial/javascript-interview-questions 20/39
4/19/24, 11:29 AM Top 80+ JavaScript Interview Questions [Ultimate List]
var hoistedVariable;
2. “===” operator is also a comparison operator that is used to compare the values as well as
types.
Example:
var x = 3;
var y = "3";
In JavaScript programming, the “var” keyword has been used from the very initial stages of
JavaScript.
We can perform functions with the help of the keyword “var’ by accessing various variables.
Keyword “let”
The Keyword “let” was added later in ECMAScript 2015 in JavaScript Programming.
Variable declaration is very limited with the help of the “let” keyword that is declared in Block.
Also, it might result in a ReferenceError as the variable was declared in the “temporal dead
zone” at the beginning of the block.
String coercion
Example:
https://www.simplilearn.com/tutorials/javascript-tutorial/javascript-interview-questions 21/39
4/19/24, 11:29 AM Top 80+ JavaScript Interview Questions [Ultimate List]
var x = 4;
var y = "4";
x + y // Returns "44"
Boolean coercion
Example:
var a = 0;
var b = 32;
if(a) { console.log(a) } // This code will run inside the block as the value of x is 0(Falsy)
if(b) { console.log(b) } // This code will run inside the block as the value of y is 32 (Truthy)
EXPLORE PROGRAM
Here, the a=432 is a primitive data type i.e. a number type that has an assigned value by the
operator. When the var b=a code gets executed, the value of ‘var a’ returns a new address for
‘var b’ by allocating a new space in the memory, so that ‘var b’ will be operated at a new
location.
Example:
var a = 432;
var b = a;
The reference of the 1st variable object i.e. ‘var obj’ is passed through the location of another
variable i.e. ‘var obj2’ with the help of an assigned operator.
Example:
https://www.simplilearn.com/tutorials/javascript-tutorial/javascript-interview-questions 23/39
4/19/24, 11:29 AM Top 80+ JavaScript Interview Questions [Ultimate List]
Syntax
(function()
// Do something;
})
();
One cannot use JavaScript keywords as a parameter or function name in strict mode.
Strict mode can be defined at the start of the script with the help of the keyword ‘use strict’.
Example:
function higherOrder(fn)
https://www.simplilearn.com/tutorials/javascript-tutorial/javascript-interview-questions 24/39
4/19/24, 11:29 AM Top 80+ JavaScript Interview Questions [Ultimate List]
fn();
Once it has been found, the pattern will be returned directly, otherwise, it returns an “empty”
result.
test ()
It is an expression method in JavaScript that is also used to search a string with a specific
pattern or text.
Once it has been found, the pattern will return the Boolean value 'true', else it returns ‘false’.
Example:
return function(b){
return a + b;
https://www.simplilearn.com/tutorials/javascript-tutorial/javascript-interview-questions 25/39
4/19/24, 11:29 AM Top 80+ JavaScript Interview Questions [Ultimate List]
add(3)(4)
1. Date objects are used to inherit properties from the Date prototype
2. Math objects are used to inherit properties from the Math prototype
3. Array objects are used to inherit properties from the Array prototype.
https://www.simplilearn.com/tutorials/javascript-tutorial/javascript-interview-questions 26/39
4/19/24, 11:29 AM Top 80+ JavaScript Interview Questions [Ultimate List]
Constructor functions are used to create single objects or multiple objects with similar properties
and methods.
Example:
function Person(name,age,gender)
this.name = name;
this.age = age;
this.gender = gender;
console.log(person1);
console.log(person2);
Also, it is automatically included in the HTML pages where the browser understands the
script.
Server-side Javascript
1. There is no block
scope. There is no block scope. There is no block scope.
https://www.simplilearn.com/tutorials/javascript-tutorial/javascript-interview-questions 28/39
4/19/24, 11:29 AM Top 80+ JavaScript Interview Questions [Ultimate List]
function catchValues()
console.log(variable1);
console.log(variable2);
// Both the variables are accessible from anywhere as their declaration is in the global scope
const x = {name:"Vijay"};
const y = 31;
Rest parameter is used to declare the function with improved handling of parameters.
Rest parameter syntax can be used to create functions to perform functions on the variable
number of arguments.
It also helps to convert any number of arguments into an array as well as helps in extracting
some or all parts of the arguments.
Spread Operator(...)
It's also to spread one or more arguments that are expected in a function call
https://www.simplilearn.com/tutorials/javascript-tutorial/javascript-interview-questions 29/39
4/19/24, 11:29 AM Top 80+ JavaScript Interview Questions [Ultimate List]
It s also to spread one or more arguments that are expected in a function call.
The spread operator is used to take an array or an object and spread them.
Pending is an initial
It is the state
state of promise. It
where the It is the
is the initial state of
It is the state where the promise promise is state where
promise where it is
has been fulfilled that assures rejected and the promise
in the pending state
that the async operation is done. the async is rejected
that neither is
operation has or fulfilled.
fulfilled nor
failed.
rejected.
Example:
function sumOfThreeElements(...elements)
if(elements.length > 3 )
else
l t 0
https://www.simplilearn.com/tutorials/javascript-tutorial/javascript-interview-questions 30/39
4/19/24, 11:29 AM Top 80+ JavaScript Interview Questions [Ultimate List]
let sum = 0;
let i = 0;
sum += elements[i];
i++;
})
It also ensures that a particular code does not run until another code has completed its
execution.
console.log(first); // Outputs 1
console.log(second); // Outputs 2
console.log(third); // Outputs 3
Prototypal inheritance allows any object to be cloned via an object linking method and it
serves as a template for those other objects, whether they extend the parent object or not.
https://www.simplilearn.com/tutorials/javascript-tutorial/javascript-interview-questions 32/39
4/19/24, 11:29 AM Top 80+ JavaScript Interview Questions [Ultimate List]
Classical Inheritance
Classical inheritance is a class that inherits from the other remaining classes.
EXPLORE PROGRAM
Async-await functions are executed sequentially one after another in an easier way.
Generators
Generator functions are executed with one output at a time by the generator’s yield by yield.
The ‘value: X, done: Boolean’ is the output result of the Generator function.
We can debug in JavaScript with the help of two methods, console.log() and debugger
keyword.
Here are some advanced level JavaScript interview questions and answers for you to prepare
during your interviews.
https://www.simplilearn.com/tutorials/javascript-tutorial/javascript-interview-questions 34/39
4/19/24, 11:29 AM Top 80+ JavaScript Interview Questions [Ultimate List]
arr.length = 0;
arr = [];
arr.pop();
arr.splice(0, arr.length);
While in Strict mode, all variables have to be declared explicitly, values cannot be assigned
to a read-only property, etc.
We can enable strict mode by adding ‘use strict’ at the beginning of a JavaScript code, or
within a certain segment of code.
if (function abc(){})
a += typeof abc;
console.log(a);
The output of this JavaScript code will be 10undefined. The if condition statement in the code
evaluates using eval. Hence, eval(function abc(){}) will return function abc(){}.
Inside the if statement, executing typeof abc returns undefined because the if statement code
executes at run time while the statement inside the if the condition is being evaluated.
84. Can you write a JavaScript code for adding new elements in
a dynamic manner?
<script type="text/javascript">
function addNode() {
https://www.simplilearn.com/tutorials/javascript-tutorial/javascript-interview-questions 36/39
4/19/24, 11:29 AM Top 80+ JavaScript Interview Questions [Ultimate List]
newP.appendChild(textNode); document.getElementById("firstP").appendChild(newP);
</script>
Call Apply
return 11;
};
typeof Foo();
The output would be a reference error since a function definition can only have a single
reference variable as its name.