Javascript Interview Questions Flashcards

1
Q

What is functional programming?

A

It’s the idea that functions should be pure functions, that they should not mutate state, that they should have no side effects. Some features are that functions can be used as values and as arguments.

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

What is the difference between scope and context?

A

Scope refers to the lifetime or access of a variable when a function is invoked.

Context is related to the object. Context is the value of the ‘this’ keyword.

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

What is a closure?

A

An inner function that has access to the outer (enclosing) function’s variables—scope chain. The closure has three scope chains: it has access to its own scope (variables defined between its curly brackets), it has access to the outer function’s variables, and it has access to the global variables

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

How to clone an object?

A

Object.assign({},obj);

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

What are some properties of functional programming?

A

Pure functions / function purity.
Avoid side-effects.
Simple function composition.

not mutating state, writing pure functions. It’s more scalable, manageable, its more scalable to store someone’s age as current year - minus birth year

Mention of features that support FP: first-class functions, higher order functions, functions as arguments/values.

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

What are Javascript is a multi-paradigm language.

A

Javascript is a multi-paradigm language. supporting imperative/procedural programming along with OOP (Object-Oriented Programming) and functional programming. JavaScript supports OOP with prototypal inheritance.

Prototypal inheritance (also: prototypes, OLOO).
Functional programming (also: closures, first class functions, lambdas).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Redux Actions? What does dispatch do?

A

Dispatch sends actions from the store. Actions are payloads of information that send data from your application to your store. They are the only source of information for the store.

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

What are “actions” in Redux?

A

In a nutshell, actions are events. Actions send data from the application (user interactions, internal events such as API calls, and form submissions) to the store. The store gets information only from actions. .

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

What are action creators?

A

Actions are created with action creators. That sounds obvious, I know. They are just functions that return actions.

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

What are reducers?

A

In Redux, reducers are functions (pure) that take the current state of the application and an action and then return a new state. They take the current state and an action as arguments and then returns the next state.

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

What is the Redux Store?

A

Store is the object that holds the application state and provides a few helper methods to access the state, dispatch actions and register listeners

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

How to change state in Redux?

A

The only way to change the state is to emit an action, an object describing what happened.

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

What can you do in componentDidMount?

A

Starting AJAX calls to load in data for your component.

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

componentWillReceiveProps

A

we have access to both the next props (via nextProps), and our current props (via this.props).

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

componentDidUpdate

A

Use when updating the DOM in response to prop or state changes.

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

What’s the box model?

A

Margin border padding content

17
Q

What’s prototypal inheritance?

A

instances inherit directly from other objects. Instances are typically instantiated via factory functions or Object.create().

18
Q

Class inheritance?

A

Instances are typically instantiated via constructor functions with the new keyword.

19
Q

What is a closure?

A

A closure is an inner function that has access to the variables in the outer (enclosing) function’s scope chain. The closure has access to variables in three scopes; specifically: (1) variable in its own scope, (2) variables in the enclosing function’s scope, and (3) global variables.
It remembers the environment it was defined in.

Before, functions are just stacked on top.

Advanced languages Now they can be on the heap. Functions are objects, remember the context they are in. It’s you wouldn’t have to worry about the memory allocation of language. now it can be on the heap and you don’t have to worry. Old language you had to allocate memory for it, and free it later. you have to remember the pointer. function in C you create it, call it and it disappears from the free store.

functions act like objects and the variable that it captures are like the constructor arguments.

20
Q
Write a function that would allow this. 
var addSix = createBase(6);
addSix(10); // returns 16
addSix(21); // returns 27
A
function createBase(baseNumber) {
  return function(N) {
    // we are referencing baseNumber here even though it was declared
    // outside of this function. Closures allow us to do this in JavaScript
    return baseNumber + N;
  }
}

var addSix = createBase(6);
addSix(10);
addSix(21);

21
Q

How do you clone an object?

A

Object.assign({},obj);

22
Q

Write a function that will loop through a list of integers and print the index of each element after a 3 second delay.

A
const arr = [10, 12, 15, 21];
for (let i = 0; i < arr.length; i++) {
  // using the ES6 let syntax, it creates a new binding
  // every single time the function is called
  // read more here: http://exploringjs.com/es6/ch_variables.html#sec_let-const-loop-heads
  setTimeout(function() {
    console.log('The index of this number is: ' + i);
  }, 3000);
}