JavaScript Interview Questions
July 24, 2015
- Explain event delegation
- Explain how 'this' works in JavaScript
- Explain how prototype inheritance works
- What do you think of AMD vs CommonJS?
- Explain why the following doesn't work as an IIFE: function foo(){ }();.
- What's the difference between a variable that is: null, undefined or undeclared? How would you go about checking for any of these states?
- What is a closure, and how/why would you use one?
- What's a typical use case for anonymous functions?
- How do you organize your code? (module pattern, classical inheritance?)
- What's the difference between host objects and native objects?
- Difference between: function Person(){}, var person = Person(), and var person = new Person()?
- What's the difference between .call and .apply?
- Explain Function.prototype.bind.
- When would you use document.write()?
- What's the difference between feature detection, feature inference, and using the UA string?
- Explain AJAX in as much detail as possible.
- Explain how JSONP works (and how it's not really AJAX).
- Have you ever used JavaScript templating?
- If so, what libraries have you used?
- Explain "hoisting".
- Describe event bubbling.
- What's the difference between an "attribute" and a "property"?
- Why is extending built-in JavaScript objects not a good idea?
- Difference between document load event and document ready event?
- What is the difference between == and ===?
- Explain the same-origin policy with regards to JavaScript.
- Make this work: duplicate([1,2,3,4,5]); // [1,2,3,4,5,1,2,3,4,5]
- Why is it called a Ternary expression, what does the word "Ternary" indicate?
- What is "use strict";? what are the advantages and disadvantages to using it?
- 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
- Why is it, in general, a good idea to leave the global scope of a website as-is and never touch it?
- Why would you use something like the load event? Does this event have disadvantages? Do you know any alternatives, and why would you use those?
- Explain what a single page app is and how to make one SEO-friendly.
- What is the extent of your experience with Promises and/or their polyfills?
- What are the pros and cons of using Promises instead of callbacks?
- What are some of the advantages/disadvantages of writing JavaScript code in a language that compiles to JavaScript?
- What tools and techniques do you use debugging JavaScript code?
- What language constructions do you use for iterating over object properties and array items?
- Explain the difference between mutable and immutable objects.
- What is an example of an immutable object in JavaScript?
- What are the pros and cons of immutability?
- How can you achieve immutability in your own code?
Event delegation is tagetting element may not exist in current DOM and attach the event when they show up. Here is the example. $(this) is the special variable represents the target element.
This function will get the element of '.dog-group' and target 'li' and attach the function which addClass 'highlight'.
'This' is in general referring to the objects that the function can be called. For instance within a function this.name = name; means whatever instance of the object's name will be called. In summary, 'this' in general refer something left side of dog notation. If not, it is usually refer 'window'. Because everything ouside of the scoup of the function belongs to 'window'
In object oriented concept, prototype will inherite whatever right side of prototype notation to be inherited to left side of the object. prototype can also inherite constructor function so that left side of the object can be a constructor for other objects with 'new' keyword.
First, to invoke the function we need to wrap the whole function within '()'. so (foo(){})(); this could invoke the function immediately.
variable = null; is intentionally to make the value as a null. variable = undefined is something is declared but not assigned. For instance,
Because it is simply undefined because the scope of var its_sound is only valid inside of Foo function.
To simply put, a closure is the function that returns other function to reveal variables inside of the function. The reason this is important is that we can call the function later (or as a matter of fact) whenever we want.
For this case, the returned function will access variable 'bar' and print out.
Anoymous function is useful when it is asigned to other varialbes. This is called Anonymous Function Expression. Look at this example.
When we call addNumber(4,5), addNumber variable is invoking anonymous function to add two parameters.
function Person(){} is a constructor function so that can be instanciate other Person type object. var person = new Person() is exactly how to do it. person is going to inherite all of traits from Person constructor.
.call will call the function with arguments that are passing. The difference between .call and .apply is that apply will take array as an arguments.
JSONP is 'JSON with padding'. It's the hacky way of calling XMLHTTPRequest without violating SOP(Same Origin Policy). SOP prevent any unknown server calling API request so JSONP with callback function is pretending the request calls are from a server.
Better Interpretation: JSONP is a communication technique used in JavaScript programs running in web browsers to request data from a server in a different domain, something prohibited by typical web browsers because of the same-origin policy(SOP).
If function is created with the'var' keyward, it will be hosisted to the top of the function so it can be called wherever in the script.
This is caused by hoisting. var message; is hoisted but the expression is not so it resulted in undefined.
When there is series of targets selected for the event, it triggers for all the elements. For instance,
In short, unless you know exactly what you are doing, you should never use'=='. '===' which is strick equal sign confirms that the two objects in right and left sides are same in every way like type and value. In performance wise, to determine two objects are '==', a machine have to go through significant process to try to match up two objects. The example of how dangerous to use '==' is '1' == 1 returns 'true'
Probably because global scope can be used literally globally, you will never know where it is ended up.