JavaScript Flashcards

1
Q

What is the difference between class and prototypal inheritance?

A

Class inheritance: Classes are like a blueprint, a description of the object to be created. Classes inherit from classes and create subclass relationships

Prototypal inheritance: A prototype is a working object instance. Objects inherit directly from other objects.

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

What is a Closure?

A

A closure gives you access to an outer function’s scope from an inner function.

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

Use cases for closures?

A
  1. Data Privacy / Emulating Private Methods: Closures are commonly used to give objects data privacy. By utilizing closures, we can make it so we can’t get data except through the object’s methods. That way, malicious code can not interact with the data.
  2. Event Handlers and Callbacks: JavaScript is heavy on asynchronous operations, and closures are often used in event handlers and callbacks.
  3. Factory functions are functions that return other functions. Closures are especially useful here because they allow each returned function to have its own private state.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is a pure function?

A

A pure function is a which which:
- Given the same input, always returns the same output.
- Produces no side effects, which means that it can’t alter any external state

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

Motivations for pure functions?

A
  • Avoiding shared state (can cause bugs difficult to debug)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is hoisting?

A

Hoisting allows you to use functions and variables before they’re declared.

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

What is functional programming?

A

Functional programming is the process of building software by composing pure functions, avoiding shared state, and side effects.

  • Declarative rather than imperative
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Benefits/motivations for functional programming

A

More concise, more predictable, and easier to test than OOP code.

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

What is a promise?

A

A promise is an object that may produce a single value some time in the future, either a resolved value, or a reason it’s not resolved.

Promises are how javascript handles asynchronous code/operations.

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

3 states of a promise?

A

Pending, fullfilled, rejected

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

null vs undefined

A

undefined means value of the variable is not defined.

null means empty/non-existent

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

If you want to use an arbitrary object as value of this, how will you do that?

A

There are at least three different ways to doing this by using bind, call and apply.

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

What does bind do?

A

bind allows you to borrow a method and set the value of this without calling the function. It simply returns an exact copy of the function with new value of this

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

What is this in JavaScript?

A

At the time of execution of every function, JavaScript engine sets a property to the function called this which refer to the current execution context. this is always refer to an object and depends on how function is called

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

What is currying in JavaScript?

A

currying is when a function — instead of taking all arguments at one time — takes the first one and returns a new function, which takes the second one and returns a new function, which takes the third one, etc. until all arguments are completed.

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

What is currying in JavaScript?

A

currying is when a function — instead of taking all arguments at one time — takes the first one and returns a new function, which takes the second one and returns a new function, which takes the third one, etc. until all arguments are completed.

16
Q

When should someone use currying?

A
  • It divides your function into multiple smaller functions that can handle one responsibility, which makes errors less likely
  • It is used in functional programming to create a higher-order function
17
Q

What is the prototype chain?

A

The prototype chain in JavaScript is a series of linked prototype objects that forms a chain-like structure. This chain is used in the lookup process when the program needs to access a property or a method of an object: the property is looked up not only on the object itself but also on its prototype, the prototype’s prototype, and so on, until the property is found or the chain ends. At the end of this chain is Object.prototype, whose prototype is null, signifying the end of the chain.

18
Q

What is a callback?

A

A callback is a function that gets passed as an argument to another function and is executed after some operation has been completed.

19
Q

What was the motivation for creating Promises?

A

Promises were introduced in ES6 to solve the problems of callback hell (nested callbacks).

20
Q

What is a promise?

A

A Promise represents a value which may not be available yet, but will be resolved at some point in the future or rejected entirely.

21
Q

What is the .then() method?

A

then() is a method that takes a callback for when the Promise is resolved

22
Q

What is the .catch() method?

A

catch() is a method that takes a callback for when the Promise is rejected.

23
Q

What is the async keyword used for?

A

An async function is a function that knows how to expect the possibility of the await keyword being used within it

24
Q

What is the await keyword used for?

A

The await keyword can only be used inside an async function and pauses the function execution until the Promise is resolved or rejected.

25
Q

What is strict mode in JavaScript?

A

A way to opt into a restricted variant of JavaScript, reducing bugs and making code easier to understand and predict.

It helps prevent errors by catching common coding mistakes and “unsafe” actions like using undeclared variables.

26
Q

What does it mean for JavaScript to be multi-paradigm?

A

JavaScript allows developers to write code using various programming styles, like object-oriented or functional, often within the same program.

27
Q

What is JavaScript’s relationship with ECMAScript?

A

ECMAScript is the standard that JavaScript follows.

28
Q

What is the purpose of the TC39 committee?

A

TC39 is a group of JavaScript developers, implementers, academics, and more, collaborating with the community to maintain and evolve the definition of JavaScript. They propose and approve changes and additions to the standard.

29
Q

How does JavaScript perceive each .js file in an application?

A

JavaScript treats each .js file as a separate program.

30
Q

What is the “global scope” in JavaScript and how is it used?

A

The “global scope” is a space where JS programs can share their state and functionality. It allows separate JS programs (or files) to cooperate and act as a single program.

31
Q

What is a module in JavaScript (from ES6 onwards)?

A

A module in JavaScript is a block of code that can be treated as a single unit. It can be loaded via mechanisms such as import statements or

 tags.