JavaScript Interview Questions

Practice for JavaScript interviews by solving TestDome questions. Our interview questions are used by more than 7,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

Need to practice your JavaScript programming skills for an upcoming job interview? Try solving these JavaScript interview questions that test knowledge of JavaScript programming concepts such as closures, DOM manipulation, event handling, and other skills. We’ll provide feedback on your answers, and you can use a hint if you get stuck.

These JavaScript interview questions are examples of real tasks used by employers to screen job candidates such as JavaScript developers, front-end developers, full-stack developers, and others that require knowledge of the JavaScript programming language and its application in web development.

1. Ensure

JavaScript Error handling Language Public

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.

2. Even Numbers

JavaScript Attribute manipulation Bug fixing Selectors Styling New Public

A Mathematics tutoring web application for kids has a button that highlights all even numbers (in span), using 'yellow' as the background color. Clicking the button again removes the background. All further clicks add or remove the 'yellow' background alternately.

Currently, the button doesn't work as expected. For the given HTML markup and JavaScript function, select all options that will identify and fix the bugs in the JavaScript function:

<div id='numbers'>
 <button id='btn' onclick='toggleEvenColor()'>Toggle even number highlighting</button>
1: function toggleEvenColor() {
2:   let spanElements = document.querySelectorAll('#numbers span');
3:   spanElements.forEach(function() {
4: = 'transparent';
5:   });
6: }

(Select all acceptable answers.)

3. Remove Property

JavaScript Language Objects Public

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.

4. Date

JavaScript Strings Public

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.

5. Image Gallery

JavaScript DOM manipulation Event handling Selectors Public

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="" alt="First">
  <button class="remove">X</button>
<div class="image">
  <img src="" alt="Second">
  <button class="remove">X</button>

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="" alt="Second">
  <button class="remove">X</button>

6. Check Digit

JavaScript Strings Public

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.

7. Song

JavaScript Algorithmic thinking Linked list Set New Public

A playlist is considered a repeating playlist if any of the songs contain a reference to a previous song in the playlist. Otherwise, the playlist will end with the last song which points to undefined.

Implement the method isRepeatingPlaylist that, efficiently with respect to time used, returns true if a playlist is repeating or false if it is not.

For example, the following code prints "true" as both songs point to each other.

let first = new Song("Hello");
let second = new Song("Eye of the tiger");

first.nextSong = second;
second.nextSong = first;


8. Endangered Species

JavaScript Selectors New Public

An HTML div element contains lists of endangered species grouped by continent and the species population status.

    <ul data-continent="North America">
        <li data-species="California condor">Critically Endangered</li>
        <li data-species="American bison">Near Threatened</li>
    <ul data-continent="Europe">
        <li data-species="Cave bear">Extinct</li>

Implement the function endangeredSpecies that returns how endangered a species is on a particular continent.

For example, endangeredSpecies('North America', 'American bison') should return 'Near Threatened'.

9. Closures

JavaScript Bug fixing Closures Public

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.

  In my life, I used the following web search engines:<br/>
  <a href="//">Yahoo!</a><br/>
  <a href="//">AltaVista</a><br/>
  <a href="//">Google</a><br/>

10. Loop

JavaScript Bug fixing DOM manipulation Public

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">

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

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

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

11. Closest Relative

JavaScript JavaScript Recursion Selectors New Public

The following HTML represents a family tree:


Implement the closestRelative function so that when a parent HTML element is passed, the function returns the parent's closest relative whose name matches the relativeName, and obeys the following rules:

  • The parent parameter is the HTML element of which the closest relative will be a descendant.
  • Each member of the family, including children, may also be a parent to one or more children.
  • Children are more closely related to the parent than grandchildren.
  • If several children in the same generation have the same name, then the first child in the tree is considered the closer relative.
  • If no matching relative is found the function should return null.

For example, the below code should print 'MIKE' for the given family tree:

let parent = document.getElementsByTagName('James')[0];
let relative = closestRelative(parent, 'Mike');
console.log(relative && relative.tagName); // prints MIKE

12. Customer List

JavaScript DOM manipulation Event handling New Public

Implement the showCustomers function so that it renders customers as list items. The first argument to the function, customers, is an array of objects with the name and email properties. The second argument to the function, targetList, is an unordered HTML list to which each customer should be added as a separate list item.

The name and email properties should be added as two paragraphs inside the list item. At first, the email paragraph element should not be present in the DOM. The email paragraph element should be added to the DOM after the name is clicked, and it should be removed from the DOM when the name is clicked again.

For example, the following code:

document.body.innerHTML = `
  <ul id="customers">
let customers = [{name: "John", email: ""},
                 {name: "Mary", email: ""}];
showCustomers(customers, document.getElementById("customers"));

let customerParagraph = document.querySelectorAll("li > p")[0];
if(customerParagraph) {;

Should render:

  <ul id="customers">

13. Topic Coloring

JavaScript Attribute manipulation CSS selectors DOM manipulation New Public

Write the function newMessage, which receives the name of the topic as the parameter. Function should change the background color of the p tag to red where the data-topic-name is topicName. A topicName that doesn't have a matching data-topic-name should be ignored.

For example, if the HTML is:

  <p data-topic-name="discussion">General discussion</p>
  <p data-topic-name="bugs">Bugs</p>
  <p data-topic-name="animals">Animals</p>

After calling newMessage("discussion") the HTML should be:

  <p data-topic-name="discussion" style="background-color: red;">General discussion</p>
  <p data-topic-name="bugs">Bugs</p>
  <p data-topic-name="animals">Animals</p>

14. Two Sum

JavaScript Algorithmic thinking Dictionary Public

Write a function that, when passed an array and a target sum, returns, efficiently with respect to time used, two distinct zero-based indices of any two of the numbers, whose sum is equal to the target sum. If there are no two numbers, the function should return null.

For example, findTwoSum([ 3, 1, 5, 7, 5, 9 ], 10) should return an array containing any of the following pairs of indices:

  • 0 and 3 (or 3 and 0) as 3 + 7 = 10
  • 1 and 5 (or 5 and 1) as 1 + 9 = 10
  • 2 and 4 (or 4 and 2) as 5 + 5 = 10

15. Food Ranking

JavaScript DOM manipulation Event handling New Public

A website needs a list where users can rank their favorite foods. Write the setup function, which should register click handlers on all Up! and Down! buttons. The Up! button should move the list item one place up in the list, while Down! button should move the list item one place down in the list.

For example, consider this code:

document.body.innerHTML = `<ol>


If the button Up! button in Pizza list item is clicked, Pizza should be the first item in the list, while Taco should be the second item.

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

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)

Web Developer Online Test (Easy / Hard)


JavaScript Online Test (Easy / Hard)

JavaScript Algorithms Online Test (Easy)

JavaScript Algorithms and Node.js Online Test (Easy)

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

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

JavaScript and SQL Online Test (Easy / Hard)

JavaScript with jQuery Online Test (Easy / Hard)

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


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 and JavaScript Online Test (Easy / Hard)

Not exactly what you are looking for? Go to our For Jobseekers section.
Dashboard Start Trial Sign In Home Tour Tests Questions Pricing For Jobseekers