interview-questions

JavaScript(ES5/ES6) Interview Questions

1. Does JavaScript pass parameter by value or by reference?

It’s always pass by value, but for objects the value of the variable is a reference. Because of this, when you pass an object and change its members, those changes persist outside of the function. This makes it look like pass by reference. But if you actually change the value of the object variable you will see that the change does not persist, proving it’s really pass by value.

2. What is the keyword this in JavaScript? What problems can it - cause?

The this keyword refers to different things in different context and that is the may reason it can be troublesome. This is a short summary of what this can mean in different contexts. The easiest rule of thumb - always prefer arrow functions as there behavior is the most ‘normal’.

3. What’s the difference between a variable that is: null, undefined, or undeclared?

Or in short:

4. What is === and !== operator?

This is strict equality. For strict equality the objects being compared must have the same type and:

5. What is the drawback of creating true private methods in - JavaScript?

In ES5 creating true private method in javascript is very memory inefficient because a new copy of the method would be created for each instance. This will change with ES9 as there is a Stage 3 proposal for private methods:

6. What is a potential pitfall with using typeof bar === "object" to - determine if bar is an object? How can this pitfall be avoided?

Null and Array is also considered objects! In order to avoid this behavior check explicitly for these cases.

7. What is a closure? How to create one in JavaScript?

8. What is an anonymous function?

Anonymous function are just functions declarations without a name. Every time you write the function keyword, anywhere in your code, giving it a name right after is actually optional. Omitting the name makes the function, well, anonymous.

9. What is duck/weak typing?

It is a term used in dynamic languages that do not have strong typing. The idea is that you don’t need a type in order to invoke an existing method on an object - if a method is defined on it, you can invoke it. The name comes from the phrase “If it looks like a duck and quacks like a duck, it’s a duck”.

10. What are higher order functions? Provide an example in the - context of JavaScript?

Functions in JavaScript are first-class objects. You can pass them into other functions as arguments just like any other object and these parameter functions are called higher order functions.

11. What is function currying? How would you implement currying for - any functions?

Currying is when you break down a function that takes multiple arguments into a series of functions that take part of the arguments.

function add (a, b) {
 return a + b;
}

add(3, 4); // returns 7
// Same function using curring.
function add (a) {
 return function (b) {
   return a + b;
 }
}

12. What is the difference between a function declaration and a - function expression?

They are mostly similar except:

13. What is the difference between a function and an arrow function?

There are two major differences:

14. What is JavaScript Self-Invoking anonymous function or - Self-Executing anonymous function?

15. What are promises?

A promise is an object that may produce a single value some time in the future: either a resolved value, or a reason that it’s not resolved (e.g., a network error occurred). A promise may be in one of 3 possible states: fulfilled, rejected, or pending. Promise users can attach callbacks to handle the fulfilled value or the reason for rejection.

16. How do you check if an object is an array or not?

Array.isArray()

17. What is the difference between call and apply?

The difference is that apply lets you invoke the function with arguments as an array; call requires the parameters be listed explicitly. A useful mnemonic is “A for array and C for comma.”

18. What is function hoisting in JavaScript?

In short function hoisting means that functions are moved to the top of their scope. NEVER depend on hoisting as it can produce unexpected behavior.

19. What is the difference between classical inheritance and - prototypal inheritance?

A very complex question but in short:

20. When should you use let and when var? What is the difference - between the two?

You should always prefer let over var.

21. What is the difference between a let variable and a const - variable? Provide an example

const cannot be updated or re-declared BUT there is some specific behavior:

22. What does destructuring assignment mean?

Destructuring assignment is a special syntax that allows us to “unpack” arrays or objects into a bunch of variables, as sometimes they are more convenient. Destructuring also works great with complex functions that have a lot of parameters, default values, and soon we’ll see how these are handled too.

23. How to achieve encapsulation in the context of JavaScript?

24. What are generators? What is lazy evaluation?

25. What is the difference between synchronous, asynchronous and - parallel function calls? How can do each one in JavaScript?

In JavaScript we can use callbacks, Promises and async/await to do async programing and WebWorkers to do parallel programming.

26. Why is it bad to use global variables? Give examples

The main reason is the shared state problem:

27. What is event bubbling?

When an event is triggered on an element, for example a mouse click on a button, the same event is also triggered on all of that element’s ancestors. This process is known as event bubbling;

28. What is event delegation and what problem does it solve?

29. What is the difference between throttling and debouncing?

If you have a function that gets called a lot - for example when a resize or mouse move event occurs, it can be called a lot of times. If you don’t want this behaviour, you can Throttle it so that the function is called at regular intervals. Debouncing will mean it is called at the end (or start) of a bunch of events.

30. What is the event loop?

The Event Loop in Node.js

The event loop is a fundamental aspect of Node.js that enables its non-blocking, asynchronous behavior. It is the core mechanism that allows Node.js to handle multiple operations simultaneously without the need for multiple threads.

Key Points

Example

In a Node.js application, when an asynchronous function is called, it is processed outside the main thread. The event loop continuously checks the callback queue for any callbacks that need to be executed. If it finds one, it removes it from the queue and executes it.

This mechanism allows Node.js to perform heavy I/O operations without blocking the single thread, making it highly scalable for I/O-bound tasks.

In summary, the event loop is a core concept in Node.js that enables asynchronous programming by allowing long-running operations to be processed without blocking the main thread.

31. What data types exit in JavaScript?

Six data types that are primitives:

and Object

32. What are the most common module systems in JavaScript?

33. Why is using eval bad?

34. Can you explain the „scope chain“?

The scope chain is a fundamental concept in many programming languages, including JavaScript, that defines how variable names are resolved in nested scopes.

For example:

let globalVar = "global";

function outerFunction() {
    let outerVar = "outer";

    function innerFunction() {
        let innerVar = "inner";

        // Here, innerFunction's scope chain is [innerFunction's scope, outerFunction's scope, global scope]
        console.log(innerVar); // Resolves to "inner"
        console.log(outerVar); // Resolves to "outer" via scope chain
        console.log(globalVar); // Resolves to "global" via scope chain
    }

    innerFunction();
}

outerFunction();