JavaScript - The Advanced Concepts Flashcards

1
Q

What does it mean that JavaScript is a non-blocking language?

A

It means that while it’s a single-threaded language, it can have asynchronous behavior (so that a asynchronous line doesn’t block the rest of the code).

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

Is JavaScript compiled or interpreted?

A

It’s compiled through JIT compilers that try to combine aspects from compiled and interpreted languages.

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

How does the JIT compiler works?

A

It checks for possible code optimizations and compile them on the fly

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

Which expressions are hard to optimize ?

A

eval, arguments, for…in, with and delete

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

What should you do with objects to optimize hidden classes ?

A

Add all properties in the same order for similar objects

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

How does hidden classes work?

A

Every object has a hidden class. Once a new property is added, a new hidden class is created and a transition between them is stablished.

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

How is the garbage collection done in JS ?

A

Mark and Sweep (mark all objects that are referenced in memory, sweep the ones that weren’t marked before).

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

Which are some of the most common causes of memory leaks in JS?

A

Use of global variables, not removing obsolete event listeners.

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

How can you access the methods from the web API?

A

With the “window” command.

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

When is the callback queue executed ?

A

When the callstack is empty

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

What is the predominant scope used in JS?

A

Static (lexical) scope

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

When does hoisting happen ?

A

Every time a new execution context is created

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

Which keywords are hoisted?

A

Functions are fully hoisted; vars are partially hoisted.

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

What’s the most efficient way to iterate through objects?

A

Use Object.keys (obj) to get the keys in a separate array and iterating through it.

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

Why are the “eval” and “with” keywords hard to optimize?

A

Because they distort how the variable scope works.

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

How can you check the scope of a function ?

A

Through the [[scope]] block.

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

What happens when you create a variable without a “var”, “let” or “const” identifier?

A

It is created on the global scope.

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

Is the “var” keyword local, function or global scoped ?

A

function

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

Is the “let” keyword local, function or global scoped ?

A

local

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

What’s the syntax of IIFEs?

A

(function(…) {…} ) () ;

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

Are IIFEs hoisted? Why?

A

No, because they don’t start with the “function” keyword.

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

What’s the biggest advatage of IIFEs?

A

It helps keeping the global namespace clean by encapsulating all variables and functions inside of a single globally scoped variable.

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

Is the “this” keyword lexically or dynamically scoped?

A

Dynamically

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

Is it possible to change the scope of the “this” keyword ? How?

A

You change the scope of the “this” keyword to lexical scope when you use it inside of an arrow function.

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

” ‘function (x) {…}’ is equivalent to ‘(x) => {…}’ “. True or false statement?

A

False. On the first one, “this” will have dynamic scope. In the second, it has lexical scope. The second one also doesn’t have access to the “arguments” keyword.

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

What method is called when you run a function ?

A

The call ( ) method from the same function you are trying to run.

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

What are the two most commonly used ways of sharing the same method between different objects ?

A

Using the “this” keyword inside the method or through method.apply( ).

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

What’s the meaning of the first parameter of the call () method?

A

The object at which the function should be called upon. If null is passed, it will be called upon the window object.

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

What does “function.bind()” does ?

A

It binds the “this” keyword inside the function to the object passed as a parameter.

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

“Functions in JS are just objects”. True or False ?

A

True

31
Q

Is JS strongly or weakly typed ? What does it mean?

A

Weakly. It means that the language is more lenient when dealing with operations with different types

32
Q

Is JS statically or dynamically typed? What does it mean?

A

Dynamically. It means the type of the variables are defined on execution instead of compilation.

33
Q

Functions are which kind of object?

A

Callable objects

34
Q

Why are closures needed in JS?

A

They are needed to guarantee that any functions declared inside another function can have access to their variables even if the parent function was already executed and is not on memory anymore.

35
Q

What are the biggest advantages of using closures ?

A

Time and memory efficiency. They also permit the creation of private variables in a OOP context.

36
Q

What kind of inheritance is implemented in JS?

A

Prototypal Inheritance

37
Q

Which type in JS has the “prototype” property?

A

Functions

38
Q

“The reccomended way of inheriting a prototype is by changing the __proto__ property to the desired parent object”. True or False?

A

False. The preferred method is through “Object.create(parent)”

39
Q

“When checking for a property in an object, JS will start with the top of the prototype chain and follow down until the object”. True or False?

A

False. It’s the opposite direction.

40
Q

What’s the biggest advantage of prototypal inheritance over classic inheritance?

A

On classical inheritance, only classes can be inherited from classes. Prototypes can be inherited by other prototypes and objects.

41
Q

What’s the main use of prototypes in JS?

A

Simulating OOP.

42
Q

How can you create objects (in OOP sense) in JS?

A

Through “Object.create(constructor)”

43
Q

How can you add properties to constructors in JS?

A

By assigning them to the function prototype

44
Q

Which operator is used to create OOP objects in JS?

A

The “new” operator.

45
Q

“When you create two different objects from the same prototype, their functions will be pointing to the same memory address”. True or False?

A

True if the function was added to the prototype, false if it was created inside the constructor itself.

46
Q

“The only way to add variables to constructors is through the ‘this’ keyword”. True or False?

A

True

47
Q

What’s the difference between imperative and declarative programming?

A

Imperative is defining what to do and how to do. Declarative is about telling what to do and what’s the expected result.

48
Q

Which Functional Programming principle does memoization use?

A

Memoization works through caching the result of previous calls to make future calls faster. This requires the idempotence principle to work (the idea that a function should return the same output for the same input).

49
Q

How can you create private variables in JavaScript?

A

By using closures to encapsulate them.

50
Q

What does currying means?

A

It means taking a function that receives n arguments and turning it into n functions that receive one argument and return a function for the next.

51
Q

What’s the difference between currying and partial application?

A

Currying is a partial application one argument at a time. Partial application can also be used to create a function that receives only the first argument and return a function that receives all the others simultaneously.

52
Q

What’s the meaning of arity?

A

The amount of parameters a function receives

53
Q

How can you improve the arity of a function?

A

By converting it into a function that applies the argument only partially.

54
Q

When should Functional Programming be preferred to Object Oriented Programming?

A

When you are dealing with massive amounts of data.

55
Q

When should Object Oriented Programming be preferred to Functional Programming?

A

When there are many objects that could share the same methods

56
Q

What does it mean to compose functions?

A

It means to apply a function on the result of a function.

57
Q

What’s the difference between compose and pipe?

A

Composing two functions f and g means executing g and applying f to the result. Piping them means executing f and applying g to the result.

58
Q

“JavaScript is a synchronous, single-threaded, non-blocking language”. True or False?

A

True

59
Q

Which are the possible states for promises?

A

Fulfilled, Rejected or Pending

60
Q

What does it mean that a promise was settled?

A

It means that it was completed, either fulfilled or rejected.

61
Q

How is the standard way of creating promises?

A

const p = new Promise ( (resolve,reject) => {if (…) resolve() else reject()} );

62
Q

When does the execution of a promise begin?

A

Right after it’s instantiation

63
Q

What happens when you use “promise.then()”?

A

It will execute the function inside the “then()” part after it’s finished executing; the parameters passed to the function will be the return of the promise.

64
Q

What’s the type of the return of the resolve() and reject() functions?

A

Promises

65
Q

What happens when a promise gets rejected inside a chain?

A

The following “then()” promises won’t execute and the code will look if there’s any “.catch()” to handle the reject scenario

66
Q

What’s the difference between “Promise.all()” and “Promise.allSettled()”?

A

Promise.all() will only execute the following “then()” functions if all the promises inside are fulfilled. Promise.allSettled() will execute after all the promises finish execution (doesn’t matter how many, if any, gets fulfilled).

67
Q

“Async / Await is just syntatic sugar for promises”. True or False?

A

True

68
Q

“The finally block is also available with promises to clear up things whether they get resolved or rejected”. True or False?

A

True

69
Q

How can you iterate through a promise array?

A

With “for await (… of …)”.

70
Q

When are promises executed (in relation to the synchronous JS code)?

A

After the code has finished executing (but before it has returned) and the stack is empty.

71
Q

Do promises use the job queue or the callback queue?

A

Job Queue

72
Q

What’s the difference between the “job queue” and the “callback queue”? Which (if any) has priority?

A

The “job queue” (also known as micro-task queue) is for more relevant (and usually faster) operations wrapped around promises. It has priority over the callback queue (that’s used with the web API).

73
Q

How are the three ways you can execute a different group of promises?

A

Parallel (all of them running at the same time, the code returns when all of them finish executing), sequential (one after the other, code returns when the last one finishes) and race (all at the same time, code returns the result of the first one to finish).

74
Q

How are the asynchronous functions handled underneath the hood?

A

With service workers that work on different threads.