JavaScript Flashcards

1
Q

Can you name two programming paradigms important for JavaScript developers?

A
  • Imperative programming, with procedural and object-oriented programming paradigms supported
  • Functional programming, an example of which being closures
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the difference between classical inheritance and prototypal inheritance?

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What does map() do? (2)

A
  • 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What does reduce() do? (3)

A
  • 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).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What does filter() do? (2)

A
  • 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).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a stack? How would you implement a stack in JS? (3)

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is a queue? How would you implement a queue in JS? (3)

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What does call() do in JS? (2)

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What does apply() do in JS? (2)

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is lexical scope?

A

Scope created within a function, defined with var

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is block scope?

A

The keywords let and const define block scoped variables in pairs of curly brackets, otherwise JS doesn’t support block scope

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is scope chaining?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What sits at the top of every scope chain?

A

A global scope object, which is window in the browser and global in node

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is scope shadowing?

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is a closure?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is garbage collection?

A

When the JS runtime decides when/if to release any allocated memory.

17
Q

How does JS runtime garbage collect a closure? (2)

A
  • 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
18
Q

What does splice() do? (2)

A
  • 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
19
Q

What is hoisting? (2)

A
  • 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
20
Q

How does this work in JS?

A

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.

21
Q

What is a set in JS?

A

A set is a collection of items which are unique i.e no element can be repeated.

22
Q

What does spread (…) do in JS?

A
  • 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.
23
Q

Why would you use a Set in JS? (2)

A
  • When there are no repetitions

* When order doesn’t matter

24
Q

What are the advantages of declarative programming? (3)

A
  • 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
25
Q

What is transpilation?

A

Compiling source code into new source code, rather than into an executable

26
Q

What is hoisting?

A

All declarations, both variables and functions, are moved to the “top” of a scope and processed first before any part of your code is executed. Functions are hoisted first before variables.

27
Q

How does JS handle a variable declaration initialisation (e.g var a = 2;)

A

JS sees this as two statements: var a and then a = 2. Since (variable) declarations are hoisted, the declaration is processed during compilation and the rest is left in place for execution

28
Q

What is the difference between a function declaration and a function expression?

A
  • Simply, if the function keyword is the first keyword in a statement then it is a declaration, otherwise (e.g for an IIFE where ( precedes it, or when you do var a = function …) it is an expression.
  • Function declarations are hoisted, whereas function expressions are not
29
Q

What is meant by functions are “first class”?

A

Functions are a sub type of object, and can be handled like any other object (i.e can be passed as an argument to other functions, can be returned by another function and can be assigned as a value to a variable)

30
Q

What is a module?

A

A function (generally captialised) that returns an object, which contains references to inner functions, where the object acts as an API to the hidden and private inner functions.

31
Q

What are the requirements for a module? (2)

A
  • There must be an outer enclosing function, and it must be invoked at least once (each time creates a new module instance)
  • The enclosing function must return back at least one inner function, so that this inner function has closure over the private scope, and can access and/or modify that private state
32
Q

How are arrow function different from normal function declarations?

A

Arrow functions discard all the normal rules for this binding, and instead take on the this value of their immediate lexical enclosing scope.