I thought I might as well blog this. These are notes for a Udemy course titled JavaScript Interview Prep. The answers below are coded out on the Repl.it and Codepen websites. I have added hyperlinks to each of the code examples.
Also check out the following resources for practice algorithms: JS: Interview Algorithm; Learning Algorithms in Javascript From Scratch; Coderbyte challenges; Khan Academy JS interview questions; Tech Interview Handbook; etc. Also check out freeCodeCamp.org for a whole bunch of algorithm challenges, or for a pretty good overview of algorithms in computer science.
- Triple Add Function: tripleAdd(10)(20)(30) // returns total of all 3 numbers added together
- What is an IIFE, and why are they used?: Immediately invoked function expression. It declares a function and then calls it immediately. Used to take advantage of the local scope of variables inside the function. Example in Repl.it
- Source code guessing: Given some source code, guess what will happen when the various buttons are pushed.
- Closures: What is a closure? Code out an example of a closure. My Answer: a closure defines a function in a specific scope so that it explicitly has access to that scope. Instructor’s Answer: A closure is an inner function that has access to the scope of an enclosed function.
- ‘This’ keyword: What is the ‘this’ keyword in JavaScript, and how is it used? My Answer: The ‘this’ keyword relates to functions that are properties of objects.
- Hoisting: Describe what variable and function hoisting is and how it works. Answer: Variables and functions are hoisted to the top of the scope in which they are declared. Essentially, you can’t call or log out a variable or function before it is defined. Also, bear in mind that
let
andconst
are block scoped, not function or global scoped. - Scope and ‘self’ question: Look at the given code and guess at what is logged out to the console. NOTE: The code explains it all.
- == vs ===: What is the difference between
==
and===
? Answer:==
stands for ‘equals’ and tests for abstract equality (doesn’t test for data type), whereas===
stands for ‘strict equals’ and tests for strict equality (tests for data type). - Log number function: Look at the given source code and determine what will be logged to the console. Answer: It logs as undefined, probably because of the keyword
var
being used twice. The secondvar
is function scoped, sovar num
is hoisted to the top of the function, but not defined until after theconsole.log
. - Use strict: What does
use strict
do to code, and what are the benefits of using it? Answer: Enforces stricter parsing and error handling in the code. It prevents the use of global variables; enforces that all function parameters must be unique; prevents deleting Object properties that are important. Essentially,use strict
throws errors when we do bad stuff with our code, informing us early of errors that could throw off our code later. - Curry function: Given some starter code, curry the function. Answer: Function with multiple parameters should be re-written as multiple, embedded functions with one parameter each.
- Counter function: Write a function that keeps track of how many times it was called and returns the number. Answer in the code.
- Logging X and Y: Looking at the example code, what are the values of
X
andY
when they are logged out? Answer is in the code example. - Call & apply methods: Describe the JavaScript
call()
andapply()
methods. How do they function? What arguments do they take? How are they different? Answers: Both are predefined JavaScript methods. Thecall()
method can be used to call a function with an object as an argument. Theapply()
method is similar, but it takes an array as an argument. - Determine ‘list2’: Look at example code, and determine what
list2
will contain when it is logged to the console. Then, determine howlist2
can refer tolist1
without the extra elements added by thepush()
method. - Singly and doubly invoked functions: create a function (see source code) that can be either singly invoked or doubly invoked. Answer in the code.
- JSON data: Describe what JSON format is. Given source code, delete the data types not permitted in JSON, and replace placeholder text with the corresponding data type, properly formatted as JSON. Answer in the code.
- Order logged out: Given source code, determine in what order the numbers 1, 2, 3, 4 will be logged. Answer in the code.
- Making an object: List and describe three different ways of making an object. Answer in the code.
- Constructor functions: This isn’t a separate question, but, rather, a continuation of notes from the last question. Check it out.
- Type of data types: Given some source code, determine what will be logged out for each
console.log
statement. Answer in the code. - Bind method: Describe the
bind()
function method. How does it work? What parameters does it take? Code out an example of how it is used. Answers: From MDN, “Thebind()
method creates a new function that, when called, has itsthis
keyword set to the provided value, with a given sequence of arguments preceding any provided when the new function is called.” See code example. - Two Objects: Given source code, determine what is logged out at the bottom. Answer in the code.
- Array constructor: Given source code, determine what is logged out for each of the constructor statements. Answer in the code.
- Array indexOf: Given source code which uses the
indexOf()
function, determine what will be logged out to the screen for eachconsole.log()
. Answers in the code. - Equivalent numbers: Given source code, determine what will be logged out to the console. Answer in the code.
- Objects and strings: Given source code, determine what is logged out. Answer in the code.
- Strings and arrays: Given source code, determine what will be logged out at the bottom. Answers in the code.
- Object properties: Note that this is a very common interview question. Given source code, determine what will be logged out at the bottom. Answers in the code.
- X and Y: Given source code, determine what will be logged out at the bottom. Answers in the code.
- Withdraw from account: Given source code, determine what will be logged out at the bottom. Answers in the code.