The Talent500 Blog
Master JavaScript Interview Questions: Essential Concepts for JavaScript Developers 1

Master JavaScript Interview Questions: Essential Concepts for JavaScript Developers

Learn essential JavaScript interview questions, including closures, timers, ViewState, and more. Master your concepts and ace the interview.

Jeff Atwood, the co-founder of programming question-and-answer website Stack Overflow, famously quoted, “Any application that can be written in JavaScript, will eventually be written in JavaScript.”

Atwood’s law has proven correct. With the availability of modern JS-based frameworks like Node.js and Vue, the versatility of the programming language has increased multi-fold. Today, industry-leading companies like Google and Facebook use JavaScript to build their most complex web applications.

Interviewing for the position of a JavaScript developer can be challenging, given that the scope of the language is so vast. Your JavaScript concepts will be tested in-depth for a mid or senior-level position. Here are some essential JavaScript interview questions you must know.

Explain the output of the code below on the console

var myObject = {

   foo: “bar”,

   func: function() {

     var self = this;

     console.log(“outer func: this.foo = ” + this.foo);

     console.log(“outer func: self.foo = ” + self.foo);

     (function() {

       console.log(“inner func: this.foo = ” + this.foo);

       console.log(“inner func: self.foo = ” + self.foo);

     }());

   }

};

myObject.func();

The output will be:

outer func: this.foo = bar

outer func: self.foo = bar

inner func: this.foo = undefined

inner func: self.foo = bar

The outer function uses both this and self to refer to myObject. Therefore both the variables can properly reference and access foo.

But in the inner function, this no longer refers to myObject, which is why this.foo is undefined. However, the reference to the local variable self remains in scope and will remain accessible.

How will you convert a string of any base to an integer in JavaScript?

JavaScript has the parseInt() function to convert numbers between different bases. It takes the string to convert as its first parameter, and the other parameter is the base of the given string.

For example, to 4F (or base 16) to integer in JavaScript, we will use:

parseInt (“4F”, 16);

A common practice is to wrap the entire content of a JavaScript source file in a function block; what is the significance of this approach?

Many popular JavaScript libraries like NodeJS and jQuery support this approach. Using this technique, we can create a closure around the entire content of the file creating a private namespace. This helps avoid any name clashes between different JS libraries or modules.

Also, jQuery plugins often use this technique to create an easily referenceable alias for a global variable. Here’s an example:

(function($) { /* jQuery plugin code referencing $ */ } )(jQuery);

This code uses the closure technique to use $ in jQuery code even if it is disabled by using jQuery.noConflict() function.

Explain the “closure” in JavaScript with an example

In JavaScript, a closure is an inner function with access to the variables defined in the outer (enclosing) function.

A closure function can access variables in three scopes:

  1. Global variables
  2. Variable in its scope
  3. Variables in the enclosing function’s scope

Example of closure function in JS:

var globalVar = “xyz”;

(function outerFunc(outerArg) {

   var outerVar = ‘a’;

   (function innerFunc(innerArg) {

   var innerVar = ‘b’;

   console.log(

     “outerArg = ” + outerArg + “\n” +

     “innerArg = ” + innerArg + “\n” +

     “outerVar = ” + outerVar + “\n” +

     “innerVar = ” + innerVar + “\n” +

     “globalVar = ” + globalVar);

   })(456);

})(123);

Here the innerFunc has access to variables from the global namespace, innerFunc, and outerFunc.

Therefore, all the output functions will be valid. The output will be:

outerArg = 123

innerArg = 456

outerVar = a

innerVar = b

globalVar = xyz

How do timers work in JavaScript? 

We can execute a code at a set time using timers in JavaScript. Using setTimeout, setInterval, and clearInterval functions, we can also repeat a code in a given interval.

We use these functions: 

setTimeout(function, delay): It starts the timer calling a particular function after the mentioned delay

 setInterval(function, delay): It repeatedly executes a given function within the said delay.

clearInterval(id): It stops the timer. 

Timers in JS are operated in a single thread, implying they cannot run concurrently. It can cause events to queue up, waiting for execution.

What is the difference between ViewState and SessionState?

‘ViewState’ is a session state specific to a single web page, while ‘SessionState’ is specific to user-specific data. You can access the SessionState data across all web application pages.

What property will you use to detect the operating system on a client machine? 

JavaScript has the ‘navigator. Platform string (property)’ function that detects the client machine’s OS.

This article is a reference guide to give you an overview of some essential domains within JavaScript that interviewers emphasize. Besides technical skills, you will need some crucial soft skills to ace the interview. Check out these resources:

Most valuable soft skills of exceptional software engineers

Soft skills that still impress employers in 2022

Are you a JavaScript developer exploring remote work opportunities? Talent500 can connect with top-tier companies across the globe. Sign up today.

0
Manik Sharma

Manik Sharma

Manik Sharma specializes primarily in UI or Software Development using Javascript libraries like React and Redux along with HTML, CSS, and other libraries like Bootstrap, Node.js, Express.js, MongoDB. He loves to talk business and think of cool startup ideas. Definitely, an entrepreneur in making. He is equally interested in discussing innovative ideas that can make a huge difference in someone's life.

Add comment