Javascript Interview Questions
I was asked recently by a friend of mine to come up with a list of interview questions that you can get asked during a Javascript interview. I looked by to my last interview and came up with a few topics for him to brush up on. I thought I’d write them down so others might make use of them. If you were asked anything I’ve not included, let me know in the comments section.
We’ll start easy…
What is Javascript?
Mozilla Developer Network describes Javascript as:
JavaScript® (often shortened to JS) is a lightweight, interpreted, object-oriented language with first-class functions, most known as the scripting language for Web pages, but used in many non-browser environments as well such as node.js or Apache CouchDB. It is a prototype-based, multi-paradigm scripting language that is dynamic,and supports object-oriented, imperative, and functional programming styles.
The standards for Javascript are written in the ECMAScript documents. Currently we’re on ES5 but ES6 has just been locked down so I’m looking forward to checking out some of the goodies it has to offer such as native promises and modules and classes.
What is variable scoping?
Variable scoping in Javascript can get complicated so lets do some examples.
Globally scoped variable
1 2 3 4 5 |
|
Locally scoped variable
1 2 3 4 |
|
Closure
The return function still has access to the internal variable by trapping it in the closure. This is one of the great features of closures. More on this later.
1 2 3 4 5 6 7 8 |
|
Implicit Globals WATCH OUT FOR THESE
This is where you don’t declare a variable but then set it, these automatically go into the global scope. It is worth while checking for implicit globally scoped variables every now and again in your javascript because they can cause issues where things don’t seem to act like they should.
1 2 3 4 5 |
|
What does variable hoisting mean in Javascript?
Javascript has a way of hoisting variable declarations to the top of the function. This can produce some unexpected results if you are not aware of it them.
1 2 3 4 5 6 |
|
The above example is interpreted exactly the same as this
1 2 3 4 5 6 7 |
|
Where this gets confusing is in an example like this
1 2 3 4 5 6 7 8 9 10 |
|
What’s going on here? Well we’ve declared x outside the function text and set it to 100.
Inside the test function, the interpreter has seen there’s a variable declaration, ignored the fact that it
is inside a code block and hoisted it to the top of the function scope. The condition is not met so x
has not been set to 199 and remains to have a value of ‘undefined’
It is the same as writing
1 2 3 4 5 6 7 8 9 10 11 |
|
What is the difference between == and === in Javascript?
== checks equality whereas === checks equality and type. What does this mean?
1 2 3 4 5 6 7 8 9 10 |
|
What kind of conditional statements are available in Javascript
if statement
1 2 3 |
|
if else statement
1 2 3 4 5 |
|
if else if statement
1 2 3 4 5 |
|
switch statement
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
What would the following if condition example check for
1 2 3 |
|
This would satisfy if condition===‘undefined’ or condition===null or condition===0 or condition===“” or condition===NaN or condition===false.
This is a common way of checking for null or undefined but you have to double check that your variable would not ever be set to false or 0 because it will also satisfy.
What kind of loops are available in Javascript?
for loop
You can see in the second loop, we cache the length of the array so we don’t need to calculate the length of the array each iteration. Micro-performance improvement that can add up over time, although newer Javascript engines like V8 will more than likely JIT compile for loops automatically into their most efficient form.
1 2 3 4 5 6 7 |
|
for in loop
1 2 3 |
|
while loop
1 2 3 |
|
do while loop
NOTE: This does one full iteration before checking the condition
1 2 3 |
|
What different ways can you create an Array in Javascript?
The following examples show you different ways to create an array in Javascript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
|
How do you write an immediately invoked function?
If you were to write the following code, it would assign the function to the var addFirstFiveNumbers. In order to access the return value, you would have to execute the function.
1 2 3 4 5 6 7 8 9 |
|
If you wanted the function to execute immediately and store the return value in the var instead of the function, you use an immediately invoked function. To do this, wrap your function in parenthesis and put a set of empty parenthesis after it
1 2 3 4 5 6 7 8 9 |
|
How can you handle errors in Javascript?
Try Catch statements
These allow you to throw errors inside the try and catch them in the catch block
1 2 3 4 5 6 |
|
Try Finally statements
These allow your code to fail gracefully by performing some clean up task after your code has finished executing or failing.
1 2 3 4 5 6 7 8 9 |
|
Try Catch Conditional statements – THESE ARE NON STANDARD (and do not work in V8)
1 2 3 4 5 6 7 8 9 10 11 12 |
|
How would you execute a function in 30 seconds time?
Set Timeout
1 2 3 4 5 6 |
|
How would you execute a piece of code every 10 seconds?
Set Interval
1 2 3 4 5 6 |
|
Explain Inheritance in Javascript
MDN explains:
Inheritance is a way to create a class as a specialized version of one or more classes (JavaScript only supports single inheritance). The specialized class is commonly called the child, and the other class is commonly called the parent. In JavaScript you do this by assigning an instance of the parent class to the child class, and then specializing it. In modern browsers you can also use Object.create to implement inheritance.
Difference between Object.create and the new operator
Both are ways to inherit from a base class, but Object.create inherits from the prototype. What does this mean? Well lets do an example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
Here, Andrew is an instance of the Person class and so has species set on it. Andy on the other hand has inherited from the Person class’s prototype only and so has access to the prototype functions of the Person class but none of the attributes set on the class.
What is the difference between .call() and .apply()
At first you might think they do the same thing. Take a this arg and pass it to a function, lets see that in action
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
Where .call() and .apply() differ is the arguments you pass after the this arg.
In a .call(), you pass your this arg then followed by the args the function is expecting.
In a .apply(), you pass your this arg followed by an array containing the arguments to be passed which then get split out into the function params.
Lets see these in action
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
How would you go about dealing with an asynchronous request?
Deferred promises
jQuery and Q both provide a way to do promises and ECMAScript6 will provide a way to do native Javascript promises.
Lets look at the jQuery Deferred object.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
|
myFunction is calling the asyncFunction and getting a deferred promise back. When the promise gets resolved after the ajax request returns, the done function will get executed. If the promise gets rejected, the fail function will get executed.
These are useful when you are making calls out to a webservice for data.
Some Javascript shortcuts/hacks
Ternary operators
These are great for situations where you want to assign a var based on a condition.
test ? expression1 : expression2
You can play with these here at JSFiddle
1 2 3 4 5 6 |
|
Using bitwise operators in place of Math.floor
You can have a play with these and see them in action at JSFiddle
Why use them?
They are marginally faster than Math.floor as we can see from this JSPerf
Why not to use them?
- They reduce clarity in your coding
- They only work 32-bit signed integers
- They work by truncating the decimal rather than working out what the floor is
- You might not get the outcome you are expecting
- Math.floor(NaN) === NaN
– (NaN >> 0) === 0
Using bitwise operators in place of Math.floor
- Math.floor(NaN) === NaN
You can have a play with these and see them in action at JSFiddle.net
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
|