JavaScript Flashcards
Can you name two programming paradigms important for JavaScript developers?
- Imperative programming, with procedural and object-oriented programming paradigms supported
- Functional programming, an example of which being closures
What is the difference between classical inheritance and prototypal inheritance?
- Inheriting from a class means an object will have the attributes and methods that the super class defines
- Prototype inheritance means that an object will have a reference to attributes and methods that the prototype has, and uses these as a fallback
What does map() do? (2)
- An array method, the map() method creates a new array with the results of calling a function for every array element.
- The map() method calls the provided function once for each element in an array, in order.
What does reduce() do? (3)
- An array method, the reduce() method reduces the array to a single value.
- The reduce() method executes a provided function for each value of the array (from left-to-right).
- The return value of the function is stored in an accumulator (result/total).
What does filter() do? (2)
- An array method, the filter() method creates an array filled with all array elements that pass a test (provided as a function).
- The filter() method executes a provided test for each value of the array (from left-to-right).
What is a stack? How would you implement a stack in JS? (3)
- A stack is a FILO data structure, can be an array in JS
- Use the pop() method to remove a value from the top of the stack
- Use the push() method to add value(s) to the top of the stack
What is a queue? How would you implement a queue in JS? (3)
- A queue is a FIFO data structure, can be an array in JS
- Use the shift() method to remove a value from the front of the queue
- Use the unshift method to add value(s) to the front of the stack
What does call() do in JS? (2)
- It can be used to invoke (call) a method with an owner object as an argument (parameter).
- What this means is if a method includes
this
, then this will refer to the argument object - It takes arguments separately, i.e if there are n values then there are n arguments
What does apply() do in JS? (2)
- It can be used to invoke (call) a method with an owner object as an argument (parameter).
- What this means is if a method includes
this
, then this will refer to the argument object - It takes arguments as an array, i.e if there are n values then the argument is an array of n elements
What is lexical scope?
Scope created within a function, defined with var
What is block scope?
The keywords let
and const
define block scoped variables in pairs of curly brackets, otherwise JS doesn’t support block scope
What is scope chaining?
If you think of nested scoping as a tree structure, where a function b() nested in a() is a child node to a(), a scope chain is the path from a node to the root node
What sits at the top of every scope chain?
A global scope object, which is window
in the browser and global
in node
What is scope shadowing?
In a nested function, declaring a lexically scoped variable with the same name as a lexically scoped variable in the parent function is valid, and the nested function only has access to it’s locally scoped variable (normally it would have access to the parents variable too)
What is a closure?
If an inner function accesses a value in an outer function, it is said to close over that value, hence is a closure. This inner function can then be returned from the outer function, and still access the outer variable.
In other words, closure is when a function is able to remember and access its lexical scope even when that function is executing outside its lexical scope.
What is garbage collection?
When the JS runtime decides when/if to release any allocated memory.
How does JS runtime garbage collect a closure? (2)
- For a closure to correctly resolve its reference to the closed over variable, not only does the memory for the variable need to be kept but the scope chain must also be kept
- Once the closure is no longer required, it is marked for garbage collection, followed by the closed over variable and finally the scope chain
What does splice() do? (2)
- An array method, to add/remove items to/from an array, and returns the removed item(s).
- Pass starting index and number to remove, or negative index from end of array
What is hoisting? (2)
- Hoisting is the JavaScript interpreter’s action of moving all variable and function declarations to the top of the current scope.
- However, only declarations are hoisted, and it’s legal to refer to a variable before its defined, so it’s possible to refer to a variable that is undefined
How does this work in JS?
The context of a function, what is referred with the this
keyword, in JavaScript depends on how a function is invoked, not how it’s defined.
What is a set in JS?
A set is a collection of items which are unique i.e no element can be repeated.
What does spread (…) do in JS?
- Spread syntax allows an iterable such as an array expression or string to be expanded in places where zero or more arguments (for function calls) or elements (for array literals) are expected
- It can also be used for an object expression to be expanded in places where zero or more key-value pairs (for object literals) are expected.
Why would you use a Set in JS? (2)
- When there are no repetitions
* When order doesn’t matter
What are the advantages of declarative programming? (3)
- Generally terser and easier to read, which aids readability and maintainability in large codebases
- Often avoid creating and mutating state, so less need for unnecessary variable and storage
- You only need to describe what you want to achieve, and there’s no need to list instructions on how to achieve it