Basic JavaScript Interview Questions
Basic JavaScript Interview Questions
1. What is JavaScript?
JavaScript is a popular web scripting language that’s used for client-side and server-
side development. JavaScript can be inserted into HTML pages and executed by
web browsers.
Apart from web development, it’s used in mobile app development, game
development, querying databases, and more.
Note: This is the most basic JavaScript question, so it’s important not to overlook it.
<!DOCTYPE html>
<html>
<head>
<script>
console.log("This is JS code.");
</script>
</head>
<body>
</body>
</html>
2. Write JavaScript code in an external file with a .js extension, then use
the src property in the script tag.
<!DOCTYPE html>
<html>
<head>
<script src="file_path.js"></script>
</head>
<body>
</body>
</html>
var firstValue = 1;
Number.POSITIVE_INFINITY;
Number.NEGATIVE_INFINITY;
console.log(isNaN(25)); // false
try {
// code to run
catch(err) {
finally {
node file_name.js
console.log(myArray.includes(1)); // true
console.log(myArray.includes(7)); // false
2. Using .indexOf()
3. Using .find()
class Person {
constructor(name) {
this.name = name;
sayHi() {
console.log(`Hi ${this.name}`);
const user = {
age: 25
};
user.age = 25;
This is one of those nice and simple JavaScript coding questions to know.
function User(name) {
this.name = name;
}
let firstUser = new User('John Doe');
console.log(userName.charAt(0)); // "J"
34. What’s the Difference Between Var, Let, & Const Keywords?
Var: Declared variables are functionally scoped, or globally scoped when declared
outside a function. You can reassign new values and redeclare them.
Let: Declared variables are block-scoped, meaning they can only be accessed within
their declaration scope. It’s possible to reassign different values to them, but you
cannot redeclare them.
Const: These are constants, meaning they are block-scoped, and you cannot
reassign new values or redeclare them.
function makeMultiplier(multiplier) {
return function(x) {
return x * multiplier;
console.log(double(5)); // prints 10
console.log(triple(5)); // prints 15
(function sayHello () {
console.log("Hello World!")
}) ();
Can be accessed before and after the function is Created when execution reaches it, and can only be acces
defined. after definition.
44. How Will You Create, Read, & Delete a Cookie Using
JavaScript?
Creating a cookie:
To create a cookie, you must assign a string to the cookie property of the document.
Reading a cookie:
To read a cookie value, you call the cookie property of the document object.
Deleting a cookie:
Cookies are deleted by default when a user closes the browser. You can also set a
deletion time with an expiration date, and the expires parameter.
This is a type of function that can access variables within its parent scope, even after
the parent function has returned, as shown below.
Closures are often used in JavaScript for things like private variables and methods
and for factory functions.
function parentFunc(name) {
function childFunc() {
console.log(message);
return childFunc;
myArray = [];
console.log(myArray); // []
myArray.length = 0;
console.log(myArray); // []
3. Apply the pop method while the length is greater than zero
while(myArray.length > 0) {
myArray.pop();
console.log(myArray); // []
myArray.splice(0, myArray.length);
console.log(myArray); // []
Event Capturing: The event is handled by the outermost element, which is then
propagated to the innermost element
Event Bubbling: The event is handled by the innermost element first and then
propagated to the outermost element (reverse of event capturing)
50. What’s the Difference Between the Javascript Engine &
Javascript Runtime?
The JavaScript engine converts JavaScript code into machine code that can be
executed by the computer, while the JavaScript runtime is the environment in which
the code is executed, such as a web browser or Node.js
function removeDuplicates(arr) {
return arr.filter((elem,
console.log(removeDuplicates([1,2,2,3,3])); // [1, 2, 3]
2. Using forEach(): You need to create an empty array, then use forEach() as you
iterate through the array, adding elements to the empty array if it’s not already a
member.
function removeDuplicates(arr) {
let uniqueArray = [];
arr.forEach(element => {
if (!uniqueArray.includes(element)) {
uniqueArray.push(element);
});
return uniqueArray;
3. Using Set: The simplest approach, this is an inbuilt object for storing unique values
in an array.
console.log(uniqueArray);
myFunction();
This is one of those programming questions in JavaScript that you should know if
you’re a more experienced developer.
60. What Is Memoization in JavaScript?
Memoization is a technique to store results of expensive function calls and then
return cached results when the same inputs occur again. This helps to optimize
function performance by avoiding unnecessary re-computations and reducing time
complexity.