The questions below were asked in a preliminary check before the actual interview. The role itself was for a “JavaScript Engineer”. This particular set of questions is from a job I applied to over a year ago. I’ve chosen to share them here because I think readers of this blog would benefit from knowing what kind of things are expected if you’re applying to a JavaScript-centric role.
-
What is the relationship between ECMAScript, Javascript and Jscript?
-
What core types are available in Javascript?
-
What do the following statements return and why?
parseInt("07"); parseInt("09"); parseInt("010"); "1" + 2 + 3; 3 + 2 + "1"; "1" == 1; "1" === 1; "1" == true; "1" === false;
-
Alert the contents of the bar variable using the foo variable
var foo = "bar"; var bar = "foobar";
-
Alert the string “foobar” after a 10 second delay.
-
Implement Array.prototype.filter for IE.
-
Create a Person class with public/private/privileged members and methods.
-
Why does the statement 5 * 1.015 not return 5.075?
-
Replace the string
"The quick brown fox jumps over the lazy dog"
with the string"The1 quick2 brown3 fox4 jumps5 over6 the7 lazy8 dog9"
. -
List several ways that you can communicate asynchronously with the server.
-
How would you embed this URL in a link inside an XHTML document?
http://www.google.com/search?hl=en&q=%22xhtml%22
-
How would you serve a StyleSheet only to IE users older than IE8?
-
Write a jQuery plugin to display a list of images in a Lightbox.
What do you think of these questions? Fair?
Related:
Thanks for reading! Please share your thoughts with me on Twitter. Have a great day!
Mostly fair, though I’d say there’s a bit of a skill gap between a timeout alert and writing OOP with privileged members. But maybe that’s intentional ๐ Also if I were the interviewer I’d try to avoid specifying the framework, but again it all depends on the scenario.
Questions seem to be fair and interesting enough, though some things here require quite too much time for an interview, aren’t they?
Also, for the purists, it would be a good idea to write a tiny comment “yes, we know that this is not an actual class, but JS implementation of such”.
Also, quirks of XHTML are kinda dated, while knowing them may show the natural curiosity โ it’s natural enough that even very good dev will not have direct experience with them. If already, it’d be better to ask something pure XML related, not XHTML / validation type question.
Is it typical to use anything but radix 10/16 in development? And as long as your allowed to say “because javascript is retarded that way” for some of the Why’s, it looks good.
I’d argue that the question:
“How would you serve a StyleSheet only to IE users older than IE8”
Really isn’t a JS question at all, because I’d expect you to use IE conditional comments.
They all seem very fair, ranging from very basic to mildly advanced. I’m a little confused on the
Alert the contents of the bar variable using the foo variable
question. I feel like it should be a simple answer (possibly because it was near the beginning of your list, whatever significance that has), but it was the one question that has me stumped.Not fair. Given that most of these answers can be found in seconds via google, these test will quantify the user’s ability to answer test questions, not demonstrate and understanding of the fundamentals.
IMO, tests are an outdated concept for hiring a developer/engineer. A test places the developer into an environment at right angles to the environment in which they will be working.
In a test environment, they have to solve rapid fire tests about theoretical constructs in a severely shortened period of time. In real life environment, an engineer solves a set of interconnected tests about practical constructs, over several days or weeks.
For those of us who are novices, what would the correct answers be?
I have a feeling this is not the right way to do it:
What am I missing here?
I have something similar from Mathias, not sure if there is another way to get the index, my code is :
Looks pretty good… now I know I’m not a JavaScript developer.
@Ross Hadden – this?
“Write a jQuery plugin to display a list of images in a Lightbox.”
I hope the right answer is, “why on earth would I do something that’s been done a hundred times before?”
At least, if I had a programmer faced with a need for a lightbox, I’d kill him if he wrote it himself…
@Graham B
Or may be @Ross Hadden thought like me :
I didn’t think it’s that tricky ๐
Thank you for sharing thoses questions.
I also think it’s a good idea to share answers because I’m usually really pissed when there is only questions, not answer in such article, and it’s sometimes not easy to find the good answer(s). So here are my answers as I would respond the day of the interview :
#1.
ECMAScript = Define various detailled specs
Javascript = The language, general word
Jscript = A Microsoft implementation
(please don’t ask more :-p)
#2.
Boolean
Number
String
Date
Object?
(after a rapid search it seems the 2 last are wrong)
#3.
[ok wrong for the 2 last again, and i really don’t care why.
I just know that it works if you set the second argument to 10. Fuck it. Damn binary-octal-stupid parser^^]
Do I just invented `type priority` ? Anyway i think i’m right even if i don’t know the real name used for the inner workings.
#4.
#6.
uh I don’t really know it doesn’t exist in IE, I’m relying on higher level frameworks usually….
I guess something like (sorry, comment system broke the indentation):
#7.
#8.
5 * 1.015 != 5.075
because of the way floats are stored internally (?+?+mantisse). Its an approximation. Leads to small variations when doing maths. Be really really careful for financial calculations!!#9.
*Challenge accepted*
Dammit I forgot the replace method could take a function as second argument.
Hum wondering about perfs… Depending on string length, targetted browsers, …
#10.
-AJAX (XmlHttpRequest)
-Using flash & communication between flash and your javascript & flash remote connection
// And thoses which look terrible to me
-Creating a new element such as ,,
-Changing an iframe src
-AJAX with long polling (for server -> client)
[-page reload / change page location]
#11.
link
But I guess I’m missing the point.
Inside another link ?
link
I got a very bad feeling, this is way more complicated ^^
#12.
[meh, comment system is really broken on this one ; had to use < and > by myself]
<!– if [ IE < 8 ] –>
<style type="text/css" src="iefail.css" />
<!– [ endif ] –>
(i'm sure this is not exactly thoses chars but the idea is here)
(by the way i prefer NOT to use a separate stylesheet, also might be useful in some radical design decision ; and what about server sniffing to provide a different ?)
#13.
I think thoses questions cover some important concepts.
It is also not simple true/false questions, candidate has to write code, wich is also (very) important.
But could you judge someone javascript level in 13 questions ?
Depends a lot what your expecting:
write low level stuff / frameworks, or high end code ?
js guru ? someone with basics that will rapidly progress ?
…
Cheers++
Because of comment system issues, here is what’s missing:
#10.
-Creating a new element such as <script>,<img>,<iframe>
#11.
http://pastebin.com/M6yimsJL
(comment system html encoding is a real nightmare)
#12.
…and what about server sniffing to provide a different <head>)
This list looks great.
You could ask them to describe how they would write a serialize and deserialize (encode/decode) methods in native JavaScript. This would tell you how well they know could use Array methods join and splice.
My first thought for the fox one was:
I was thinking about alert(eval(foo));
Btw, how do you post the syntax highlighted snippets? James, your blog hates me ๐
I wrote the same code quote for quote, but minus the trailing semi. Lispy JS ftw.
Anyway I think the questions are pretty fair, agree with Graham that specifying the framework is odd, but whatever. jQuery is so common that it’s weird if you don’t know even a little, if you have a web background.
Lea, it’s WP-Syntax plugin:
These questions stand out:
> How would you embed this URL in a link inside an XHTML document?
> How would you serve a StyleSheet only to IE users older than IE8?
XHTML has nothing to do with JS, also it’s deprecated there is no real point using it.
About the second: if you want to ask people IE and JS related stuff you can ask them about event handling, AJAX techniques, JSON but CSS I don’t think is related, unless of course the correct answer is loading the CSS via JS with your IE detection code. ๐
I assume this is the correct way of replacing the string…
I had a go at ‘Implement Array.prototype.filter for IE’ but because I haven’t ever used that Array method before I had to lookup the specs on MDN as to what it actually needed to do (so not knowing would probably mean I failed).
@ Steve Antal: Since when is XHTML suddenly be deprecated?
Better get of that drugs ASAP ๐
cu, w0lf.
@Graham B: Thank you, I was right I was overlooking something very easy. Immediately after posting I realized I could do what @Ihab Khattab suggested, however your way felt sooo clean after rolling in the dirt that is eval().
Thank you!
@Graham B
@Ross Hadden
agree with you
A friend of mine suggested using this instead of window ๐
Hi guys,
I wrote these interview questions back in 2009.
As James mentions, the position was for a Javascript Engineer to work with a team of 10-12 people. At the time, it was 600KB+ OOP JS (a lot less minified/gzip’ed) targetting the usual IE7+/Firefox3+/Chrome/etc set.
The questionnaire was meant as a preliminary check that a prospective consultant could fill out before the actual interview.
I wrote a graded answer guide next to the questionnaire, perhaps I should dig it out from my archives. Nobody scored 100%, but that didn’t matter.
It helped to establish a baseline of competency and areas of interest without going into too much detail, which is why it covers both novice and intermediate areas along with some open-ended questions.
I’m glad to hear that most of you think the questions were fair. To be honest, I wouldn’t use the same question sheet today; It was specific to the team needs and quite a lot has changed since then.
I would walk out of the interview if presented with these questions. If a job presented this as a preliminary questions prior to the interview, I wouldn’t bother to apply.
The questions leave me a bad taste about the position. I cannot put my finger on why; it may be the basic level of the questions. Those questions are not in-depth enough to judge whether someone truly knows javascript beyond the basics and parroting back what they were told was ‘good’. Questions about prototypal inheritance, favorite framework and why, patterns, etc would be much more approriate.
My one_liner to tackle the fox transformation:
Regarding the foo bar question:
if you really care about the skill of appliers, check their commits to opensource projects
Katie, you would be surprised by how many people who classified themselves as Javascript Experts that questionnaire filtered out.
As mentioned, it helped to establish a baseline of competency and areas of interest without going into too much detail. That was quite deliberate, it was not meant to judge whether someone truly knows Javascript.
As far as the foo/bar question, there wasn’t any mention as what should happen with the foo variable so I just simply did…
@TiTi Thanks for sharing your answers
@James I think it’s was a very good idea to share these interview questions, you probably helped some self called ‘JavaScript Experts’. And even the author seems interested in the comments (-:
Some of these questions are useful, but some like the first part of 3 I would not use.
1. a) ECMAScript is the spec for JavaScript and another name for JavaScript
b) JavaScript is an implementation of the ECMAScript language standard and is primarily used to manipulate the DOM
c) JScript is a JavaScript dialect MS created
2. Core types:
Number
,String
,Boolean
,undefined
,null
3. Statement Evaluations:
4. Alert contents of
bar
usingfoo
5. Alert “foobar” after 10 seconds
6. Implement
Array.prototype.filter
for IE.7. Create a
Person
class with public/private/privileged members and methods.8. Why does the statement 5 * 1.015 not return 5.075?
JavaScript uses a floating-point number format which cannot store certain numbers “exactly”. In other words, some numbers are approximations. To get precise decimal places use
toFixed
orMath.round
9. Replace the string “The quick brown fox jumps over the lazy dog” with the string “The1 quick2 brown3 fox4 jumps5 over6 the7 lazy8 dog9”
10. List several ways that you can communicate asynchronously with the server.
– XMLHttpRequest – standard ajax call
– create new
img
,iframe
,script
elements with asrc
attribute– update the
src
attribute onimg
,iframe
,script
11. How would you embed this URL in a link inside an XHTML document?
a) change & to &
b) change %22 to "
result: "XHTML" google search
12. How would you serve a StyleSheet only to IE users older than IE8?
13. Write a jQuery plugin to display a list of images in a Lightbox.
Don’t reinvite the mass produced wheel
8.
I know that is not even the question… but the this a way to do it without using Math.round() or any thing like that:
Pretending that the number is always is given with 3 decimals:
Nice, I’ll use some of them if you don’t mind ๐
Surprised seeing so many getting the answer to question 1 wrong! And when I see a question whose answer is “implementation dependent” or “nonstandard”, I worry that the interviewer might have the wrong answer.
So here’s the deal with parseInt.
ECMAScript 3 discourages octal interpretation but allows it; so the answer is “implementation dependent — don’t expect a consistent result from parseInt(‘010’);”
Says that octal is optional.
ES5 banned the octal interpretation allowance mistake. The function parseInt no longer allows implementations to treat Strings beginning with a 0 character as octal values:
But web browsers c2011 are not caught up to the new spec — Firefox 5 and Safari 5 parse octal when no radix is specified, in violation of ECMAScript 5.
I’ve updated MDC docs for parseInt regarding octal interpretation in ES3 and how it is forbidden in ES5.
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/parseInt
Great questions! I usually interview JS engineers at my current job, so probably Iโll use some of them :). Here are my thoughts about some questions:
#2 โ Good one, most people miss undefined and null.
#3 โ Good one. Iโd add the usual closure created inside a loop and trying to use the loop index.
#4 โ Good. Using eval is a total fail for the candidate.
#5 โ Ok. Not using an anonymous function is a total fail.
#6 โ I think Array.prototype.reduce is a better choice. You have to deal with empty initialValue, that is very tricky. If the candidateโs implementation doesnโt match the Spec completely (specially for corner cases), itโs ok, but I’ll show the spec to the candidate and ask for a full implementation (letโs check if the candidate can read and implement a spec). BTW, all solutions provided in previous comments lack type checking (double fail: it is in the spec and omit type checking is a very bad practice).
#11 โ Just replace &. Changing %22 is a bad (you are mixing url encoding and html encoding).
#12 โ Not JavaScript, I donโt care if a JS Engineer doesn’t know about it.
#13 โ I donโt care if a JS Engineer doesnโt know about how to implement something in jQuery. I expect him to be able to discuss about jQuery good features vs. bad features, and build his own framework/library.
I like the PHP explanation of the comparison types (i.e. equal vs. identical)
( http://www.php.net/manual/en/language.operators.comparison.php )
If I was interviewing someone for a JavaScript position, I’d probably just have them write a basic CSS selector engine. Difficult, but fun. I’ve written 3. ๐ Usually doesn’t take more than a day or two to have something working well.
Oh, and here’s my slightly hackish #9:
Really enjoyed going through these. More…!
Fair, fair
now i know that i’m not a javascript developer… yet :p
***Replace the string “The quick brown fox jumps over the lazy dog” with the string “The1 quick2 brown3 fox4 jumps5 over6 the7 lazy8 dog9”.***
“The quick brown fox”split(” “).map(function(e){
return e+(++i);
});
//Replace the string “The quick brown fox jumps over the lazy dog”
//with the string “The1 quick2 brown3 fox4 jumps5 over6 the7 lazy8 dog9″
str=”The quick brown fox jumps over the lazy dog”;
str2=””;
str.split(‘ ‘).forEach(function(x){str2+=x+x.length+’ ‘});
alert(str2);
I noticed most folks used the map function for the string manipulation, whereas I went for array methods like Christian L for the browsers from Redmond.
No need to declare the length outside the loop. Having it declared inside still only gets executed once, like var i = 0. Also it’s more efficient to avoid string concatenation just to add spaces to the array indices. Passing a space as the argument for join accomplishes the same thing more efficiently.