DEV Community

Cover image for The "skill-check" JS quiz
Nikola Perišić
Nikola Perišić

Posted on

The "skill-check" JS quiz

How well do you really know the language that powers the web? It’s easy to get comfortable with const and fetch(), but JavaScript has some dusty corners and "gotchas" that separate the pros from the hobbyists.

P.S. If you're wondering why there's a duck on the thumbnail... honestly, it's just for attention. But hey, it worked, you're here! 🦆


1. The equality enigma

What does the following code output to the console?

console.log(0 == '0');
console.log(0 === '0');
Enter fullscreen mode Exit fullscreen mode

The answer: true, then false

Explanation: I’m not going to explain this one. If I do, you’ll just read it and forget it. I want you to search for "JavaScript Type Coercion" yourself. Trust me, you will thank me later.

2. The "this" trap

In the snippet below, what will be logged?

const user = {
  name: 'Alex',
  greet: () => {
    console.log(`Hi, I'm ${this.name}`);
  }
};

user.greet();
Enter fullscreen mode Exit fullscreen mode

The answer: Hi, I'm undefined (or an empty string in some environments).

Explanation: Arrow functions do not have their own this context. They inherit this from the surrounding lexical scope (in this case, the global window or module). To fix this, use a regular function expression.

3. Closure clarity

What is the result of this execution?

for (var i = 0; i < 3; i++) {
  setTimeout(() => console.log(i), 1);
}
Enter fullscreen mode Exit fullscreen mode

The answer: 3, 3, 3

Explanation: Because var is function-scoped and not block-scoped, the loop finishes before the setTimeout callbacks run. By then, i has been incremented to 3. Using let instead of var would result in 0, 1, 2 because let creates a new binding for each iteration.

4. Reference vs. value

What happens to list2?

let list1 = [1, 2, 3];
let list2 = list1;
list1.push(4);

console.log(list2.length);
Enter fullscreen mode Exit fullscreen mode

The answer: 4

Explanation: Arrays in JavaScript are objects, which means they are passed by reference. list2 isn't a copy; it's a pointer to the same memory address as list1.

5. Bonus: The "typeof" quirk

What is typeof null?

The answer: "object"

This is a famous bug from the first version of JavaScript that was never fixed to maintain backward compatibility.

How did you do? 🏆

Drop your score in the comments below!


Connect with me

GitHub
LinkedIn
Medium

Top comments (1)

Collapse
 
shiracamus_500e7d4ba3862c profile image
shiracamus • Edited

Arrays in JavaScript are objects, which means they are passed by reference.

MDN JavaScript reference says:

Arguments are always passed by value and never passed by reference. This means that if a function reassigns a parameter, the value won't change outside the function. More precisely, object arguments are passed by sharing, which means if the object's properties are mutated, the change will impact the outside of the function.