Book a demo

Whether you’re a senior or junior JavaScript developer, you’ll probably be invited to a coding interview at least once in your career. If you’re facing your first interview, you may feel insecure because you don’t know what to expect or how to prepare. 

That’s why in this blog post, we’re going to share five JavaScript coding questions and some preparation tips to help you pass your interview with flying colors.

Question 1: What will be the output of this code?

Questions like these are popular with recruiters because they help test your JavaScript knowledge with realistic examples. Typically, you’re given a code snippet and must predict its output before executing it and seeing the result. 

For example, what’ll be the output of this code?

console.log(0.1 + 0.2);
console.log(0.1 + 0.2 == 0.3);

 

A suitable answer might be that you can’t be sure of the result because the number type in JavaScript represents floating-point numbers, which make arithmetics subject to rounding.  

A look at the actual output shows that we’re right:

0.30000000000000004
false

 

Remember that a coding interview isn’t just about stating the correct outcomes. Interviewers also want to know how you solve problems and how you communicate your thought processes. And questions like these are great for that.

Extra reading: Tips on How to Prepare for a Tech Assessment Test

Question 2: What do you know about scopes?

Scope is an important topic that can be troubling for some JavaScript developers. To test your knowledge, recruiters may ask open-ended questions like the one in the “What do you know about scopes?” or more specific ones, such as “What are the differences between declaring a variable with var, let, and const?” 

You may also need to determine the output of a code, considering scope types, closures, and hoisting:

for (let i = 0; i <= 1; i++) {
  let x = 1;
  console.log(x);
}
console.log(x);

 

Since variables declared with let are block-scoped, x is only available inside the loop, and the console.log outside the loop will throw an error.

If you had difficulty with this example, you should brush up on your scope knowledge. If you haven’t mastered the basic JavaScript mechanics, it may negatively affect your interview score.

Question 3: What is the same-origin policy?

Interviewers will undoubtedly ask some general web and app development questions to assess your understanding of relevant concepts and best practices. 

An answer to the sample question might be: The same-origin policy allows scripts on one website to access data on a second website only if they have the same origin. This prevents malicious scripts from gaining access to sensitive data on other websites. 

As a JavaScript developer, you need to know more than just how to code. So make sure you know about security, best practices, APIs, networking, etc. Show recruiters that you’re a well-rounded developer who has a comprehensive view of their work.

The JavaScript developer's job is versatile – and so are the questions you may receive in a coding interview.
Whether you’re a junior or senior JavaScript developer, proper prep for a coding interview is vital.

Question 4: What are arrow functions?

Interestingly, junior developers may find answering this JavaScript coding interview question easier because arrow functions were introduced with ECMAScript 6 (ES6) in 2015. If you learned JavaScript before that time, you might not be too familiar with more recent features. In contrast, newer developers have probably already learned JavaScript based on ES6.

In a nutshell, arrow functions are a short-form way for writing functions (but there’s a lot more you need to know about them!):

// “normal” function with ES5
var x = function(x, y) {
  return x + y;
}

// arrow function with ES6
const x = (x, y) => x + y;

 

Recruiters expect you to know about crucial changes and updates in the JavaScript ecosystem. After all, they’re looking for developers who build applications in a modern, clean, and sustainable way! So make sure you’re always up to speed.

You might also like: Programming Problem-Solving Interview Questions You Should Ask

Question 5: Can you fix the code?

Here, recruiters present you with JavaScript code with one or more errors that you need to find and fix. Often these are follow-up questions to “What will be the output of this code?”.

So let’s revisit Question 1, where we had to predict this code’s output:

console.log(0.1 + 0.2); // 0.30000000000000004
console.log(0.1 + 0.2 == 0.3); // false

 

Question: How can you rewrite the code to make the number comparison true?

There are several ways to do this, such as using the toFixed() method to format the sum in fixed-point notation:

let x = 0.1 + 0.2; // 0.30000000000000004
console.log(x.toFixed(1) == 0.3); // true

Questions like these help recruiters assess your troubleshooting and problem-solving skills and figure out how you would work with unfamiliar code on a team. So be sure to share your thoughts and ask for support when needed.

How to prepare for JavaScript coding interview questions

Naturally, there’s more to a Javascript coding interview than answering programming questions. To prepare properly, you should also consider the following:

We hope this post has given you a good idea of what questions recruiters may ask you during a JavaScript coding interview. We wish you all the best!

Want more tips on preparing for a coding interview? Check out Codility Developer Training – an app where you can develop your coding skills with our lessons and take part in our challenges.

Sarah Mischinger is a freelance technical content writer for Codility. She leverages her knowledge and experience in web development & writing to convey technical content in an understandable and engaging way.

Connect on Twitter & LinkedIn