JOBSEEKER?

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

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 7  
 


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


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


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


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


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


6. Endangered Species

Selectors
   
Hard 

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

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

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'.

ECMAScript 7  
 


  •   Example case: Wrong answer
  •   Several species and continents: Wrong answer
  •   Continents with the same species: Wrong answer


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


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


8. 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 7  
 


  •   Example case: Exception
  •   Appending divs: Exception


9. Closest Relative

JavaScript Recursion Selectors
   
Hard 

The following HTML represents a family tree:

<James>
  <Dave></Dave>
  <Mike></Mike>
  <Sarah></Sarah>
</James>

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


  •   Example case: Exception
  •   Several generations: Exception
  •   Children with the same name: Exception


10. Customer List

DOM manipulation Event handling
   
Hard  

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. The email HTML element should not be present at first, and its HTML element should be added / removed when the name is clicked.

For example, the following code:

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

let customerParagraph = document.querySelectorAll("li > p")[0];
if(customerParagraph) {
  customerParagraph.click();
}
console.log(document.body.innerHTML);

Should render:

<div>
  <ul id="customers">
    <li>
      <p>John</p>
      <p>john@example.com</p>
    </li>
    <li>
      <p>Mary</p>
    </li>
  </ul>
</div>
ECMAScript 7  
 


  •   Example case: Exception
  •   Customer names are rendered correctly: Wrong answer
  •   Clicking customer names shows email: Wrong answer
  •   Clicking customer names again hides email: Wrong answer


11. Topic Coloring

Attribute manipulation CSS selectors DOM manipulation
   
Easy 

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:

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

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

<div>
  <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>
</div>
ECMAScript 7  
 


  •   Example case: Wrong answer
  •   The topic with the newest message is red: Wrong answer
  •   Function newMessage is called with some topicName(s) that do not exist: Wrong answer


12. Food Ranking

DOM manipulation Event handling
   
Hard 

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>
  <li><button>Up!</button>Taco<button>Down!</button></li>
  <li><button>Up!</button>Pizza<button>Down!</button></li>
  <li><button>Up!</button>Eggs<button>Down!</button></li>
</ol>`;

setup();

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.

ECMAScript 7  
 


  •   Example case: Wrong answer
  •   Elements can move up: Wrong answer
  •   Elements can move down: Wrong answer


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

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.
Dashboard Start Trial Sign In Home Tour Tests Questions Pricing For Jobseekers