JAVASCRIPT INTERVIEW
QUESTIONS & ANSWERS
Important JavaScript questions and answers
for JavaScript Developer technical interview
DEVELOPER UPDATES
JAVASCRIPT INTERVIEW QUESTIONS AND ANSWERS
LocalStorage is a feature of JavaScript that allows you to save
data on the client side. This means that the data is kept in local
storage on the user's device, instead of on a server.
Cookies can be accessed on the server and localStorage data is
not accessed on the server.
Why “use strict” is used in JavaScript?
'Use strict' is used in JavaScript to make sure that your code is
written correctly. Strict mode enforces the following rules
Variables must be declared before they are used,
All comparisons must be done between literal values
No function calls can be made until the function has been
defined.
This helps to catch errors early on in the development process
and keeps your code more organised.
What is the difference between null and undefined?
The difference between null and undefined is that null
represents a nonexistent value, while undefined represents a
value that hasn't been defined yet.
What are promises?
In JavaScript, promises are used to handle asynchronous
operations. They are objects that represent the successful or
unsuccessful completion of an asynchronous operation, allowing
you to write more concise and readable code when working
with asynchronous APIs.
A Promise has three states: pending, fulfilled, or rejected. A
Promise is said to be settled when it is fulfilled or rejected, and
its value cannot be changed.
Promises simplify code execution, avoid callback hell, and
improve readability.
DEVELOPER UPDATES [Link] 06
JAVASCRIPT INTERVIEW QUESTIONS AND ANSWERS
Write a code to demonstrate promises.
Let's assume, if you're creating a web application that sends data
requests to a server, you must handle those requests
asynchronously to keep the application responsive to user
interactions.
Promises are an important part of this type of task because they
let you handle operations that happen at different times in a
clean and effective way.
Function returning promise
On success, Resolving promise with data
On failure, Rejecting promise
On success, this block will be executed
On failure, this block will be executed
DEVELOPER UPDATES [Link] 07
JAVASCRIPT INTERVIEW QUESTIONS AND ANSWERS
What is an arrow function? Give an example.
With arrow functions, you can write functions in a shorter way,
which makes code easier to read and write. They also have some
advantages over traditional functions, like being able to look at
the lexical scope (Accessing variables and functions defined in
the outer function or global scope).
Arrow functions implicitly bind the this keyword to the
enclosing scope, so you don't need to use bind(), call(), or apply()
to bind it to the current object.
For example, arrow functions can help you write code that is
easier to read and understand when you need to write several
small functions for event listeners.
Here is the example of arrow function
Arrow function
Arrow function with single expression
DEVELOPER UPDATES [Link] 08
JAVASCRIPT INTERVIEW QUESTIONS AND ANSWERS
What is NaN Property?
The NaN (not a number) property is used to represent a value
that does not exist in the number system. The NaN property can
be used to check if a number is valid or not.
What is the difference between “==” and “===”?
“==” is used to check the equality of the variable and “===” is used
to check equality as well as data types of the variable.
What is a callback function in JavaScript?
A callback function is a function that is passed as an argument
to another function and it is executed after some
operation/event is executed.
DEVELOPER UPDATES [Link] 09
JAVASCRIPT INTERVIEW QUESTIONS AND ANSWERS
How to define a callback function?
In above example, we passed sendMessage as an argument to
getMessage() and sendMessage() is executed after getting a
message.
DEVELOPER UPDATES [Link] 10
JAVASCRIPT INTERVIEW QUESTIONS AND ANSWERS
What is the scope of the let variable?
The scope of a let variable is the block in which it is defined. In
other words, the let keyword will reserve a block of memory for
the variable and it will only be accessible from within that block.
If you want to access the let variable from outside of its block,
you will need to use the var keyword.
What are closures?
A closure in JavaScript is a special type of function that keeps
track of variables and information from the outside, or
"enclosing", function even after it has finished running.
This allows the inner function to continue to use and reference
these values, even after the outer function has completed its
execution.
Closures are a useful for creating private variables and
generating functions with specific attributes.
For example, let's consider a website where a user can click on
different buttons to change the background color of the page.
Without using closures, you would need to create a separate
function for each button to handle the click event and change
the background color, like this:
DEVELOPER UPDATES [Link] 11
JAVASCRIPT INTERVIEW QUESTIONS AND ANSWERS
Function to set red background color
Function to set green background color
Call functions on click event
This function will generate new
function for each button(Closure)
Calling closure for each button, We can create
any number of color buttons
In this example, the changeBackgroundColor function acts as a
function factory, generating a new function for each button that has
the desired background color "closed over" within its scope.
This way, when the generated function is executed, it has access to
the correct background color value, even though the
changeBackgroundColor function has already returned.
DEVELOPER UPDATES [Link] 12
JAVASCRIPT INTERVIEW QUESTIONS AND ANSWERS
What is the scope?
Scope refers to the current context of code execution. Scope
determines the visibility and lifetime of variables and functions.
JavaScript has two types of scope: global scope and local scope.
Global scope addresses the context in which all code is
executed. Variables and functions declared in the global scope
are available to all parts of your code
Local scope is the context limited to a function. Variables and
functions inside a function are only visible and accessible within
that function, making them invisible outside of it.
Write a function to set cookies using JavaScript?
Write a function to read cookies values using
JavaScript?
DEVELOPER UPDATES [Link] 13
JAVASCRIPT INTERVIEW QUESTIONS AND ANSWERS
How to add and remove a class to HTML Element?
How to fetch data from an API?
There are several different methods that you can use in
Javascript. The most common way is to use the fetch() method.
This method accepts a URL as an input and returns the data that
is located at that URL.
Can we use Javascript at the server-side and How?
Yes we can use Javascript at the server-side using [Link].
Can we use JavaScript for Mobile and Desktop App
Development and How?
Yes, We can use Javascript for Mobile Development using React-
Native. And for desktop app development using the [Link]
framework.
DEVELOPER UPDATES [Link] 14
JAVASCRIPT INTERVIEW QUESTIONS AND ANSWERS
List out the different types of JavaScript Frameworks
and Libraries?
1. [Link]
2. Angular
3. [Link]
4. [Link]
5. React Native
6. [Link]
7. [Link]
How to empty an array?
DEVELOPER UPDATES [Link] 15
Stay ahead with daily updates!
Follow us on social media for useful web
development content.
@richwebdeveloper
@new_javascript
@developerupdates
@developerupdates
@_chetanmahajan
Note: This kit is continuously updated. Please continue to
check our website for updated questions and answers. You will
receive all updates for FREE
Download Now
[Link]