JavaScript Interview Questions

Advanced and Basic JavaScript Interview Questions

Javascript is a scripting language(supports scripts) for Web pages but it is also used in non-browser environments as well. It is a powerful, lightweight, interpreted, scripting language with first-class functions (i.e. the language supports passing functions as arguments to other functions).

In order to add the dynamic interactivity to web pages Javascript is embedded within Hypertext Markup Language (HTML). Since it runs on the client side of the web so it can be used to design/program how the web pages behave on the occurrence of a particular event. That's the reason why it is widely used for the behavior of the web pages.

Read Advanced and Basic Javascript Interview Questions and Answers. We have listed some Javascript Developer interview questions with their answers that help you to crack javascript interviews

Read Best JavaScript Interview Questions

Download Advanced and Basic JavaScript Interview Questions PDF

Advanced and Basic JavaScript Interview Questions

In modern javascript let & const are different ways of creating variables. Earlier in javascript, we use the var keyword for creating variables. let & const keyword is introduced in version ES6 with the vision of creating two different types of variables in javascript one is immutable and other is mutable.
const: It is used to create an immutable variable. Immutable variables are variables whose value is never changed in the complete life cycle of the program.
let: let is used to create a mutable variable. Mutable variables are normal variables like var that can be changed any number of time.

There are three types of errors available in JavaScript

  • Load time errors: Errors which come up when loading a web page like improper syntax errors are known as Load-time errors and it generates the errors dynamically.
  • Run time errors: Errors that come due to misuse of the command inside the HTML language.
  • Logical Errors: These are the errors that occur due to the bad logic performed on a function which is having a different operation.

document.getElementsByClassName() method is used in Javascript to get an element with a class name.

Method NamegetElementsByClassName
ParameterString (name of class)
OutputArray of HTMLCollection that have inputted className

If we check in the program, every local scope has a connection with one or more scope in their back which forms a chain. This chain goes on until it met with the global scope which is the root of this hierarchy. As global scope doesn't have a parent, so it is on the top of the chain. This is known as scope chain.

The scope chain in JavaScript is basically used to resolve the values of the variable. Without this, it is difficult for a JavaScript to choose a certain value for a variable if there are many variables defined at different scopes.

MUL means simple multiplication of numbers. It is a techique in which you pass a one value as an argument in a function and that function returns another function to which you pass the second value and the process go on. For example: x*y*z can be representing as:

function mul (x) {  
return function (y) { // anonymous function    
return function (z) { // anonymous function      
return x * y * z;    };

There are many ways to reverse a string in JavaScript. These are:

Using in-built functions: the inbuilt function reverse() reverses the string directly. Here’ how:

str = str.split(""); //convert 'jQuery' to array
str = str.reverse(); //reverse 'jQuery' order 
str = str.join(""); //then combines  the reverse order values.

First split the string to an array, then reverse an array and after that join the characters to form a string.

Using a loop: First, count a number of characters in a string, then apply a decrementing loop on an original string which starts from the last character and prints each character until count becomes zero.

There are several ways to redirect page to another page in JavaScript. These are:

  1. Using location.href: It is the first approach to redirect page. In this, we can go back to access the original document. Syntax:window.location.href =“”
  2. Using location.replace: Another approach to redirect page. In this, it is not possible to navigate back to the original document by clicking on the back button as it removes the URL of the original document. Syntax: window.location.replace(";");

The design pattern is a general reusable solution to a commonly occurring problem in software design. Some of the design patterns are:

  1. Creational design pattern: These patterns dealt with the mechanism of object creation which optimize object creation with the basic approach.
  2. Structural design pattern: these patterns deal with different classes and objects to provide new functionality.
  3. Behavioral Patterns: These patterns are to improve communication between objects and to recognize patterns.
  4. Concurrency design patterns: These patterns handle with multi-thread programming paradigms.
  5. Architectural design patterns: These patterns used to deal with architectural designs.
  • The array.slice() removes items from the array and then return those removed items as an array whereas array.slice() method is selected items from an array and then those elements as a new array object.
  • The splice() method affects the original array whereas slice() method doesn’t affect the original array.
  • Splice() method takes n number of arguments whereas slice() can take only two arguments.

Syntax of splice(): array.splice(index, howmany, item1, ....., itemX)

Syntax of slice(): array.slice(start, end)

You can add a property to an object using object.property_name =value, delete object.property_name is used to delete a property.


    let user = new Object();
    // adding a property'Anil';
    user.age  =25;
    delete user.age;

A promise is an object in JavaScript which is used to produce a value that may give result in the future. The value can be resolved value or it can be a reason which tells why the value is not resolved.

A promise can be of three states:

  • Fulfilled: The operation is completed and the promise has a specific value.
  • Rejected: The operation is failed and promise has a reason which shows why the operation failed.
  • Pending: Th operation is not fulfilled or rejected, means it has not completed yet.

There are many ways to remove duplicates from JavaScript array. These are described below with examples:

1. By using Set: It is the simplest approach to remove duplicates. Set is an inbuilt object to store unique values in an array. Here's how we use set:

    function uniquearray(array) { 
       let unique_array= Array.from(set(array)) 
       return unique_array;}

As in the above code, you created a set of an array which automatically eliminates the duplicate values.

2. By using Filter: Another approach to remove duplicates from an array is applying filter on an array. To call filter method, it requires three arguments: array, current element, index of current element. Here’s how we use filter:

function unque_array (arr){
 let unique_array = arr.filter(function(elem, index, self) {
 return index == self.indexOf(elem); } 
return unique_array }

3. By using for loop: In this, we can use for loop to remove duplicates. In this we make an empty array in which those elements will be added from the duplicate array which are not present in this before. Thus, finally we will get an array which has unique elements. Code to implement this:

Array dups_names = ['Ron', 'Pal', 'Fred', 'Rongo', 'Ron'];
function dups_array(dups_names) {
 let unique = {};
 names.forEach(function(i) {
    If (!unique[i]) {
      unique[i] = true;    }
return Object.keys(unique);}   // Ron, Pal, Fred, Rongo

These are the main three methods used in JavaScript to get a unique array.

Null is a value that can be assigned to a variable or an object.

Undefined means a variable has been declared but no value is assigned to it. This type of variable is declared itself to be undefined.

Undeclared means the variable has declared without any datatype.

Null, Undefined are primitive data types whereas Undeclared is not a primitive data type.

  • The primitive data types are numbers, strings, Boolean, undefined, null and anything other than these data types are known as non-primitive such as objects and functions.
  • Primitive data types are immutable while non-primitives are mutable.
  • Primitives are known immutable as they can't be changed once they created but non-primitive are changeable, means once an object is created, it can be changed.
  • Primitives data types are compared with their values, it means two values are strictly equal if they have the same data type and holds the same value.
  • Non-primitives are not compared with values. For example, if two objects have the same properties and values, they are strictly not equal.

It is easy to add a new property in existing function by just giving value to the existing function it. For example, let we have an existing object person, to give new property check the below code: “India”;

The new property “country” has added to the object person.