If You Can Answer These 7 Questions Correctly, You’re Decent at JavaScript

JavaScript is one of the most versatile and widely-used programming languages, making it a staple for web developers. But how do you know if you have a decent grasp of the language? By answering these seven questions, you can test your knowledge and see if you’re up to the task. Let’s dive in!

1. What Is the Difference Between == and ===?

One of the first hurdles for JavaScript developers is understanding the difference between == (equality) and === (strict equality). While == compares values after type coercion, === checks both the value and type. For example:

console.log(5 == '5'); // true
console.log(5 === '5'); // false

If you understand why the above happens, you’re off to a good start.

2. What Is this and How Does It Behave in Different Contexts?

The this keyword is a cornerstone of JavaScript, but its behavior can be tricky. It refers to the object that is currently executing the function. For example:

const obj = {
  name: 'JavaScript',
  getName() {
    return this.name;
  }
};

console.log(obj.getName()); // 'JavaScript'

However, if you extract the function:

const getName = obj.getName;
console.log(getName()); // undefined or error, depending on the environment

If you can explain why the output changes, you’re on solid ground.

3. How Does let, const, and var Differ?

Understanding the differences between let, const, and var is essential for modern JavaScript development. Here’s a brief rundown:

  • var is function-scoped and can be redeclared.
  • let is block-scoped and can be reassigned but not redeclared in the same scope.
  • const is block-scoped and cannot be reassigned or redeclared.

Can you predict the output of the following code?

function test() {
  if (true) {
    var x = 10;
    let y = 20;
    const z = 30;
  }
  console.log(x); // ?
  console.log(y); // ?
  console.log(z); // ?
}




4. What Is a Closure, and Why Is It Useful?

A closure is a function that remembers its lexical scope even when executed outside of that scope. For example:

function outerFunction(outerVariable) {
  return function innerFunction(innerVariable) {
    console.log(`Outer: ${outerVariable}, Inner: ${innerVariable}`);
  };
}

const closureFunc = outerFunction('outside');
closureFunc('inside'); // Outer: outside, Inner: inside

Closures are powerful for creating private variables and managing state in JavaScript.

5. Can You Explain the Event Loop?

The event loop is at the heart of JavaScript’s asynchronous behavior. It’s responsible for managing the execution of tasks, including:

  • Synchronous code in the call stack
  • Asynchronous tasks in the callback queue
  • Promises in the microtask queue

Consider this code:

console.log('Start');
setTimeout(() => console.log('Timeout'), 0);
Promise.resolve().then(() => console.log('Promise'));
console.log('End');

What will be the output, and why?

6. What Are Prototypes and How Does Inheritance Work in JavaScript?

Prototypes are fundamental to JavaScript’s inheritance model. Every object has an internal property called [[Prototype]], which points to another object. For example:

function Person(name) {
  this.name = name;
}

Person.prototype.greet = function() {
  return `Hello, my name is ${this.name}`;
};

const alice = new Person('Alice');
console.log(alice.greet()); // Hello, my name is Alice

If you understand how prototypes enable inheritance, you’re doing great.

7. Can You Identify and Avoid Common Pitfalls?

Finally, being decent at JavaScript means recognizing potential pitfalls, such as:

  • Implicit type coercion: [] + {} vs. {} + []
  • Hoisting: Understanding how variables and functions are hoisted
  • Mutating objects: Avoiding unexpected changes to shared objects

For instance:

const obj = { a: 1 }
const obj2 = obj
obj2.a = 2
console.log(obj.a) // ?

JavaScript is a dynamic and sometimes quirky language. If you can confidently answer these seven questions and understand the concepts behind them, you’re on your way to mastering the language. Keep learning, practicing, and exploring – the JavaScript ecosystem is vast, and there’s always more to discover!