Frontend Interview Questions Flashcards

1
Q

What’s the difference between const, let and var?

A

Const is not immutable, but you can’t point a const to a different pointer, but you can add items to the array.

Let, you can change the pointer but it’s going to be scoped to whatever the closure is.

Var, is hoisted to the top. Var will throw undefined if accessed before, but const and let will throw reference error.

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

Explain prototypical inheritance.

A

JavaScript has a baseline prototype, which is the object. Everything inherits from the object.

Everything in JavaScript has a prototype and a baseline object that it inherits from. When you create a new object based on that object, you either have the choice to inherit or override the prototype’s properties.

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

What does “this” mean in JavaScript?

A

“This” is the global context of everything that is available to access to all the objects available to you that are not locally defined.

In most cases, the value of this is determined by how a function is called (runtime binding). It can’t be set by assignment during execution, and it may be different each time the function is called. ES5 introduced the bind() method to set the value of a function’s this regardless of how it’s called, and ES2015 introduced arrow functions which don’t provide their own this binding (it retains the this value of the enclosing lexical context).

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

What’s the data structure of the DOM?

A

A tree

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

How can you tell if image element is loaded on the page?

A

There’s an onload callback for image. If it’s loaded, it will fire that callback.

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

What are call and apply?

A

They are ways to change the scope when calling a function. Call is a series of elements and apply is an array.

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

What is event delegation?

A

Event listeners are really expensive memory-wise on a page, so event delegation is a way of improving performance by placing a listener on an parent element so that the actions performed on children elements will bubble up.

Another advantage is we don’t have to unbind listener for removed elements.

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

What is a worker?

A

Worked is something you would use in a browser to offload computationally expensive work to a different thread, since JavaScript is single-threaded. This way you don’t block the thread of the UI.

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

What’s a callback?

A

A callback is a function that is passed to another function and intended to be called at a later time. The most common use of callbacks is when executing asynchronous requests such as getting user input or requesting data over a network.

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

What is a closure?

A

A closure is the combination of a function and the lexical environment within which that function was declared.

Closures are functions that have access to their enclosing function’s variables scope even after the outer function has returned.

“Lexical” refers to the fact that lexical scoping uses the location where a variable is declared within the source code to determine where that variable is available.

Use cases: partial applications or currying.

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

Explain the execution context, including lexical scope.

A

The execution context is the environment / scope that the current code is being evaluated in.

5 main aspects:

  • Single threaded.
  • Synchronous execution.
  • 1 Global context.
  • Infinite function contexts.
  • Each function call creates a new execution context, even a call to itself.

Think about lexical scope in terms of the physical location of the code where it is first written. The function or code will have access to the data in the original location it was declared.

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

What is hoisting?

A

Explain “hoisting”.
Hoisting is a term used to explain the behavior of variable declarations in your code. Variables declared or initialized with the var keyword will have their declaration “moved” up to the top of their module/function-level scope, which we refer to as hoisting. However, only the declaration is hoisted, the assignment (if there is one), will stay where it is.

Note that the declaration is not actually moved - the JavaScript engine parses the declarations during compilation and becomes aware of declarations and their scopes. It is just easier to understand this behavior by visualizing the declarations as being hoisted to the top of their scope. Let’s explain with a few examples.

console.log(foo); // undefined
var foo = 1;
console.log(foo); // 1
Function declarations have the body hoisted while the function expressions (written in the form of variable declarations) only has the variable declaration hoisted.
// Function Declaration
console.log(foo); // [Function: foo]
foo(); // 'FOOOOO'
function foo() {
  console.log("FOOOOO");
}
console.log(foo); // [Function: foo]
// Function Expression
console.log(bar); // undefined
bar(); // Uncaught TypeError: bar is not a function
var bar = function() {
  console.log("BARRRR");
};
console.log(bar); // [Function: bar]
Variables declared via let and const are hoisted as well. However, unlike var and function, they are not initialized and accessing them before the declaration will result in a ReferenceError exception. The variable is in a "temporal dead zone" from the start of the block until the declaration is processed.

x; // undefined
y; // Reference error: y is not defined

var x = "local";
let y = "local";
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Function expressions vs declarations.

A
Expression: var foo = function() {}. Only declaration is hoisted, not body
Function: function foo() {} body is hoisted
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What’s the difference between bind, call and apply?

A

Bind returns a new function bound to the context given as an argument. Call and apply invoke the function with the applicable context. Apply takes and array and call takes a series of elements as arguments.

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

What are object constructor properties and functions?

A

The constructor property returns a reference to the Object constructor function that created the instance object. Note that the value of this property is a reference to the function itself, not a string containing the function’s name.

Constructor functions are functions that serve as a “blueprint” for creating multiple instances of a given type of object.

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

What are mixins?

A

A mixin is a class containing methods that can be used by other classes without a need to inherit from it. In other words, a mixin provides methods that implement a certain behavior, but we do not use it alone, we use it to add the behavior to other classes.

17
Q

What is object composition?

A

Composition is about taking simple objects and combining them to build more complex ones. To build a Lamborghini you might define a function for constructing essential features like engine, design and brakes. This creates a relationship of a Lamborghini has a engine, brakes and design.

18
Q

What are higher order functions?

A

Higher order functions are functions that take in another function as an argument or return a function as a result.