The Talent500 Blog
5 most frequently asked JavaScript interview questions 1

5 most frequently asked JavaScript interview questions

JavaScript and frontend development go hand in hand. To ace your JavaScript interview check this article out. Read on to learn all about the most frequently asked JavaScript interview questions.

JavaScript is the core of front-end development. JavaScript interview questions are asked to gauge the technical abilities of front-end developers. If you plan to start your career as a front-end developer, you are up for an exciting domain that frequently witnesses innovations and technological advancements.

Your skills as a front-end developer will be tested on how well versed you are with JavaScript. In this article, we are sharing the most frequently asked JavaScript interview questions.

1. What is the drawback of using a bar with typeof bar === “object” to determine if bar is an object? How can the drawback be avoided?

While typeof bar === “object” is a reliable way of checking if bar is an object, the problem arises because null is also considered an object in JavaScript.

That’s why the following code will return true, unlike most JavaScript developers anticipating it to return false.

var bar = null;

console.log(typeof bar === “object”); // logs true!

As long as you are aware of this nature of JavaScript, you can easily avoid the problem by proactively checking if bar is null:

console.log((bar !== null) && (typeof bar === “object”)); // logs false

This statement eliminates the problem, but the important point is to know that the above solution will return false if the bar is a function. In most cases, it is a required behavior, but if you want to return true for functionals, you can make a change like this:

console.log((bar !== null) && ((typeof bar === “object”) || (typeof bar === “function”)));

2. What will be the output of the following code, and why?

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();

This is another commonly asked JavaScript interview question. The output of the code will be:

outer func: this.foo = bar

outer func: self.foo = bar

inner func: this.foo = undefined

inner func: self.foo = bar

In the outer function, myObject is referred to by both this and self, which is why both can adequately refer to and access foo. While, in the inner function, this no longer refers to myObject and results in undefined output. However, the reference to the local variable self remains in scope and therefore is accessible.

3. What is a scope, and what are its types? 

In JavaScript, a scope determines how an object, a variable, or a function can be accessed in a particular section of your code. 

Scopes are of two types in JS: 

  • Global scope: It is defined outside the function and can be accessed from any code section. For example: 

var name = ‘LambdaTest’;

console.log(name); // logs ‘LambdaTest’

function newLogName() {

console.log(name); // ‘name’ is accessible here and everywhere else

}

newLogName();

  • Local scope: Here, variables are defined inside the function, scoped in the function only, and cannot be used outside. 

// Global Scope

function sampleFunction() {

// Local Scope #1

function oldFunction() {

// Local Scope #2

}

 }

// Global Scope

function newFunction() {

// Local Scope #3

}

// Global Scope

4. What will be the output of the code below ? Explain your answer

console.log(0.1 + 0.2);

console.log(0.1 + 0.2 == 0.3);

This developer interview question is a bit tricky to answer. You can’t be sure of what the outcome will be. The code might print out 0.3 and true, or it might not. The reason being in JavaScript, all the numbers are treated as floating point precision which is why they may not always output the expected result.

The above example is a classic case of how JavaScript handles numbers. The code will surprisingly print:

0.30000000000000004

false

The easiest solution to this problem is to use a special constant Number.EPSILON to compare the absolute difference between two numbers:

function areTheNumbersAlmostEqual(num1, num2) {

       return Math.abs( num1 – num2 ) < Number.EPSILON;

}

console.log(areTheNumbersAlmostEqual(0.1 + 0.2, 0.3));

5.What is prototype property?

Prototype property in JavaScript is used for implementing inheritance. Each function in JS has a default prototype property value which is null. Methods and properties are added to the prototype to make it worthwhile for the instances. This is how a JavaScript function is made robust and available for multiple instances. 

This JavaScript interview question is better explained with the following code to calculate the perimeter of a rectangle: 

function Rectangle(x, y) {

this.x = x;

this.y = y;

}

Rectangle.prototype.perimeter = function() {

return 2 * (this.x + this.y);

}

var rectangle = new Rectangle(4, 3);

console.log(rectangle.perimeter()); // outputs ’14’

Conclusion 

These are some critical JavaScript interview questions that you must understand. JavaScript is an essential language for developers. Beginners can start learning JavaScript from these websites.

Talent500 helps developers find the best opportunities with global companies. Sign up today and get ready to be discovered by the best companies.

 

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