JavaScript Interview Questions

Practice for JavaScript interviews by solving TestDome questions. Our interview questions are used by more than 5,000 companies and 450,000 individual test takers.

Jobseekers: Certify Your Knowledge

Take a Certification Test

Companies: Use Our Tests for Screening

Buy a Pack Of Candidates

Try to solve 7 JavaScript interview questions and Front End interview questions below. Hints can help you find answers to questions you are having trouble with.

1. Ensure

Error handling Language
   
Easy  

Implement the ensure function so that it throws an error if called without arguments or the argument is undefined. Otherwise it should return the given value.

ECMAScript 6  
 


  •   No argument throws an error: Wrong answer
  •   Truthy argument returns argument: Wrong answer
  •   Falsy argument is treated differently from undefined: Wrong answer


2. Remove Property

Language Objects
   
Easy  

Implement the removeProperty function which takes an object and property name, and does the following:

If the object obj has a property prop, the function removes the property from the object and returns true; in all other cases it returns false.

ECMAScript 6  
 


  •   Property removed: Wrong answer
  •   Correct return value: Wrong answer


3. Date

Strings
   
Easy  

Write a function that converts user entered date formatted as M/D/YYYY to a format required by an API (YYYYMMDD). The parameter "userDate" and the return value are strings.

For example, it should convert user entered date "12/31/2014" to "20141231" suitable for the API.

ECMAScript 6  
 


  •   Example case: Wrong answer
  •   Two-digit month and day: Wrong answer
  •   One-digit day: Wrong answer
  •   One-digit month: Wrong answer


4. Image Gallery

DOM manipulation Event handling Selectors
   
Easy  

An image gallery is a set of images with corresponding remove buttons. This is the HTML code for a gallery with two images:

<div class="image">
  <img src="https://goo.gl/kjzfbE" alt="First">
  <button class="remove">X</button>
</div>
<div class="image">
  <img src="https://goo.gl/d2JncW" alt="Second">
  <button class="remove">X</button>
</div>

Implement the setup function that registers a click event handler and implements the following logic: When the button of class remove is clicked, its parent <div> element should be removed from the gallery.

For example, after the first image has been removed from the gallery above, it's HTML code should look like this:

<div class="image">
  <img src="https://goo.gl/d2JncW" alt="Second">
  <button class="remove">X</button>
</div>
ECMAScript 6  
 


  •   Example case: Wrong answer
  •   Remove single image: Wrong answer
  •   Remove multiple images: Wrong answer


5. Check Digit

Strings
   
Easy  

Your company assigns each customer a membership ID, and you are implementing a check digit for those IDs.

The check digit should be calculated by adding up all digits in each membership ID. If the result of the sum is a number with more than a single digit, another iteration is required, and the digits of the result also should be added together. This process should repeat until a single-digit number is calculated.

For example, for the membership ID "55555" the sum of all digits is 25. Because this is not a single-digit number, 2 and 5 would be added, and the result, 7, would be the check digit.

ECMAScript 6  
 


  •   Example case: Wrong answer
  •   Single iteration required: Wrong answer
  •   Two iterations required: Wrong answer
  •   Multiple iterations required: Wrong answer


6. Closures

Bug fixing Closures
   
Hard  

Fix the bugs in the registerHandlers function. An alert should display anchor's zero-based index within a document instead of following the link.

For example, in the document below, the alert should display "2" when Google anchor is clicked since it is the third anchor element in the document and its zero-based index is 2.

<body>
  In my life, I used the following web search engines:<br/>
  <a href="//www.yahoo.com">Yahoo!</a><br/>
  <a href="//www.altavista.com">AltaVista</a><br/>
  <a href="//www.google.com">Google</a><br/>
</body>
ECMAScript 6  
 


  •   Example case: Wrong answer
  •   Duplicate links: Wrong answer
  •   Various links: Wrong answer


7. Loop

Bug fixing DOM manipulation
   
Hard  

Function appendChildren should add a new child div to each existing div. New divs should be decorated by calling decorateDiv.

For example, after appendChildren is executed, the following divs:

<div id="a">
  <div id="b">
  </div>
</div>

should take the following form (assuming decorateDiv does nothing):

<div id="a">
  <div id="b">
    <div></div>
  </div>
  <div></div>
</div>

The code below should do the job, but for some reason it goes into an infinite loop. Fix the bugs.

ECMAScript 6  
 


  •   Example case: Exception
  •   Appending divs: Exception


If you feel ready, take one of our timed public JavaScript Interview Questions tests:
HTML/CSS

HTML/CSS and JavaScript Online Test (Easy / Hard)

HTML/CSS, JavaScript, and Bootstrap Online Test (Easy / Hard)

HTML/CSS, JavaScript, and React Online Test (Easy / Hard)

HTML/CSS, JavaScript, C# Algorithms, and SQL Online Test (Easy / Hard)

HTML/CSS, JavaScript, C#, and SQL Online Test (Easy / Hard)

HTML/CSS, JavaScript, Java Algorithms, and SQL Online Test (Easy / Hard)

HTML/CSS, JavaScript, Java, and SQL Online Test (Easy / Hard)

HTML/CSS, JavaScript, Node.js, and SQL Online Test (Easy / Hard)

HTML/CSS, JavaScript, PHP, and SQL Online Test (Easy / Hard)

HTML/CSS, JavaScript, Python Algorithms, and SQL Online Test (Easy / Hard)

HTML/CSS, JavaScript, Python, and SQL Online Test (Easy / Hard)

HTML/CSS, JavaScript, Ruby, and SQL Online Test (Easy / Hard)

HTML/CSS, JavaScript, Scala, and SQL Online Test (Easy / Hard)

HTML/CSS, JavaScript, VB.NET, and SQL Online Test (Easy / Hard)

JavaScript

JavaScript Online Test (Easy / Hard)

JavaScript and Node.js Online Test (Easy)

JavaScript and SQL Online Test (Easy / Hard)

JavaScript, Node.js, and SQL Online Test (Easy / Hard)

ASP.NET (Core) MVC

ASP.NET (Core) MVC, HTML/CSS, JavaScript, C#, and SQL Online Test (Easy / Hard)

ASP.NET Web Forms

ASP.NET Web Forms, HTML/CSS, JavaScript, C#, and SQL Online Test (Easy / Hard)

TypeScript

TypeScript and JavaScript Online Test (Easy / Hard)

Not exactly what you are looking for? Go to our For Jobseekers section.