10 JavaScript questions to boost your skills (Part 1)

Tram Ho

One way we can challenge ourselves to grow is to practice with quiz questions! The following questions will be challenges as well as guidance. If you know exactly how to answer each question, that’s great, but if you make some mistakes and find out why you’re wrong, then I think that’s even better. again!

1. Compare array sorted

Consider the following array. How will the console log return results under different sorting conditions?

Answer and explain

True, True, false

First, sort will sort your original array and also return a reference to that array. This means that when you write arr2.sort () , the array arr2 object is sorted.

However, it turns out the ordering of arrays doesn’t matter when you compare objects. Because arr1.sort () and arr1 point to the same object in memory, the first test will return true. The same is true for the second comparison: arr2.sort () and arr2 points to the same object in memory.

In the third test, the sort order of arr1.sort () and arr2.sort () is the same; however, they still point to different objects in memory. Therefore, the third test result is false.

2. Set Objects

Consider the following set and guess what the log will return

Answer and explain

[{a: 1}, {a: 1}]

Although it is true that Set will eliminate duplicates, the two values ​​we create Set are references to different objects in memory, despite having identical key-values. This is also the reason {a: 1} === {a: 1} is wrong.

It should be noted that if the set is created with an object variable, assuming obj = {a: 1}, the new set ([obj, obj]) will have only one element, since both elements in the array refer to the same object. object in memory.

3. Transform multi-level Objects

Consider the following Object, which represents a user – Joe and his dog, Buttercup. We use Object.freeze to protect the object and then try to rename ButterCup. What will the log be …

Answer and explain

Daffodil

Object.freeze will perform freezing of the surface of an object, and will not protect deep attributes from being changed. In this example, we won’t be able to change user.age or user.name , but we won’t have any problems changing user.pet.name . If we feel we need to protect an object from being changed in “every way,” we can apply Object.freeze recursively or use the ” deep freeze ” library.

4. Inheriting prototypes

In this question, we have the Dog function. Our dog clearly knows what to say. So when we ask Pogo to speak, what happens?

Answer and explain

woof

Each time we create a new Dog instance, we set the speak property of that instance to a function that returns the string “woof”. Because this is set every time we create a new Dog instance, the interpreter never has to look beyond the prototype string to find the speaking attribute. Therefore, the aforementioned method on Dog.prototype.speak will never be used.

5. Order in Promise.all

In this question, we have the timers function that returns a Promise that will resolve after a random amount of time. We use Promise.all to solve an array of timers . What will the log tell us, or will everything be random?

Answer and explain

[“first”, “second”]

The order in which the Promises are dealt with is not important to Promise.all. We can trust that they will be returned in the same order that they are provided in the array.

My article temporarily ends here, if you have any questions or disagree with any part, please leave a comment below.

Share the news now

Source : Viblo