JavaScript Interview Questions
,JavaScript Interview Questions ii
Contents
1 Javascript: An Overview 1
1.1 What is Javascript? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 Javascript Code Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.2.1 Implementing Javascript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.2.2 Writing Javascript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2.3 Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.3 Javascript Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2 Theoretical Questions 5
2.1 Can you name two programming paradigms important for JavaScript app developers? . . . . . . . . . . . . . . . 5
2.2 What is functional programming? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.3 What is the difference between classical inheritance and prototypal inheritance? . . . . . . . . . . . . . . . . . . 5
2.4 When is prototypal inheritance an appropriate choice? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.5 What is asynchronous programming, and why is it important in JavaScript? . . . . . . . . . . . . . . . . . . . . 6
2.6 What are JavaScript data types? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.7 What is the difference between "==" and "==="? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.8 What is an undefined value in JavaScript? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.9 What are the different types of errors in JavaScript? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.10 Define event bubbling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.11 What is the significance, and what are the benefits, of including use strict at the beginning of a JavaScript source
file? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.12 What are Screen objects? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
3 Code Output Questions 9
3.1 What will the code below output to the console and why? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
3.1.1 Answer: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
3.2 What will the code below output to the console and why? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
3.3 What will the code below output? Explain your answer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
3.4 Consider the following code snippet: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
3.4.1 Answer: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
3.5 What will the code below output to the console and why? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
,JavaScript Interview Questions iii
3.5.1 Answer: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
3.6 The following recursive code will cause a stack overflow if the array list is too large. How can you fix this and
still retain the recursive pattern? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
3.6.1 Answer: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
3.7 What will the code below output to the console and why? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
3.7.1 Answer: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
3.8 What will be the output when the following code is executed? Explain. . . . . . . . . . . . . . . . . . . . . . . . 13
3.8.1 Answer: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
4 Write Code Questions 14
4.1 What will the code below output to the console and why? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
4.1.1 Answer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
4.2 Write a sum method which will work properly when invoked using either syntax below. . . . . . . . . . . . . . . 14
4.3 Write a simple function (less than 80 characters) that returns a boolean indicating whether or not a string is a
palindrome. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
4.4 Functional Javascript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
4.4.1 Given the following array, build me an array of cars with those colours: . . . . . . . . . . . . . . . . . . 15
4.4.1.1 Answer: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
4.5 Dynamic Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
4.5.1 I’ve created an array of 1000 cars. When I call run on the first car, I want it to run as normal. When I call
run on any of the others and any cars created in the future, I want it to run as normal, but also log The
color car is now running to the console. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
4.5.1.1 Answer: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
4.6 Binding Shim . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
4.6.1 Can you shim the "bind" function? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
4.6.1.1 Answer: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
4.7 Animation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
4.7.1 How could you implement moveLeft animation? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
4.7.1.1 Answer: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
4.8 Memorization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
4.8.1 How could you implement cache to save calculation time for a recursive fibonacci function? . . . . . . . 16
4.8.1.1 Answer: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
4.8.2 How could you cache execution of any function? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
4.8.2.1 Answer: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
4.8.3 How could you set a prefix before everything you log? for example, if you log(my message) it will log:
"(app) my message"? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
4.8.3.1 Answer: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
, JavaScript Interview Questions iv
5 Rapid Fire - Tricky Questions 18
5.1 Is false false? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
5.2 Is ’ ’ false? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
5.3 Is Boolean(function(){}) true or false? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
5.4 What is 2 + true? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
5.5 What is 6+9? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
5.6 What is the value of +dude? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
5.7 If you have var y = 1, x = y = typeof x; What is the value of x? . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
5.8 If var a = (2, 3, 5); what is the value of a? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
5.9 What is -5%2? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
5.10 What is typeof arguments? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
6 Essential Questions 20
6.1 What is the drawback of creating true private methods in JavaScript? . . . . . . . . . . . . . . . . . . . . . . . . 20
6.2 What will be the output of the code below? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
6.3 What is a "closure" in JavaScript? Provide an example. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
6.4 Write a mul function which will produce the following outputs when invoked: . . . . . . . . . . . . . . . . . . . 21
6.5 How to empty an array in JavaScript? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
6.5.1 One Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
6.6 What will be the output of the following code? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
6.7 What is the difference between the function declarations below? . . . . . . . . . . . . . . . . . . . . . . . . . . 22
6.8 What will be the output of the code below? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
6.9 Consider the following code: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
6.10 Hoisting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
6.10.1 What’s the result of executing this code and why? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
7 General Questions 25
7.1 What is ‘this’ keyword in JavaScript? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
7.2 What is the difference between ViewState and SessionState? . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
7.3 Does JavaScript support automatic type conversion? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
7.4 How can you read and write a file using JavaScript? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
7.5 How can you read and write a file using JavaScript? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
7.6 What is the use of Void(0)? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
7.7 What are JavaScript Cookies? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
7.8 How can a particular frame be targeted, from a hyperlink, in JavaScript? . . . . . . . . . . . . . . . . . . . . . . 26
7.9 Explain the role of deferred scripts in JavaScript? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
7.10 How can a particular frame be targeted, from a hyperlink, in JavaScript? . . . . . . . . . . . . . . . . . . . . . . 26
7.11 What does a timer do and how would you implement one? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
7.12 Explain the concept of unobtrusive Javascript? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
7.13 What is "event delegation" and how does it work? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
7.14 What is the importance of tag? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
7.15 What is the difference between window.onload and onDocumentReady? . . . . . . . . . . . . . . . . . . . . . . 27
8 A Final Note 28