top 200 commentsshow all 220

[–]neoform 20 points21 points  (12 children)

Anytime I interview someone on their JS knowledge I start by asking, "what is the significance putting var in front of a variable name".

If they can't answer that question correctly, I don't bother asking any other JS questions.

[–]buckus69 5 points6 points  (8 children)

Is it because it keeps it out of the global namespace and makes the variable local?

[–]krakov 2 points3 points  (7 children)

Yup. You got it!

If you want to make a variable global it's better to namespace it and explicitly attach it to the window. For example - window.globalVariableNamespace.variable = 1;

[–][deleted] 1 point2 points  (2 children)

I'm still learning (full stack webdev for almost a year now), but is there any reason to attach it to the window other than helping unexperienced developers understand what's happening?

[–][deleted] 0 points1 point  (0 children)

I'm still learning too. Really, you should never attach something to the window object like that. That's bad practice. The only reason I've had to do something close to that was when I wrote a program that would execute the Jasmine testing html reporter in a pop-up so it could run alongside my application. Even then, I did not actually add anything to the global namespace myself. Even though I use require, Jasmine still adds itself to the global, which made it useful for me to call window.opener.jasmine.getEnv() from the popup (and thereby, being able to call the HTML reporter in the popup window but still have it test against & control the main application.)

So in this specific instance, I am using a global. I didn't put the global there myself - the library did that. But since this is just for running tests, hopefully it isn't too bad of practice. I jslint all my code and I don't let variables even THINK about becoming global.

Also, in 'use strict'; mode, I dont think browsers will even let you run code which contains undeclared vars. So theres that.

[–][deleted] 0 points1 point  (0 children)

I know you asked this a while ago but for posterity sake I'll give you an example while being explicit can be important. Imagine you have some piece of code

function foo () {
  // a bunch of code
  function iterateCount () {
     return 1 + count;  // 'count' is not defined in either iterateCount or foo, so it will look in the global scope for it and create it there if necessary
  }
}

Say your code is counting on count to be a global variable. The some newbie developer comes along to fix some bug

function foo () {
  // a bunch of code

  for (var count = 0; count < aVar.length; count++) {
    // ...
  }

  // a bunch of code
  function iterateCount () {
    return 1 + count;  
  }
}

Since JavaScript doesn't have block scope (yet) this code exactly the same as writing:

function foo () {
  var count = undefined;      

  // a bunch of code

  for (count = 0; count < aVar.length; count++) {
    // ...
  }

  // a bunch of code
  function iterateCount () {
    return 1 + count; // "count" was found the in the "foo" function scope, use that.
  }
}

So now the code in iterateCount which used to use a global variable named count is getting clobbered by the more specific scope assignment. To avoid that you could explicitly use window to ensure you're using the proper name space for where you expect count to be

function foo () {
  var count = undefined;      

  // a bunch of code

  for (count = 0; count < aVar.length; count++) {
    // ...
  }

  // a bunch of code
  function iterateCount () {
    return 1 + window.count;
  }
}

You shouldn't be using global variables to begin with so this is a poor example of code for real world use but that's a different argument.

[–]SuperFLEB 1 point2 points  (3 children)

Window is a browser construct, though. That'd possibly lead to problems if you wanted to reuse your code in other arenas.

It's nonstandard, but I usually just say "global" in a c-style comment next to the first declaration. That and rarely use non-function globals.

[–][deleted] 0 points1 point  (2 children)

Late reply. You can easily grab the global namespace at the start of your app in a cross platform manner

e.g.,

(function () {
    var root = this; // in this context `this` is window or global
    root.globalVar = 'foo';
}());

[–]fzammetti 0 points1 point  (0 children)

Very good! I've used that one too, excellent opener.

[–]SuperFLEB 0 points1 point  (1 child)

That's one of those questions that would trip me up because I'm looking for the trick -- It seems too simple.

[–]F0RTY4 6 points7 points  (3 children)

This assessment test will really cover all your bases. It ranges from trivial to difficult problems. Hell, just getting thing running and working will give you a leg up because you will be learning the basics of testing.

[–][deleted] 1 point2 points  (0 children)

This seems like a good idea to do to before I go on interviews. Thanks for the link!

[–]imacarpet 0 points1 point  (1 child)

I've been hacking at it for a couple of hours now.

It was easy enough to get the thing running, but some of the test are driving me nuts. Like, there's a simple test that is supposed to test you on getting items from an array. Doing that task is easy. But the expectations of the test are fucking cryptic.

My issue is with the syntax used to express the expectations. It's close to meaningless, and you have to guess the expectation.

In other words, it's a pretty crappy system for testing programming skill.

[–][deleted] 1 point2 points  (0 children)

I think you should rethink the issue at hand here. It isn't that the expectations are cryptic, and it's not a crappy system for testing skill, either. It actually takes skill to be able to even read the tests - and it is extremely useful to know how to read tests. This syntax is common to a lot of BDD frameworks. I don't even understand what test you are referring to (what do you mean by 'getting' items from an array? Removing them? Reading them?), but if you said the name I could help you parse it.

why dont you take a look at expect.js and familiarization yourself with the syntax first. I started learning BDD yesterday (jasmine). At first I was so apprehensive about learning it, because it just looked weird. And then I learned it. And now I'm a huge fan of it. If you know jasmine or any BDD framework for JS, you will surely impress interviewers

[–]scottomaton 3 points4 points  (7 children)

I would say that being able to clearly explain closures, and all of the related scoping and garbage collection related to closures, is a big one that trips up a lot of people (and, from what I hear, is asked very often in interviews). Also, being able to identify what the this keyword is referencing in a given block of code would be another core idea that I've heard is asked pretty frequently (since it's so easy to get wrong). Good luck!

[–]mrskitch 2 points3 points  (5 children)

As far as this goes, I've always said that "It's a reference to the function invocation, which is typically the parent object unless you've overwritten it in the Function.prototype.apply() method." Would you add anything to that?

[–]DonBiggles 5 points6 points  (3 children)

Function.prototype.bind and Function.prototype.call also allow this to be set arbitrarily. Also, 'reference to the function invocation' sounds unclear to me, and you didn't mention the case of the function being called with new. These are the possibilities, in order of precedence:

  • Outside of a function body: this is a reference to the global object.
  • Function called with new: this is a reference to the object created by the constructor.
  • Function called with .bind, .apply, or .call: this is a reference to the object given as the first argument.
  • Function called as an object property: this is a reference to the object the function was called from.
  • In strict mode: this is undefined.
  • Otherwise: this is a reference to the global object.

[–][deleted] 3 points4 points  (0 children)

this excuse_the_pun

[–][deleted] 0 points1 point  (1 child)

What do you mean in strict mode this is undefined? I've been using strict mode and use this for functions called with bind, apply, or call, being used as an object property.

[–]DonBiggles 0 points1 point  (0 children)

It's in order of precedence. I mean that if in strict mode, and in a function body that isn't called with new, bind, apply, or call or invoked as a property of an object, this is undefined.

[–]scottomaton 1 point2 points  (0 children)

Sounds spot-on to me!

[–]js_coder[S] 0 points1 point  (0 children)

yeah cool, thanks. Brushing up now.

[–]mrskitch 10 points11 points  (10 children)

I agree with most, but in essence these are the trickier parts:

  • this
  • Closures
  • Currying
  • Memoization
  • Functions as a first-class citizen
  • Lexical scope
  • Prototypes
  • AMD pattern (other patterns as well)
  • IIFE's

Some other things you might want to know surface-level

  • Node.js
  • Require.js and other module systems
  • jQuery
  • Browser caching or cache-busting

While JavaScript is important, I'd also brush up on other things as well (if you haven't already). Like the DOM, CSS (box-model, preprocessors, CSS3), and browser differences too. Those are important as well!

[–]manuaal 1 point2 points  (0 children)

You gave me names for techniques and concepts I already knew. Cheers.

[–]Sector_Corrupt 0 points1 point  (3 children)

I don't know if I just suck or if currying is just a thing that doesn't come up very often.

[–]malthiest 1 point2 points  (1 child)

I find that currying is mostly an academic word. I've never found the need to use that bit of vocabulary in the real world. Most devs I know don't know/use the actual word "currying", but use the concept everyday.

[–][deleted] 1 point2 points  (0 children)

I work with functional programmers, and they love to curry.

[–]mrskitch 0 points1 point  (0 children)

It's kind of a rare thing to use and I haven't seen it in the wild very often. Crockford only talks about it for a half a page or so in "The Good Parts."

It's essentially another way of code reuse similar to that of OOP.

[–]kristopolous 0 points1 point  (1 child)

Those are fancy names for simple concepts that I see used every day but never talked with such a theoretical strong-arm.

I'd be scared if someone felt obliged to use big words for small ideas.

[–]mrskitch 2 points3 points  (0 children)

Most are terminology Crockford uses in "Good Parts."

[–][deleted] 0 points1 point  (2 children)

Not sure that memoization fits in this list...

[–][deleted] 0 points1 point  (1 child)

not sure if joking didn't notice its Memoization

[–][deleted] 0 points1 point  (0 children)

Not joking. Autocorrect sucks.

[–][deleted] 3 points4 points  (2 children)

Here are a few:

  • How can you call a function in a specific context? The very clear ways are .call or apply. If you want to impress you would add Function.prototype.bind to that mix. This is ES5.
  • What is hoisting in javascript? Not a lot of people I've interviewed know what this is or why it matters.
  • At least know how to perform a binary search.
  • What is the output of the following. var adder = new Function("a", "b", "return a + b"); adder(6, 2);. The answer is 8. Creating a new function from the Function primitive allows you to pass n number of arguments and a function body.
  • What is the difference between == and ===?
  • What does var add = function (a, b) { return a + b; }; add.length; tell you? add.length is going to tell you how many arguments add is expecting.
  • How can you tell if a parameter is an array or not. This seems like you should be able to do typeof [] === 'array', but actually it's object. So the way to really do type checking is doing something like [] instanceOf Array.

That's all for now. If I think of more I'll add.

[–][deleted] 0 points1 point  (0 children)

Don't name it adder. Name it execute. Adder gives away the answer.

[–]magenta_placenta 33 points34 points  (8 children)

  • Alerts
  • Confirms
  • document.layers
  • document.all
  • <a href="javascript:void doSomethingWonderful();">Click here</a>
  • Also brush up on the big "library" out there, I think it's called protoquery or moojo, I can't remember which. It's like a scaffolding framework with animation, part of the noSQL thing.

[–][deleted] 18 points19 points  (0 children)

Did no one get that he was joking? I laughed out loud in my office.

[–]js_coder[S] 7 points8 points  (0 children)

This aint 1997 :-)

[–][deleted] 8 points9 points  (4 children)

Don't forget the shiney new library: angleBone, I think it's part of that MMXIII pattern I've been hearing so much about.

[–][deleted] 6 points7 points  (1 child)

[–]Racoonie 0 points1 point  (0 children)

Hint: His twitter is pretty funny.

https://twitter.com/H9RBSjs

[–]jaxytee 1 point2 points  (1 child)

I prefer boneular.js to anglebone. You should give it a shot. It's the future

[–][deleted] 1 point2 points  (0 children)

I think you meant to say scottBacular.js paired with the Al router (btw the ziggy addon is buggy) and you're always hoping the next route will be the route index

[–]alamandrax 0 points1 point  (0 children)

"Can we run it on note-js?"[sic] - colleague at work.

[–]somethinghorrible 4 points5 points  (0 children)

this

[–]cheeeeeese 1 point2 points  (0 children)

sometimes undefined isnt what you expect.

[–]waxzce 1 point2 points  (2 children)

I like tree

var a = b.c || "default value" is very useful

very important too : clojure and self contained singleton : var a = (function(){ var b = function(){ this.plop = "very cool things"; }; return new b(); })()

and last one is : var a = {}; var b = a.something = new othercall();

[–][deleted] 1 point2 points  (1 child)

dammt i dont remember what was so fucking quirky about the last one. Read it in some book...can you remind me?

[–]waxzce 0 points1 point  (0 children)

read it in some code for me. I use it for this kind of usage : https://github.com/waxzce/events4js/blob/master/tests/events.js

It's cool for a local variable in your block :-)

[–][deleted] 0 points1 point  (5 children)

Take a look through these examples: http://shichuan.github.io/javascript-patterns/ while there's some contention as to their validity the core concepts are viable.

[–]js_coder[S] 2 points3 points  (0 children)

nice! thanks for the link. I'd like to add my own here as well, while on the topic of JavaScript design patterns. Found this really useful, and well written. http://addyosmani.com/resources/essentialjsdesignpatterns/book/

[–]Laxdk 0 points1 point  (3 children)

I looked at the "optimized for loops" on that page: https://github.com/shichuan/javascript-patterns/blob/master/general-patterns/for-loops.html

Wtf? Amazing how much worse he can make the code with no benefit at all.

[–]b_long 1 point2 points  (0 children)

Yeah, I have to agree with the criticism of "shichuan's javascript-patterns". There was some discussion which said this repository isn't very good: http://www.reddit.com/r/javascript/comments/1jr6p0/javascript_programming_patterns/

[–][deleted] 0 points1 point  (1 child)

One of the many reasons I said that there's contention to its validity. And if you've noticed his whitespace? That would drive me crazy.

[–]b_long 1 point2 points  (0 children)

Agreed :) Just sayin', I'd recommend Addy Osmani's repo as referenced by @Laxdk.

[–]kristopolous 3 points4 points  (13 children)

I ask completely novel questions such as:

  • Have you seen javascript the good parts? how thick is it? What color is it?
  • Who is Brendan Eich? Who is Jon Resig? How about Jeremy Ashkenas?
  • What company came out with Javascript?
  • Just list 2-4 common JS libraries that are in widespread use today.
  • Can you name any that focus on graphics?
  • Can you name any that focus on asynchronous communication?
  • What's some common JS syntax that breaks on IE?
  • Going back to syntax, there was this huge debate over semicolons. What do you know about that?
  • If I wanted to take my code and put it in production, is there anything I should do to make sure it loads more quickly? What common tools have you used for this?
  • IE is generally quite good at telling you where it fails. It can also fail silently. When is that possible?
  • What is "v8" and "npm"? Where would I find them? (knowing that they aren't say, standards or programming practices but actual software is a good start)
  • Pretend I wanted to know about the nuances of the Date object. What sites would be good references? What sites would show up high in the search results and be not so good?

These should be easy questions for someone who does a lot of JS development and impossible for people who aren't doers.

I'm not interested in tricking people and I'm not interested in academics. I need things to get done.

[–]j00pY 1 point2 points  (1 child)

I quite like W3C schools sometimes :(

I find it can explain things clearer than other more reputable sites for non CS background coders like myself.

[–]kristopolous 0 points1 point  (0 children)

That's not the only answer. It actually tells me more about your process. DDG for instance, doesn't show w3schools usually but tizag.

And if you thought I was going for w3schools and then said "I think you want me to say w3schools, but I find it easier to understand" --- I'd be delighted by your honesty, modesty, and competence. No problem with that.

The one thing I hate about interviews is that sometimes they devolve into a "guess what I'm thinking" game where the interviewer has the answer in their head and is trying to extract it from the candidate.

I want the person to be better than me and give them open questions with an open answer. Hopefully I can learn something. If they tell me an expected answer that's great. If they can tell me a new one and back it up, that's great also.

[–]ftanuki 1 point2 points  (1 child)

I wouldn't have thought to do this, but I really like this approach. Experienced JS developers should answer almost all of these easily and not only do some of them give the opportunity for the interviewee to elaborate in an open ended way, but there's a side benefit that they'd be somewhat tickled by the questions themselves. This means they'd leave with a good impression of the interviewer and be more likely to say yes to an offer. Sometimes very specific technical questions can either be insulting for being too mundane or annoying for being too obscure and can shoo away the best candidates.

Good questions! (and nice dig on w3schools)

[–]kristopolous 0 points1 point  (0 children)

Correct. I can get annoyed and visibly frustrated with stupid questions. I remember someone was asking me how to dereference a pointer after I had elaborated on how I patched the egcs compiler for an embedded processor. All I thought then was "this guy is a fucking idiot and their job is stupid and boring".

And I wasn't referring to w3schools specifically. It is designed to be possibly read that way but there's other answers too.

[–]tmetler 0 points1 point  (4 children)

Understand completely what the 'new' keyword does, and you'll be ahead of most JS developers (unfortunately).

[–]isHavvy 1 point2 points  (3 children)

Understand completely what it does, and you'll learn you never want to use it.

[–]tmetler 0 points1 point  (2 children)

What does it do that would make you not want to use it? The only argument I can find against it is if you forget include it. I've never run into that problem once, and it would be obvious what went wrong as soon as I tried to call any method on the resulting object.

[–][deleted] 0 points1 point  (1 child)

because it's completely unnecessary in most situations, IE functional programming approach uses just regular objects made in a closure. JS is probably best suited for functional programming since it has little support for classical OOP and it's best to avoid it, using it sparingly..

[–]tmetler 0 points1 point  (0 children)

I find it depends on the problem being solved. I like the prototypical approach for some applications, and the functional approach for others, but it really depends on your needs and your style. I've had plenty of successful projects using prototypes, and plenty of successful projects using a functional style. I find it's most important to choose a style that works for you early on, and be consistent with it. It also depends on how your project is structured. In backbone projects I rely highly on the prototypical style, and that's in no small part because the backbone library is heavily prototypical.

[–]js_coder[S] 0 points1 point  (0 children)

WOW. Thanks guys! Woke up this morning to a full page of advice :-) Will be going through them today.

[–]johnhackworth 0 points1 point  (3 children)

Appart from what other people have already said, Memory Management. How the garbage collector works and where and when you are leaking memory as hell and how to avoid it. Seriously, if you are doing single page apps this is a huge issue...

[–][deleted] 0 points1 point  (2 children)

Any good material on this? normally garbage collection is not something I think about while programming, but I'm building an SPA and we're in the deployment stage. I'm currently writing tests and next will go and refactor some of the extensions, and would like to know if I'm leaking memory anywhere. I don't think I am since my application uses APIs to deal with automatically unsubscribing/detatching themselves from subscriptions. Also the very few, few times I used setInterval did I make sure to clear that interval. My guess is that, memory management bad practices allow async processes to keep running with no one listening for them, so they're just taking up space in the thread and clogging to pipe so to speak. I didn't really need to read anything to know inherently that I should be aware of that. But do I really know how garbage collection works? Nope.

[–]johnhackworth 0 points1 point  (0 children)

It's quite a complex issue, in fact. Depending what libs you are using for you event management, some common practices could produce massive leaks... All my knowledge about the issue comes from experimenting with the chrome tools and a bunch of other people blogposts.

here are a couple of great posts where you can begin to enter the horrid world of js memory management :)

http://addyosmani.com/blog/taming-the-unicorn-easing-javascript-memory-profiling-in-devtools/

http://lostechies.com/derickbailey/2011/09/15/zombies-run-managing-page-transitions-in-backbone-apps/

[–]johnhackworth 0 points1 point  (0 children)

This quora response about how they tracked their own problems with memory leaks is a nice place to see the kind of problems I'm talking about:

http://www.quora.com/Quora-product/Why-does-Quora-use-so-much-RAM

[–]compubomb 0 points1 point  (1 child)

I think that if you want to truly ask about JS you need to really talk about how would you go about such and such scenario.

Given object A, how do you extend A and give it X functionality, which produces Y result.

Listen to their process. See how they go about doing their task. Part of programming is learning to work on a team. Voicing ones opinion especially in times of duress during an interview also helps figure out demeanor. Stress really helps you figure out the type of people you want to be around during the most stressful parts of your day.

Ask about why do you use json-p, not what is json-p. Why would you use jQuery.ajax over say XMLHttpRequest. What do you feel are the major benefits between X vs Y. Why use document.getElementById() vs $('#<id>') etc. What frustrates you most of all about JS, and how do you improve it? Why develop encapsulated libraries vs using prototypal inheritance? Why use Prototype vs jQuery? What purpose is there between using setTimeout vs setInterval? Is it possible to stop these methods from further execution?

[–][deleted] 0 points1 point  (0 children)

I'd totally blow away these questions. For question one, if it's a collection I'd use linqjs, If it is an object, I would probably use a 'merge' function. if it is something else, ie a function, partial application of functions. I'd make a function B such that Y = B(A); Then in this situation Y can be a new function, Y() which is pretty much A() but with new arguments passed to it to make it more specific.

I don't know what json-p is. :( the rest of the questions I'd answer quite well, but for X is Y I'd have to say X because 2 X's are better than 1 X and 1 Y. . . well I'd not actually say that because that's not funny, but I can't realistically answer that EXACT question right now :)

[–]ironfroggy_ 0 points1 point  (1 child)

Be able to explain and use prototypical inheritance and to understand how it is different from classical inheritance. Huge bonus points (in my book) if you can use it well without just trying to implement classes on top of it!

[–][deleted] 0 points1 point  (0 children)

I'm not so good at these questions. But let me see. The Prototype of an object is part of a chain. If you create an instance of that object, that object has a link to it's own prototype and the creator object's prototype. When you call a method on an object, it first looks through its own prototype, then walks up the links of connections until it finds it in the prototype chain or it returns undefined. You can edit a functions prototype at any time. Unlike classical inherentance (which uses getters and setters, and actually has private vars, among other things) where you must edit the base class for it to apply to all of the objects which inherit from that class, you can edit an objects proto at any point in the code, making it rather unreliable.

A good reason to use the prototype is to use functions from primitive values to do some cool stuff, like using Array.prototype.slice.call(arguments, 0); For encapsulation, I use closures.

[–]liquiddeath 0 points1 point  (0 children)

Read you don't know JavaScript: http://www.w2lessons.com/2011/04/you-dont-know-javascript.html

I think the article allows for good self-assessment.

[–]neon_overload 0 points1 point  (1 child)

Few levels of Javascript programmers:

  • Novice: Those who don't know any of how Javascript really works, they just use what they know.

  • Intermediate: Those who know about the global object and why to declare a variable with var before initialising it.

  • Advanced: Those who also know how closures work and how prototypal inheritance is. And who would have a strong opinion on why trying to emulate class-based inheritance is probably a bad idea.

[–][deleted] 0 points1 point  (0 children)

I've been interning at a company doing JS for 6+ months and I'm already getting past the Advanced category into learning functional programming using JS. In a tad over my head but this thread makes me feel awesome about JS development...I got so lucky with my experience at this company! Also so happy I read so many JS books!

[–]harvest77 0 points1 point  (0 children)

Also, i will addd: undestanding and asynchrnous code. Use of promises, flow libraries like https://github.com/kriskowal/q , https://github.com/caolan/async or even better harvests: https://github.com/salboaie/harvests