Note:
The purpose of this quiz isn't to be a brain-buster. It is meant for recruiters to present to front-end developer candidates.
The intent is to weed out some "Just use jQuery" applicants, but allow those that know JavaScript to pass fairly easily.
-
When might comparative type coercion occur? How would you avoid it? How would you change a "falsy" or "truthy" value into a real boolean?
-
Describe how variable scope works. Explain how to create a closure using a self-executing anonymous function (also called IIFE: immediately-invoked function expression).
-
Explain briefly how prototypal inheritance differs from class-based, "classical" inheritance.
-
Describe how the "module pattern" works. Explain how the "revealing module pattern" expands upon it.
-
How does a client-side MVC (or MVVM) approach work? What is your preferred MV* JS framework?
-
Why do these yield different results?
'1' + 2 + 3 ; // Equals '123' 3 + 2 + '1'; // Equals '51' 3 + 2 + 1 ; // Equals 6
-
Why is
0.3
not the result of the following addition? How do you work around this peculiarity?0.1 + 0.2; // Equals 0.30000000000000004
-
Describe how variable hoisting works, and how to avoid bugs that may arise from it.
-
How do these differ?
function foo() {} // versus var foo = function() {};
-
When might you use a function's
call()
method, or itsapply()
method? -
Explain how to determine if a variable is an array or an object. (Hint:
typeof
lies!) -
In the following example, what is foo aliased to? (Hint: It is what
this
means.)(function(foo) { // What is 'foo' aliased to? })(this);
-
In JavaScript (and the DOM), some global variables are actually mutable, such as:
window
,document
, andundefined
. How would you write code to ensure these were predictably available for use? Assuming someone had injected this code, how would you work around it? (Hint: See the previous question.)var window = ''; var document = 0; var undefined = true;
-
In one line of code, how you would make a copy of an array?
-
What is the difference between
setInterval
andsetTimeout
? Bonus: What is the lowest cross-browser increment that each can accurately use? -
Explain how
delete
works. What types of things cannot be deleted? -
Describe how event delegation works, and when you should use it to handle UI interaction. Example markup…
<ul id="special"> <li> <a href="#">Special link 1</a> </li> <li> <a href="#">Special link 2</a> </li> <li> <a href="#">Special link 3</a> </li> </ul>
-
What does this snippet of code do?
var foo = bar ? bar : 0;
-
When might you write something like this, and what is it shorthand for?
foo && foo.bar();
-
How do
parseInt
andparseFloat
differ? When would you use a number'stoFixed()
method? In what instance might the following code snippet actually make sense to use?var my_number = my_string - 0;
-
Write a function named
add
that returns the total of any number of parameters. Example…// Should equal 15 add(1, 2, 3, 4, 5); // Should equal 0 add(5, null, -5); // Should equal 10 add('1.0', false, 1, true, 1, 'A', 1, 'B', 1, 'C', 1, 'D', 1, 'E', 1, 'F', 1, 'G', 1); // Should equal 0.3, not 0.30000000000000004 add(0.1, 0.2);