JavaScript Interview Questions and Answers

Last updated on Feb 07, 2024
  • Share
JavaScript Interview Questions

JavaScript is a high-level programming language that is known for its dynamic, prototype-based and weakly typed characteristics. It conforms to the ECMAScript specification and is one of the core technologies of the World Wide Web. Initially only implemented client-side, JavaScript is now embedded in other types of software, including server-side in web servers and non-web programs such as PDF software and word processors. This information is often asked in JavaScript interview questions.

 

JS Interview Questions

Here in this article, we will be listing frequently asked JavaScript Interview Questions and Answers with the belief that they will be helpful for you to gain higher marks. Also, to let you know that this article has been written under the guidance of industry professionals and covered all the current competencies.

Q1. What is the difference between let, var and const?
Answer

With JavaScript, the user can declare variables with three keywords, which are let, var, and const. The terms var let and const are somewhat confusing. We will explain all 3 variables with the help of examples.

The difference between the variables var, let as well as const is described below in different ways.

  • Scope
  • Hoisting
  • Reassign the value
  • Redeclaration of the variable

NOTE: If you want to read more about the difference between var, let as well as const then you can visit here.

Q2. How to check if value exists in array JavaScript?
Answer

1. Using includes()

const fruits = ["Banana", "Orange", "Apple", "Mango"];
console.log(fruits.includes("Mango"));

2. Using indexof()

const fruits = ["Banana", "Orange", "Apple", "Mango"];
if(fruits.indexOf("Orange") !== -1) {  
   alert("Yes, the value exists!")  
} else {
   alert("No, the value is absent.")  
}

Q3. What is the difference between arrow and normal functions?
Answer

The main differences between arrow function and normal function are based on some parameters like.

  • Syntax
  • Arguments that are binding.
  • "this" keyword is used to describe the use.
  • Utilizing a keyword that is new.
  • No duplicates of identified parameters.

Syntax of normal functions

// Function declaration
function printHello(name) {
   return `Hey ${name}`;
}

NOTE: If you want to read more about Regular vs Arrow functions then you can visit here.

// Function expression
const printHello = function(name) {
  return `Hey ${name}`;
}

Syntax of arrow functions

const printHello = (name) => `Hey ${name}`;

Q4. What would be the output of the code below?

var variable1 = ["red", "green", "blue", "yellow"];
var variable2 = null;
console.log('This is a type of - ', typeof(variable1));
console.log('This is a type of - ', typeof(variable2));

Answer

This is a type of -  object
This is a type of -  object

Q5. What are the data types available in JavaScript?
Answer

In JavaScript data types is an important concept that helps to operate on variables,JavaScript supports mainly two different kinds of data types-

  • Primitives
  • Objects
1. Primitives Data Type

It supports six data types that’s are given below.

  • Boolean: It represents only true or false based on condition.
  • Number: It can be written with or without decimals. Example var num1 = 32;
  • String: It is used to store texts and inside of either double or single quotes. Example var str_one = 'Best Interview Question';
  • Null: It is "nothing" but supposed to be something that does not exist.
  • Undefined: It has no value but variable is exists. Example var MyVariable_1;
    console.log(MyVariable_1); // undefined
  • Symbol: It is new in ES6. It is an immutable data type that have unique value.Example : const BestInterviewQuestion = Symbol(‘BestInterviewQuestion’);
2. Objects Data Type

It is a type of not primitive data Type. It is a collection of properties and these properties are stored in key and value pairs.

Q6. How to create an object in Javascript?
Answer

There are various ways of creating an Object in JavaScript.

  • With the Object():
    Example : var name = new Object();
  • With Object.create():
    Example : var name = Object.create(null);
  • With the bracket syntactig sugar:
    Example : var name = {};
  • With a function constructor:
    Example :
    var ObjectName = function(Vname) {
        this. Vname = Vname
    }
    var NewVar = new ObjectName("BestInterviewQuestion.com");
  • With function constructor + prototype:
    Example :
    function myObj(){};
    myObj.prototype.name = "bestinterviewquestion.com";
    var Newk = new myObj();
  • With ES6 class:
    Example :
    class myObject {
        constructor(name) {
            this.name = name;
        }
    }
    var e = new myObject("bestinterviewquestion.com");
Q7. What is the output of below code snippet?

const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; 
const bigNumbers = numbers.map(number => {
  return number * 2;
});
console.log(bigNumbers);

Answer

[ 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 ]

Q8. Is JavaScript client side or server side?
Answer

JavaScript is a client-side language it gets executed at the user side. Whenever we browse the web, all the files from JavaScript are first fetched from the server and then performed at our side by the browser.

Q9. What is a closure JavaScript?
Answer

It’s a JavaScript feature where an inner function has access to the enclosing or outer function’s variables, which is defined as a scope chain.

There are three types of scope chains:
  • Access to its own scope
  • Access to variables of outer functions
  • Access to global variables
Q10. What is JavaScript and why it is used?
Answer

JavaScript is a high-level programming language that is used to calculate, manipulate and validate data. With the help of JavaScript, we can update and change both HTML and CSS.

We can write JavaScript code in an HTML page. When a HTML page loaded with JavaScript code, the script is sent to the browser for manipulate then HTML or CSS code will be rendered. It is a client-side technology.

Development History of JavaScript

Invented by Brendan Eich in 1995, JavaScript became an ECMA standard in 1997. JavaScript was first to run on the Netscape 2 browser.

Latest Version: ECMAScript 2018 or ES9 is the latest version and was released in October 2018.

Advantages of JavaScript

  • Client-side JavaScript is quite fast.
  • The syntax is simple and flexible.
  • It develops well with most of the other popular languages.
  • Drag and drop components give a rich interface to your site.
  • Third party add-ons enable developers to write rich snippets.
Reviewed and verified by Umesh Singh
Umesh Singh

My name is Umesh Singh having 11+ experience in Node.JS, React JS, Angular JS, Next JS, PHP, Laravel, WordPress, MySQL, Oracle, JavaScript, HTML and CSS etc. I have worked on around 30+ projects. I lo...