DEV Community

Cover image for Mastering JavaScript Interviews: 5 Common Questions and Answers
Oluwagbenga
Oluwagbenga

Posted on

Mastering JavaScript Interviews: 5 Common Questions and Answers

Hey there! If you're getting ready for a JavaScript interview, chances are you'll come across some common questions. But don't worry, I've got you covered! In this post, I'll go over five of the most common JavaScript interview questions and give you detailed answers, complete with code examples and explanations. And don't worry, I'll do my best to make it easy to understand so you can ace that interview!

Question 1: What is closure?

Answer: A closure is a function that has access to variables in its outer lexical scope, even after the outer function has returned.
Here are two examples:

Example 1:

function outer() {
  const message = "Hello";
  function inner() {
    console.log(message);
  }
  return inner;
}
const myFunc = outer();
myFunc(); // output: "Hello"
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • The outer function declares a variable called message and a function called inner.
  • The inner function has access to the message variable, even though it's declared in the outer function.
  • When the outer function is called and returns the inner function, the message variable is still available in memory, This is closure๐Ÿ˜Š

Example 2:

function counter() {
  let count = 0;
  return {
    increment: function() {
      count++;
      console.log(count);
    },
    decrement: function() {
      count--;
      console.log(count);
    }
  };
}
const myCounter = counter();
myCounter.increment(); // output: 1
myCounter.increment(); // output: 2
myCounter.decrement(); // output: 1
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • The counter function returns an object with two methods: increment and decrement.
  • The count variable is declared inside the counter function and is accessible from both methods via closure.
  • When the methods are called, they update the count variable and log its value.

Question 2: What is the difference between var, let, and const?

Answer: var, let, and const are all used to declare variables in JavaScript, but they have different scoping rules and usage restrictions.
Here are two examples:

Example 1:

function myFunction() {
  var x = 1;
  let y = 2;
  const z = 3;
  if (true) {
    var x = 4;
    let y = 5;
    const z = 6;
    console.log(x, y, z); // output: 4 5 6
  }
  console.log(x, y, z); // output: 4 2 3
}
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • Inside the if block, the x variable is redeclared and updated with a new value using var, but y and z are redeclared and updated with new values using let and const, respectively.
  • Outside the if block, the updated value of x is still available because it's function-scoped, while y and z retain their original values.

Example 2:

const myObj = { foo: "bar" };
myObj.foo = "baz";
console.log(myObj.foo); // output: "baz"
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • Even though myObj is declared with const, the properties of the object can still be modified.
  • However, if you try to reassign the entire object to a new value, you'll get an error because myObj itself cannot be reassigned.

Question 3: What is event bubbling and how does it work?

Answer: Event bubbling is a process in which an event that is triggered on a child element is also triggered on its parent elements, all the way up the DOM tree.
Again, here are two examples:

Example 1:

<div onclick="console.log('parent element clicked')">
  <p onclick="console.log('child element clicked')">Click me</p>
</div>
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • The HTML code defines a div element with an onclick attribute that logs a message when it is clicked.
  • Inside the div, there is a p element with its own onclick attribute that logs a different message when it is clicked.
  • When the p element is clicked, the event "bubbles up" to the div element, which also logs its own message.

Example 2:

document.addEventListener("click", function(event) {
  console.log(event.target);
});
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • The addEventListener method is used to attach a click event listener to the entire document.
  • When any element on the page is clicked, the event listener is triggered, and the event.target property is logged to the console.
  • The event.target property is the element that was clicked, and it can be used to determine which element triggered the event.

Question 4: What is the difference between null and undefined?

Answer: null and undefined are two values in JavaScript that are often used interchangeably, but they have different meanings. undefined is the value of a variable that has not been initialized, while null is a value that represents the intentional absence of any object value.
To explain this better, here are examples:

Example 1:

let foo;
console.log(foo); // Output: undefined
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • Declare a variable named foo without assigning any value to it.
  • When we log the variable to the console, the value of the variable is undefined, because it has not been initialized.

Example 2:

let bar = null;
console.log(bar); // Output: null
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • Declare a variable named bar and assign the value null to it.
  • When we log the variable to the console, the value of the variable is null, which indicates that there is no object value present.

Question 5: What are callbacks in JavaScript, and how do they work?

Answer: Callbacks are functions that are passed as arguments to another function, with the expectation that the callback function will be executed at a later time. Callbacks are often used in asynchronous programming, where we want to execute a certain action only after a previous action has completed.
Let me explain this concept with two code examples.

Example 1:

function doSomething(callback) {
  console.log("Do something");
  callback();
}

function doSomethingElse() {
  console.log("Do something else");
}

doSomething(doSomethingElse);
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • Declare a function named doSomething that accepts a callback function as an argument.
  • Inside the function, log a message to the console to indicate that doSomething is being executed.
  • Call the callback function that was passed as an argument.
  • Declare a function named doSomethingElse that logs a message to the console.
  • Call the doSomething function, passing doSomethingElse as the callback function.

Example 2:

setTimeout(function() {
  console.log("Timeout function executed");
}, 1000);
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • Use the setTimeout function to schedule a function to be executed after a certain amount of time has passed.
  • The first argument to setTimeout is an anonymous function that logs a message to the console.
  • The second argument is the delay time in milliseconds (1000ms = 1 second).

So there you have it! We've gone over five of the most common JavaScript interview questions and provided you with their answers. By understanding these concepts and practicing them on your own, you can boost your chances of success in your JavaScript job interviews. And don't forget to practice writing code and clearly explaining your thought process during the interview. Best of luck to you!

Top comments (0)