BETTER PROGRAMMING
5 JavaScript Questions and Answers to Test Your Skills
Test and sharpen your skills with these 5 JavaScript questions and answers
Today we are going to continue learning our beloved JavaScript language, in this edition, we are going to test our skills by answering some JavaScript mini-challenges.
Even though I’ll be giving the answer with an explanation at the end of each question, try to figure it out by yourself first, and then validate your answer against mine.
Question #1: Array Sort Comparison
Consider the following arrays and conditions, what do you think would be the result?
const arr1 = ['a', 'b', 'c']
const arr2 = ['c', 'b', 'a']console.log(
arr1.sort() === arr1,
arr2 === arr2.sort(),
arr1.sort() === arr2.sort()
)
Answer
true true false
Now that we know the result, let’s explain why. For the first 2 outputs the explanation is rather simple, the sort()
method sorts the original array and returns a reference to the same object, so arr1.sort()
is actually a reference to the same object as arr1
.