Acing the JavaScript Interview: Top 10 questions explained

JavaScript interviews are not the easiest and many candidates spent countless hours grinding through scattered coding questions to build confidence. While lots of practice makes all the difference, it also matters how you practice. Having a structured plan will help you master all the fundamental and advanced concepts that interviewers expect.

The interviewers may ask these following topics:

1. JavaScript Double Equals Sign VS Triple Equals Sign : When should we use them?

In short, == inherently converts type and === does not convert type.

Double Equals (==) checks for value equality only. It inherently does type coercion. This means that before checking the values, it converts the types of the variables to match each other.

Triple Equals (===) does not perform type coercion. It will verify whether the variables being compared have both the same value AND the same type.

2. Scope Variable : How to declare them?

Variables are one of the fundamental blocks of any programming language, the way each language defines how we declare and interact with variables can make or break a programming language.

Scope in JavaScript refers to context (or portion) of the code which determines the accessibility (visibility) of variables. In JavaScript, we have two types of scope, local , and global. Though local scope can have different meanings.

Block scope: A block is basically a section of code (zero or more statements) which is delimited by a pair of curly braces and may optionally be labeled.

Function scope : A function scope is in a way also a block scope. It scopes also encapsulate variables declared with var.

3. Closures — Private Variables : Explain the methods in JavaScript

A closure is like a snapshot of the environment a function is in when it is executed. Its closure is its own internal copy of the environment. Any variable in a closure is exclusively accessible to the child function that owns it. Operations can only be performed on these variables if the current execution context has a reference to the closure.

Private variable is only visible to the current class. It is not accessible in the global scope or to any of its subclasses. Making variables private is done for many reasons ranging from security to encapsulation. In this case, private variables can only be indirectly accessed and manipulated using good or getter and setter methods.

4. Difference between call, apply and bind

Call( ): The call() method invokes a function with a given ‘this’ value and arguments provided one by one. This means that we can call any function, and explicitly specify what ‘this’ should reference within the calling function.

Apply( ): Invokes the function and allows you to pass in arguments as an array.

Bind(): Returns a new function, allowing you to pass in an array and any number of arguments.

5. Global JavaScript Variables

A variable declared outside a function, becomes global. A global variable has global scope. All scripts and functions on a web page can access it.

In the above program, variable a is declared at the top of a program and is a global variable. It means the variable a can be used anywhere in the program.

6. Understanding the this Keyword in JavaScript

this keyword refers to an object, that object which is executing the current bit of JavaScript code.

In other words, every JavaScript function while executing has a reference to its current execution context, called this. Execution context means here is how the function is called.

To understand this keyword, only we need to know how, when and from where the function is called, does not matter how and where function is declared or defined.

7. How JavaScript works event loop stack and queue?

The Event Loop has one simple job — to monitor the Call Stack and the Callback Queue. If the Call Stack is empty, the Event Loop will take the first event from the queue and will push it to the Call Stack, which effectively runs it. Such an iteration is called a tick in the Event Loop.

JavaScript can do one single thing at a time because it has only one call stack.

The call stack is a mechanism that helps the JavaScript interpreter to keep track of the functions that a script calls.

Every time a script or function calls a function, it’s added to the top of the call stack. Every time the function exits, the interpreter removes it from the call stack.

8. Remove duplicate item from an array

There are a few ways to remove duplicate item from an array.

Using a set : A set is a collection of unique values. To remove duplicates from an array:

  • First, convert an array of duplicates to a set. The new set will implicitly remove duplicate elements.
  • Then, convert the set back to an array.

Using for each and include : The include() returns true if an element is in an array or false if it is not.

9. Reverse a string

Reversing a string is one of the most frequently asked JavaScript question in the technical round of interview.

With Built-In Functions : We can use three methods: the String.prototype.split() method, the Array.prototype.reverse() method and the Array.prototype.join() method.

  • The split() method splits a String object into an array of string by separating the string into sub strings.
  • The reverse() method reverses an array in place. The first array element becomes the last and the last becomes the first.
  • The join() method joins all elements of an array into a string.

10. Create a Fibonacci Series using a for loop

Fibonacci series is a series that generates subsequent series of numbers by the addition of the two previous numbers. The first two terms of the Fibonacci series are zero and one, respectively. And the next terms are the addition of the two previous terms.

Its recurrence relation is given by Fn = Fn-1 + Fn-2.

1. In JavaScript, it is not mandatory to declare a variable before using it. But still its a good practice to declare it before using.
2. Using html comments inside JavaScript code ensures that, the old browsers which do not support JavaScript will ignore all the code written inside the script tags.

Wrap-up

I hope these 10 most popular topics for JavaScript interview will help you out in-front of the interviewers. Wishing you a good luck for the upcoming interviews.

Thanks for reading and happy coding!

References

  1. https://thenextweb.com/news/how-to-declare-interact-and-scope-variables-in-javascript
  2. https://dev.to/somedood/emulating-private-variables-in-javascript-with-closures-and-factory-functions-2314
  3. https://www.programiz.com/javascript/variable-scope

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Fariza Mubashshara Rafa

Fariza Mubashshara Rafa

1 Follower

Information Science Major • Focusing on my interests • Project : Quarantine coding • INTP-T