× {{alert.msg}} Never ask again
Receive New Tutorials
GET IT FREE

How to Really Ace JavaScript Interview Questions: The Comprehensive Guide

– {{showDate(postTime)}}

Over the last few years, JavaScript has taken over the web development world becoming the primary language of choice for many new developers. That’s not just because JavaScript provides a great way to make web pages interactive – but also because that’s where the jobs are.

Why JavaScript?

The numbers speak for themselves. According to the US Bureau of Labor Statistics, web developer jobs are targeted to grow by 20% in 2012-2022, which is much higher than any other stream. This is backed by many major publications and professional bodies listing JavaScript in their Top 10 Programming languages for 2015. IEEE ranks JavaScript at #8, Inc puts it at #5, and Mashable lists JavaScript at #2.
So, if you’re interviewing for any kind of web development position, or for Android / iPhone app developer positions, you’ve got to brush up your JavaScript skills.

Where/How to Learn JavaScript

For new programmers, here’s a few good resources to get you started.

Here are a few other resources and developer tools to help you hone your JavaScript skills.

The catch is, most courses just teach the syntax, with a few examples thrown in. In a real interview, they are going to ask you more than just the syntax and theory.

What An Interviewer Really Looks For

In any programming interview, it’s not just about just knowing the syntax. It’s about knowing how to best use JavaScript concepts (and syntax), to solve an end problem.

  • How well do you understand the language conceptually? How effectively can you use those concepts to solve real work problems.
  • How do you write code (coding conventions, syntax, re-usability)? Will you spending half your time debugging syntax errors?
  • What’s your thought process like? How do you think about problem, come up with a solution, and translate it to working code.

These things are better learnt by doing rather than reading. A major part of your interview preparation should include solving problems, creating real apps etc. This is best done with the help of a an expert who has field experience using JavaScript in the real world, solving real problems and can bring in that extra perspective.Try out CodeMentor’s monthly plan to get 1:1 guidance for the kind of JavaScript questions asked in interviews. Most mentors have been on both sides of the interview table themselves and can help guide you on the smartest way to prepare.

In the meanwhile, here are some of the most common interview questions to help get you started.


Want to ace your technical interview? Schedule a Technical Interview Practice Session with an expert now!


JavaScript Interview Questions: Conceptual

To make sure you know the basics, interviewers typically start off with a few theoretical or conceptual questions.

  • How does type in JavaScript differ from that in other languages?

Unlike C++ and some other programming languages, JavaScript is loosely typed. This means that you can define variables without specifying a particular type. In such a situation, JavaScript automatically assigns the type depending on the value assigned to that variable. [Read more about JavaScript types and conversion here]

  • What are JavaScript timers? Are there any limitations?

In JavaScript you can use Timers to execute a function after a certain time, or repeat it at certain intervals. The functions used for this are setTimeout (function, delay), setInterval (function, delay), and clearInterval().

The catch is that all timer events run in a single thread. So if you’ve set multiple timers, they may end up interfering with each other. For example, you may still be executing the handler for Timer1 at the moment when Timer2 was meant to fire.. and therefore Timer2 will get delayed. The same holds if Timer2 is just the next instance of a repeating timer.

  • How does the virtual destructor work in JavaScript ?

[This is a trick question. The interviewer wants to make sure you’re not mixing up multiple languages]

JavaScript doesn’t have destructors. There’s no particular function called to ‘free’ objects. Instead JavaScript has a garbage collector that runs at regular intervals, cleaning up objects and freeing up memory that is no longer in use.

C++ has virtual destructors and I can explain that if you’d like. [Don’t launch into a detailed explanation. Check if the interviewer’s really interested.

  • Are Scope and Context the same?

[Following the previous trick question, the interviewers want to see if you’ll fall for this and say they’re the same.]

Though these words are many times used similarly, they refer to different things. Scope refers to the lifetime or access of a variable when a function is invoked. Context on the other hand, is related to the object. Context is the value of the ‘this’ keyword – the object that owns the currently executing code. [More about scope versus context here]

  • How does JavaScript interact with HTML or the DOM?

JavaScript is normally used to change HTML content or DOM elements. It can change HTML attributes, and HTML styles or it can be used to validate input data in forms. [You can read more about the JavaScript methods used for HTML manipulation over here.]

JavaScript Interview Questions: Syntax

Once they’ve verified you know the theory of how things work, interviewers will typically check your knowledge of syntax nuances.

  • Have you ever used “===”? Is there any such thing?

In JavaScript, the “==” (double equal) operator checks for ‘equal to’ only in value – it ignores the type. The === (triple equal) is a special operator in JavaScript that checks for equal value AND type.

  • What is the ‘this’ keyword used for?

It is used to reference the object that currently owns the code you are in.

  • Can you write two ways in which the variable can be assigned to an empty object?
var some_var = new Object();
var some_var = {};

JavaScript Interview Questions: Programs

After they’ve verified basic concepts and syntax, the interviewer will ask you to write some code. This could be either real world problems or just hypothetical situations, to see how you code and the thought process behind it. This is the part you really want to practice for and perhaps work with a mentor to guide you.

  1. Given the below code, what line of JavaScript would allow icecream.getFlavor to return the value of foo.color, without using the word “icecream”?
var icecream= {
    flavor: "vanilla",
    getFlavor : function(){
      //make this function return icecream.flavor
      //without using the word: "icecream"
    }
  }

Answer:

return this.flavor

[Yes, this is a simple answer. But the thing is we’re more used to writing code, than reading it. Interviewers ask these kinds of questions to see how quick you think on your feet and connect the dots.]

2. Implement the Fibonacci number calculator in JavaScript. Take a number as input from the user, and then print out the Fibonacci value for that number.
[ The Fibonacci sequence is F0 = 0, F1 = 1, Fn = Fn-1 + Fn-2 ]
There are many ways to do this. You can use a loop to calculate the sequence, or use recursion.

// With loops
var looping = function(n) {
   var a = 0, b = 1, f = 1;
   for(var i = 2; i <= n; i++) {
       f = a + b;
       a = b;
       b = f;
   }
   return f;
};

        // With Recursion
var recursive = function(n) {
   if(n <= 2) {
       return 1;
   } else {
       return this.recursive(n - 1) + this.recursive(n - 2);
   }
};

One variant of this question is “It should execute in the fastest way possible”. In an interview you really don’t have the time to run the code and check which is the fastest method. But you do know that loops or recursion take up a lot of time. So, ask if you can put an upper limit to n. Say n has to be under 100. You can then use a simple array to store the Fibonacci numbers and just access the array instead of computing it.

var array = ['0', '1', '1', '2', '3', '5', '8', '13', '21', '34', '55', '89', '144', /* etc*/];
var fibonacci = function (n) {
    return array[n];
};
console.log(fibonacci(10));

Conclusion

Hope this set of JavaScript interview questions helps you prepare for your big day. But again, this is just a limited set of questions. For a quick check on where you stand, try out this free JavaScript quiz over at W3Schools. If you want to go the extra mile to test your skills, you can also get certified through the W3School online certification program or better yet – just build a few real apps.

But to be truly prepared, try working with a mentor who has experience taking interviews – so you can get an understanding of what interviewers really look out for as well as practice writing code on the fly. Try Code Mentor’s Monthly pack (that comes with a 7 day free trial) or check out some of our JavaScript mentors here.


About the Author:

Richa Jain once managed the Mobile Browser team at Nvidia, and for about a decade before that, she wrote software for embedded systems at some of the world’s best semiconductor firms. Now, however, she focuses on writing about technology, business & entrepreneurship.


 

 




Questions about this tutorial?  Get Live 1:1 help from JavaScript experts!
Yuriy Linnyk
Yuriy Linnyk
5.0
Webdev Coach • 18yr+ Javascript Developer • 5yr+ CodeMentor.
You can understand it! Whether you're catching up in a coding bootcamp, or nailing a bug — reach me in the chat and we'll figure it out. I know you...
Hire this Expert
Sumit S
Sumit S
5.0
Senior full stack developer
More than 10 years of experience in web/product development(worked at Microsoft for 10 years). Currently working as a Full-stack developer....
Hire this Expert
comments powered by Disqus