27 JavaScript Interview Questions and Answers

27 JavaScript Interview Questions and Answers for 2019

Whether you’re an entrepreneur, a project manager in a large enterprise, or a CTO, you are well aware that the
success of your JavaScript project rests on your ability to find top developers.

In this guide, you‘ll find example interview questions and answers you can refer to when seeking a new JavaScript
developer to make your dynamic user interfaces come to life. You‘ll also get some practical advice on how to use
these questions to reliably identify the best people for the job.

First Things First: Select Your Job Requirements

The biggest mistake you can make as an interviewer is not knowing exactly what kind of JavaScript developer you are
looking before you sit down with your candidates. And no, “a great JavaScript developer with lots of experience” is
not specific enough.

You need to be able to reliably identify the right candidates capable of completing the JavaScript-specific tasks
for your project with your team. To do that, you need to figure out your specific job requirements.

Some example requirements for a JavaScript developer include:

  • Essential web development skills – HTML, CSS
  • JavaScript-specific skills – Libraries and toolkits, testing code
  • Design skills – Building scalable applications, security
  • Communication skills – Discussing problems and constraints
  • Initiative – Figuring out solutions by themselves

Avoid making a laundry list of non-essential skills for your perfect JavaScript developer. Instead, focus on what
your candidate will actually be doing day-to-day. Keep your requirements list as short as possible. Cut anything
they can do without or learn on the job.

With clearly stated requirements, you‘ll be able to choose the right JavaScript coding interview questions and
have a much better idea of what kinds of responses you are looking for.

Hiring now? Get top-rated JavaScript developers and dev teams from our community

The questions in this guide are broken down into two sections: Junior Developer Interview Questions and Senior Developer Interview Questions. You should use the Junior Developer JavaScript programming interview questions in this section if you‘re looking for a junior or entry level developer with less experience.

Skip to the Senior Developer sections for JavaScript interview questions and answers for experienced developers).

JavaScript Junior Developer Interview Questions

A Junior developer is someone just starting their career with less than 2 years of experience. They demand the
lowest salary, but they‘ll need guidance from more experienced JavaScript developers who can delegate tasks and
cross-check their code.

Skill Requirements for Junior JavaScript Developers

  • Essential web technologies (HTML, CSS)
  • Foundational JavaScript knowledge
  • Learning on the job
  • Following instructions and receiving feedback
  • Thinking like a programmer

Example JavaScript Junior Developer Interview Questions and Answers

Note: Important keywords are underlined in the answers. Bonus points if the candidate mentions them!

Or save yourself time and request a team to match your needs right away.

Question 1: What is JavaScript?
Requirement: Foundational JavaScript Knowledge

Answer: JavaScript is a high-level, object-based, dynamically typed, weakly typed, interpreted,
multi-paradigm programming language. It is one of the three core web technologies alongside HTML and CSS. It was
originally a client-side scripting language but can also be used server-side.

Question 2: What are the JavaScript Data Types?
Requirement: Foundational JavaScript Knowledge

Answer: Number, String, Boolean, Function, Object, Undefined

Question 3: Describe the difference between undeclared and undefined variables.
Requirement: Foundational JavaScript Knowledge

Answer: Undeclared variables have not been declared in the scope of the program and don‘t exist. If a JavaScript program tries to read an undeclared variable, a runtime error will occur.

Undefined variables have been declared but haven‘t been assigned a value. If a JavaScript program tries to read an undefined variable there won‘t be an error, but an undefined value will be returned.

Question 4: Write some example JavaScript code that creates a new HTML <p> element with some text, and appends it to the document body. Use jQuery syntax if needed.
Requirement: Foundational JavaScript Knowledge

Answer:

    var para = document.createElement("P"); // Create a <p> element
    var t = document.createTextNode("This is a paragraph"); // Create a text node
    para.appendChild(t); // Append the text to <p>
    document.body.appendChild(para); // Append <p> to <body>

Question 5: Write an example definition of a JavaScript object for a ’person‘. The object should have a first and last name, an id number, and a function to retrieve the full name of the person.
Requirement: Foundational JavaScript Knowledge

Answer: Here you‘re looking for basic knowledge of how to turn spoken requirements into working JavaScript code. Look out for things like naming conventions, neat code structure, correct use of keywords, correct semicolon use, and syntax errors. A acceptable answer should look something like this:

    var person = {
           firstName : "John",
           lastName : "Doe",
           id        : 0001,
           fullName : function() {
                  return this.firstName + " " + this.lastName;
           }
     }

Question 6: Describe what ’dynamic typing‘ means for JavaScript. Give code examples.
Requirement: Foundational JavaScript Knowledge

Answer: Dynamic typing means that the type of variables can change at runtime. For example, this is perfectly ok in JavaScript:

    n = 1;
    n = “Some string”;

This is opposed to static typing, where the programmer must explicitly state the type of each variable and conversions to unrelated types must be done explicitly.

Question 7: Write an example of a simple HTML document with some header information and some page content.
Requirement: Basic HTML skills

Answer: HTML documents are different, but they follow a basic structure of head and body. The different sections are marked with tags such as DOCTYPE, html, head, title, meta, body, h1, p.

For example:

     <!DOCTYPE html>
     <html>
       <head>
         <title>Page Title</title>
         <meta charset="UTF-8">
         <meta name="description" content="Page description">
       </head>
       <body>
         <h1>Interview Example Web Page</h1>
         <p>Some content goes here</p>
       </body>
     </html>

Question 8: Explain JavaScript callback functions and provide a simple example.
Requirement: Basic JavaScript skills

Answer: A callback function is a function that is called after another function has finished executing. A callback function is passed to another function as an argument and is invoked after some operation has been completed. For example:

     function modifyArray(arr, callback) {
       arr.push(100);
       callback();
     }

     var arr = [1, 2, 3, 4, 5];
     modifyArray(arr, function() {
       console.log("array has been modified", arr);
     });

Question 9: How would you learn about a new JavaScript library?
Requirement: Learning on the job

Answer: Web development is changing all the time, and developers need to be able to learn constantly. Here you are finding out how the candidate approaches the learning process. You want to see that they have an idea of what information they will need and where to find it. For JavaScript libraries, that means looking up online tutorials and digging into the documentation.

Question 10: Describe a time you received feedback on a programming task. How did it help you become a better programmer?
Requirement: Following instructions and receiving feedback

Answer: This is a typical open-ended question. The candidate should demonstrate they can accept, understand and act on feedback.

Question 11: What will be the output when the following code is executed? Explain.

     console.log(false == '0')
     console.log(false === '0')

Answer:

true

false

Explanation: == equal to, === equal to with datatype check

JavaScript Senior Developer Interview Questions

Here are some more advanced and technical interview questions and answers for experienced JavaScript developers. Use them to pick out the right JavaScript developers with the skills to build your web app.

Modern web development is all about understanding how to use tools, libraries and toolkits and integrate many technologies together. Expert developers need to know more than just how to write excellent JavaScript code. They need to know how that code will work in the context of the application and the entire web.

For this reason, you should include a lot of open-ended, ’messy‘ interview questions. These should spark a discussion and you should tailor them to fit your own job requirements. Don‘t be afraid to ask follow-up questions!

Skill Requirements for Senior JavaScript Developers

  • Expert JavaScript knowledge
  • Using a range of web technologies
  • Designing for specific requirements (e.g. security, scalability)
  • Node.js and server-side JavaScript
  • Maintaining and upgrading JavaScript applications
  • Experience in frameworks/toolkits/libraries you use
  • Efficient programming and clean code
  • Debugging
  • Automated testing
  • Leadership skills
  • Clear communication skills
  • Mentoring less experienced developers

Example JavaScript Senior Developer Interview Questions and Answers

Note: Important keywords are underlined in the answers. Look out for them in interviews!

Question 12: List some advantages and disadvantages of JavaScript?
Requirement: Expert JavaScript knowledge

With this question, you‘re looking for knowledge of how programming languages work, and why there are always tradeoffs to any specific web technology or language.

Answer:

Advantages:

  • Being able to compile and run immediately within the browser makes it fast in many cases
  • It‘s interoperable with many other languages and can be inserted into any webpage
  • A huge number of tools, libraries and frameworks are available
  • Most programmers know at least some JavaScript and extensive resources available to learn JavaScript
  • It‘s a highly versatile language
  • You can use JavaScript as a server-side language with Node.js
  • Allows web pages to have interactive elements and interfaces

Disadvantages:

  • JavaScript has some security vulnerabilities. Code compiled and run on the client-side can be exploited by attackers
  • Features like dynamic typing can make JavaScript code prone to errors
  • JavaScript may run differently on different browsers

Question 13: What will be the output of the following code?
Requirement: Expert JavaScript knowledge

        var foo = true;
        console.log(!foo);
        console.log(foo + 0);
        console.log(foo + "xyz");
        console.log(foo + false);
        console.log(foo && foo);

Answer: Here you‘re looking for knowledge of how variable typing and the logical operators work in JavaScript. It will output the following to the console:

        true
        1
        truexyz
        1
        true

Question 14: What will the following code output? Why?
Requirement: Expert JavaScript knowledge

        function foo(){
          function bar() {
            return ’a‘;
          }
          return bar();
          function bar() {
            return ’b‘;
          }
        }
        console.log(foo());

Answer: Here you‘re testing detailed knowledge of a critical JavaScript concept called Hoisting.

Counterintuitively, the program will output b. This is because both of the bar() functions in the code snippet will be hoisted to the top of the scope. The second one returning the string ’b‘ will be hoisted after the first, so that function will be called in the return statement.

Question 15: What is ‘this’ keyword in JavaScript? How do you use it?
Requirement: Expert JavaScript knowledge

Answer: The ‘this’ keyword refers to the object from where it was called. In their answer, the candidate should tell about call, bind, and apply.

The functions .call() and .apply() are very similar in their usage, except for one small difference. .call() is used when the number of the function’s arguments are known to the programmer, as they have to be mentioned as arguments in the call statement. On the other hand, .apply() is used when the number is not known. The function .apply() expects the argument to be an array.

.bind returns a new function(does not invoke it immediately) with the new context

Question 16: How do you explain closures in JavaScript? When are they used?
Requirement: Expert JavaScript knowledge

Answer: A closure is a locally declared variable related to a function which stays in memory when the function has returned. Take a look here for a deeper understanding: https://medium.com/dailyjs/i-never-understood-javascript-closures-9663703368e8

For example:

          function greet(message) {
            console.log(message);
          }

          function greeter(name, age) {
            return name + " says howdy!! He is " + age + " years old";
          }

          // Generate the message
          var message = greeter("James", 23);
          // Pass it explicitly to greet
          greet(message);

          This function can be better represented by using closures
          function greeter(name, age) {
            var message = name + " says howdy!! He is " + age + " years old";
            return function greet() {
              console.log(message);
            };
          }
          // Generate the closure
          var JamesGreeter = greeter("James", 23);
          // Use the closure
          JamesGreeter();

Question 17: How do you copy an object by reference? How do you copy one by value?
Requirement: Expert JavaScript knowledge

Answer: 

          a = {'w': 1}
          b = a   // copies reference to an object
          c = Object.assign({},a)  // creates new object which copies the value of A

Question 18: Why is namespacing important when coding in JavaScript? Give an example of how you would implement namespacing to group this sample configuration data:
Requirement: Namespacing

          var DEBUG = true;
          var MAX_SIZE = 100;
          var FEED_URL = "http://myfeed.com/feed/";
          var POST_IDS = [1, 2, 3];

Answer:  Namespacing is used to group things like variables, functions and objects together with unique names. It helps to avoid naming conflicts, logically separate code and make code more reusable.

Namespacing in JavaScript is often done using objects. For the example above:

          var Config = {
                 DEBUG: true,
                 MAX_SIZE: 100,
                 FEED_URL: "http://myfeed.com/feed/",
                 POST_IDS: [123, 456, 789]
          };

A variable in this namespace can be accessed like so:

          Config.MAX_SIZE;

Question 19: Give some ideas to implement automated testing on our JavaScript code?
Requirement: Testing JavaScript applications

Answer: Automated testing is essential when building robust applications. Great developers are keenly familiar with and can implement:

  • Unit testing – Testing individual functions and classes
  • Integration testing – Testing that components work together as expected
  • UI/Functional testing – Testing the interface and functioning of the actual application

Some examples of JavaScript testing tools include Jest, Mocha, Jasmine, Karma, Cucumber, Chai, Unexpected, enzyme, Ava, Protractor, Casper, Phantom, and many more.

Question 20: What are your favorite JavaScript frameworks and technology stacks? Why?
Requirement: Using a range of web technologies

Answer: Effective web development requires using and combining the many different technologies available. Here you‘re getting an idea of your candidates depth and breadth of experience in these tools. You should probe for specific technical details about the answers.

Question 21: What architectures and design patterns have you used for building JavaScript applications? What were the tradeoffs?
Requirement: Web application design

Answer: This question is about architecture and design patterns and is distinct from the question above. It‘s for senior candidates that will be part of the design process for your application. Ideally, a candidate will have experience working with multiple software architectures and understand the tradeoffs of each.

Some examples they might talk about include model-view-controller pattern, flux architecture, n-layered, microkernel architecture, microservices, monolithic, client-server, peer-to-peer, master-slave pattern, pipe-filter pattern, broker pattern, event-bus pattern, interpreter pattern,

Question 22: What‘s your experience with using server-side JavaScript?
Requirement: Node.js and server-side JavaScript

Answer: You‘ll need to ask this if your job requires server-side JavaScript using Node.js. Server-side JavaScript requires different skills to client-side.

Question 23: If you could use whatever tools you like to build our ______ application, what would you use?
Requirement: Design skills, understanding requirements

Answer: In this question, you are getting a feel for the type of developer you are talking to and how they like to code. You are especially looking for developers that try to understand the requirements first. It‘s a big red flag if a developer gives a list of libraries and tools without understanding the task. Take note to see if their answer is the same as their ’favorite‘ technology stack.

Question 24: How are you involved in the JavaScript/Web development community?
Requirement: Passion for web development

Answer: This is a very popular question for coding interviews because community and open source involvement are clear indicators of a passionate developer.

Question 25: Describe a time you fixed a particularly difficult error in a web application. How did you approach the problem? What debugging tools did you use? What did you learn from this experience?
Requirement: Debugging, Breaking down a problem into parts

Debugging is one of the key skills for any web developer. However, the real skill is in breaking the problem down in a practical way rather than finding small errors in code snippets. Debugging often takes hours or even days, so you don‘t have time in an interview setting. Asking these questions will give you an idea of how your candidate approaches errors and bugs.

Answer: In the candidate‘s response you should look out for things like:

  • A measured, scientific approach
  • Breaking down the problem into parts
  • Finding out how to reproduce the error
  • Expressing and then testing assumptions
  • Looking at stack traces
  • Getting someone else to help/take a look
  • Searching the internet for others that have had the same problem
  • Writing tests to check if the bug returns
  • Checking the rest of the code for similar errors
  • Turn problems into learning experiences

Question 26: What’s the most important thing to look for or check when reviewing another team member’s code?
Requirement: Mentoring less experienced developers, Leadership skills

Answer: Here you‘re checking for analysis skills, knowledge of mistakes that less experienced developers make, keeping in mind the larger project and attention to detail.

A good answer might mention code functionality, readability and style conventions, security flaws that could lead to system vulnerabilities, simplicity, regulatory requirements, or resource optimization.

Question 27: What tools & practices do you consider necessary for Continuous Integration and Delivery for a web application?
Requirement: DevOps systems design, Maintaining and upgrading applications

Summary

Hiring the right people for your development team is critical to the success of your project. Remember that you should not be aiming to hire the perfect JavaScript developer, but rather the right person for the job at hand.

With the help of this information and sample interview questions on JavaScript, you can ensure that the hiring process goes smoothly and successfully – allowing you to hire a great programming team to get your project completed on time and on budget.

Finally, here is a BONUS for you:
Click here to download a ready-to-use question and answer sheet that you can simply print out and bring with you to interview your candidates.

Happy Hiring!