Source: http://insights.dice.com/2012/09/26/javascript-interview-questions/
How do you implement an extend function that takes an object and extends it with new properties and makes it work on n levels of recursion? Basically, duplicating a jQuery extend.
This question shows whether candidates have an understanding of basic programming concepts such as recursion, says Jonas Huckestein, co-founder of conference-calling startup HipDial
Can you write a function that takes an object and appends it to the DOM, making it so that events are buffered until the next tick? Explain why this is useful?
This last part only applies in browser-side settings where it can dramatically increase performance, he says.
How do you write an event emitter base class that allows you to add event listeners?
This question can nicely lead into architectural questions, Huckestein says, such as: “How would you make an event emitter that’s distributed?”
What is the concept of “functions as objects” and how does this affect variable scope?
“What it can suggest is that the person really ‘gets’ JavaScript and the way it works as opposed to just having copied syntax and code from the web without understanding it,” Weinberg says. “It can also show that the person has at least some understanding of basic programming concepts, which in my experience means they will be better equipped to come up with good solutions to hard problems.”
What modern JavaScript frameworks and utilities excite you right now from an approach and code point of view, even if they’re not yet stable enough for client work?
TBD
What is the difference between .call() and .apply()?
The JavaScript Function prototype has two very powerful functions that are at the core of Javascript’s “everything is an object” mentality, including functions, Kubasik says.
Can you explain how inheritance works in JavaScript?
JavaScript has a somewhat unique inheritance model and a good understanding of it is crucial to using JavasScript in larger applications, Kubasik says. “We are looking for the applicant to discuss not only prototypes, and how that affects inheritance, but in what ways this can be more flexible than classical inheritance models seen in Java and C#.”
What is event bubbling in the DOM?
The main goal of this question is to establish that the applicant knows what order events will be propagated in the DOM – most specific to least specific.
“Not everyone may know this by the name ‘event bubbling,’ so asking about event propagation in general is sometimes needed. Ideally, this is an opportunity to discuss event models outside of the DOM, and ask follow-up questions about routing based on user actions, looking for techniques popularized with frameworks like backbone.js, or AngularJS,” Kubasik says.
I can go onto GitHub or BitBucket. I can actually look at what they’ve done with their code. I can see the projects they’ve worked on [and] I can see how much they’ve contributed to projects. I can go onto sites like Stack Overflow and see who are the influential people in the community, see who’s answering questions specifically about JavaScript,” he says. “… from that I already know they’re technically savvy, so from there, my role is just to convince them to leave where they currently are and come work for us.”
Source:
http://www.sitepoint.com/5-typical-javascript-interview-exercises/
|
1 2 3 4 5 |
(function() { var a = b = 5; })();
console.log(b); |
The code above prints 5.
The trick of this question is that in the IIFE there are two assignments but the variable a is declared using the keyword var. What this means is that a is a local variable of the function. On the contrary,b is assigned to the global scope.
The other trick of this question is that it doesn’t use strict mode ('use strict';) inside the function. If strict mode was enabled, the code would raise the error Uncaught ReferenceError: b is not defined. Remember that strict mode requires you to explicitly reference to the global scope if this was the intended behavior. So, you should write:
|
1 2 3 4 5 6 |
(function() { 'use strict'; var a = window.b = 5; })();
console.log(b); |
Define a repeatify function on the String object. The function accepts an integer that specifies how many times the string has to be repeated. The function returns the string repeated the number of times specified. For example
|
1 |
console.log('hello'.repeatify(3)); |
Should print hellohellohello.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin urna odio, aliquam vulputate faucibus id, elementum lobortis felis. Mauris urna dolor, placerat ac sagittis quis.