Answering the Front-end developer JavaScript interview questions to the best of my ability.
- Explain event delegation
Sometimes you need to delegate events to things.
- Explain how
this
works in JavaScript
This references the object or "thing" defined elsewhere. It's like "hey, thing I defined elsewhere, I'm talkin' to you."
- Explain how prototypal inheritance works.
When you inherit a prototype coded by someone else and you don't know what the hell is going on with it.
- How do you go about testing your JavaScript?
Step 1: weep. Step 2: plunk around on StackOverflow for a couple hours. Step 3: copy and paste until it works. Step 4: merge feature branch and deploy to dev environment to queue for continuous integration. Just kidding, I have no idea what that means.
- AMD vs. CommonJS?
AMD, or the Association of Mannequin Deconstruction, is a unionized organization dedicated to deconstructing mannequins at stores that are going out of business. CommonJS is JavaScript written by commoners. Like me.
- Explain why the following doesn't work as an IIFE:
function foo(){ }();
Because those IIFEs are always messing shit up. Stupid IIFEs..
- What needs to be changed to properly make it an IIFE?
Put the official IIFE hat on it, and follow the official IIFE induction ceremony protocol.
- What's the difference between a variable that is:
null
,undefined
orundeclared
?
Null variables are SOL. Undefined variables haven't been defined yet. Undeclared variables won't make it through customs at the airport.
- How would you go about checking for any of these states?
Ask for ID.
- What is a closure, and how/why would you use one?
Sometimes important events happen, often leaving lingering questions behind. Getting closure allows us to come to terms with these events and move on with our lives.
- What's a typical use case for anonymous functions?
Sometimes you don't want people to see the functions you're writing, so you do all your debugging in a private tab in Chrome. Or you can use Tor.
- How do you organize your code? (module pattern, classical inheritance?)
I try to color-code things, so I spend a lot of time getting my syntax highlighting just right.
- What's the difference between host objects and native objects?
Host objects consist of unleavened bread and are distributed to some people on Sundays. Native objects are deployed to native environments (via PhoneGap)
- Difference between:
function Person(){}
,var person = Person()
, andvar person = new Person()
?
The wonderful thing about people is that they come in all shapes and sizes. They have different personalities and aspirations.
- What's the difference between
.call
and.apply
?
Calls are made on iPhones (or Androids). Applying is how you get a job.
- Explain
Function.prototype.bind
.
The bind() method creates a new function that, when called, has its this keyword set to the provided value, with a given sequence of arguments preceding any provided when the new function is called. (I just copied and pasted this from MDN)
- When do you optimize your code?
Normally I wait an hour after I eat to optimize my code, to be safe.
- When would you use
document.write()
?
When I make banner ads.
- What's the difference between feature detection, feature inference, and using the UA string
Feature detection involves testing for the presence of a particular feature. Feature inference is making some assumptions about the presence of a feature. UA detection is the spawn of Satan.
- Explain AJAX in as much detail as possible.
Ajax is a brand of cleaning products, introduced by Colgate-Palmolive in 1947 for a powdered household and industrial cleaner. It was one of the company's first major brands.The ingredients of its scouring cleanser product include sodium dodecylbenzenesulfonate, sodium carbonate, and quartz. The Ajax name was successfully transferred to an entire line of household cleaning products and detergents; the line enjoyed its greatest success in the 1960s and early 70s. Ajax All-Purpose Cleaner with Ammonia, introduced in 1962, was the first major competitor to Mr. Clean, which Procter and Gamble debuted in 1958. Ajax' success as the so-called "White Tornado" forced Procter and Gamble to introduce its own ammoniated cleaner, Top Job, in 1963.
- Explain how JSONP works (and how it's not really AJAX).
JSONP works like a charm, and it's different than AJAX in that it's not a household cleaning supply.
- Have you ever used JavaScript templating? Who's asking?
- If so, what libraries have you used? (Mustache.js, Handlebars etc.)
Used mustaches sound gross, but I suppose I have used Mustache.js before.
- Explain "hoisting".
Sometimes you really gotta get in there, spit on your hands, and lift that sucker up over your head like you mean it.
- Describe event bubbling.
Here's a link to the IMDB page for the Butterfly Effect, starring Ashton Kutcher. It would do a better job at explaining event bubbling.
- What's the difference between an "attribute" and a "property"?
Everyone's got attributes. Some good. Some bad. As sad as it is to say, not everyone owns property. Though a discussion about economic inequality might be out of scope for an interview like this.
- Why is extending built in JavaScript objects not a good idea?
I mean, the things are already built. Just leave 'em be, or you'll mess 'em up. Hands off, pal!
- Difference between
document load
event anddocument ready
event?
Document load means the document has loaded. Document ready says "hey, I'm ready to go!"
- What is the difference between
==
and===
?
This one is almost too easy. One has an extra "=".
- Explain the same-origin policy with regards to JavaScript.
Same-origin policy makes sure a bunch of third-party bullshit doesn't make it onto the page.
- Make this work:
duplicate([1,2,3,4,5]); // [1,2,3,4,5,1,2,3,4,5]
How about this? [1,2,3,4,5,1,2,3,4,5]
- Why is it called a Ternary expression, what does the word "Ternary" indicate?
Ternary means it involves three things, and strings 'em all on one line so it's harder to read.
- What is "use strict";? what are the advantages and disadvantages to using it?
use strict : JavaScript :: Harlem Globetrotters : Basketball
- Create a for loop that iterates up to 100 while outputting "fizz" at multiples of 3, "buzz" at multiples of 5 and "fizzbuzz" at multiples of 3 and 5
That sounds hard, and for some strange reason I'm suddenly jonesing for a Dr. Pepper.
- Why is it, in general, a good idea to leave the global scope of a website as-is and never touch it?
We've only got one globe, man. If we ruin it, we'll all die off unless we can figure out how to colonize Mars real quick like.
This rules.