+100 Common JavaScript Developer Interview Questions and Answers (Part 2)

Madeen
4 min readOct 26, 2022

--

Study these questions to make your interview easier.

Continuation of the previous story about +100 Common JavaScript Developer Interview Questions and Answers (Part 1). let’s continue listing the group of questions that helped to get a job and always help to succeed in most interviews for “Front-end developer / JavaScript developer”.

7- Why do you need modules?

There were a number of reasons why ECAM created “modules”. and the benefits of using modules in the JavaScript ecosystem.

  1. Maintainability: a capability that requires organization-wide coordination, since it relies on being able to search, reuse, and change other teams’ code.
  2. Reusability: also called software reuse, is the use of existing software, or software knowledge, to build new software.
  3. Namespacing: Namespace refers to the programming paradigm of providing scope to the identifiers (names of types, functions, variables, etc) to prevent collisions between them.

8- What is the scope in JavaScript?

The scope is the accessibility of variables, functions, and objects in some particular part of your code during runtime. In other words, scope determines the visibility of variables and other resources in areas of your code.

9- What is a Cookie?

A cookie is a piece of data that is stored on your computer to be accessed by your browser. Cookies are saved as key/value pairs. or Cookies are text files with small pieces of data — like a username and password — that are used to identify your computer as you use a computer network.

10- Why do you need a Cookie?

Cookies are used to remember information about the user profile(such as username). It basically involves two steps,

  1. When a user visits a web page, the user profile can be stored in a cookie.
  2. The next time the user visits the page, the cookie remembers the user profile.

11- What is the main difference between localStorage and sessionStorage?

LocalStorage is the same as SessionStorage but it persists the data even when the browser is closed and reopened(i.e it has no expiration time) whereas in sessionStorage data gets cleared when the page session ends.

12- What are the methods available for session storage?

// Save data to sessionStorage
sessionStorage.setItem("key", "value");

// Get saved data from sessionStorage
let data = sessionStorage.getItem("key");

// Remove saved data from sessionStorage
sessionStorage.removeItem("key");

// Remove all saved data from sessionStorage
sessionStorage.clear();

13- What is a promise?

A promise is an object that may produce a single value sometime in the future with either a resolved value or a reason that it’s not resolved(for example, network error). It will be in one of the 3 possible states: fulfilled, rejected, or pending.

const promise = new Promise(
(resolve) => {
setTimeout(() => {
resolve("I'm a Promise!");
}, 5000);
},
(reject) => {}
);

promise.then((value) => console.log(value));

14- Why do you need a promise?

Promises are used to handle asynchronous operations. They provide an alternative approach for callbacks by reducing the callback hell and writing cleaner code.

15- What are the three states of promise?

  1. Pending: This is an initial state of the Promise before an operation begins
  2. Fulfilled: This state indicates that the specified operation was completed.
  3. Rejected: This state indicates that the operation did not completed. In this case, an error value will be thrown.

16- What is a callback function?

A callback function is a function passed into another function as an argument. This function is invoked inside the outer function to complete an action. Let’s take a simple example of how to use callback function

function callbackFunction(name) {
console.log("Hello " + name);
}

function outerFunction(callback) {
let name = prompt("Please enter your name.");
callback(name);
}

outerFunction(callbackFunction);

17- What is a callback hell?

Callback Hell is an anti-pattern with multiple nested callbacks which makes code hard to read and debug when dealing with asynchronous logic. The callback hell looks like this below,

async1(function(){
async2(function(){
async3(function(){
async4(function(){
....
});
});
});
});

18- What is promise.all?

Promise.all is a promise that takes an array of promises as an input (an iterable), and it gets resolved when all the promises get resolved or any one of them gets rejected. For example, the syntax of promise.all method is below,

Promise.all([Promise1, Promise2, Promise3]).then(result) => {   console.log(result) }).catch(error => console.log(`Error in promises ${error}`))

Note: Remember that the order of the promises(output the result) is maintained as per input order.

19- What is a strict mode in javascript?

Strict Mode is a new feature in ECMAScript 5 that allows you to place a program, or a function, in a “strict” operating context. This way it prevents certain actions from being taken and throws more exceptions. The literal expression "use strict"; instructs the browser to use the javascript code in the Strict mode.

20- Why do you need strict mode?

Strict mode is useful to write “secure” JavaScript by notifying “bad syntax” into real errors. For example, it eliminates accidentally creating a global variable by throwing an error and also throws an error for assignment to a non-writable property, a getter-only property, a non-existing property, a non-existing variable, or a non-existing object.

21- How do you declare strict mode?

The strict mode is declared by adding “use strict”; to the beginning of a script or a function. If declared at the beginning of a script, it has global scope.

"use strict";
x = 3.14; // This will cause an error because x is not declared

and if you declare inside a function, it has local scope

x = 3.14; // This will not cause an error.
myFunction();

function myFunction() {
"use strict";
y = 3.14; // This will cause an error
}

Part 1

--

--