Clue Mediator

JavaScript Interview Questions and Answers - Part 1

๐Ÿ“…April 13, 2022

Frequently asked JavaScript Interview Questions and Answers for both freshers and experienced developers.

List of JavaScript Interview Questions and Answers

  1. What is the difference between JavaScript and AngularJS?
  2. What is the drawback of using a type of bar === "object" in order to determine if the bar is an object? What measures would you take to avoid this?
  3. Why do we wrap the content of the JS file into a single function block?
  4. Why do we use "use strict" at the beginning of the JS file?
  5. What is NaN? How do you intend to test the value if it is equal to NaN?
  6. What is closure in JavaScript?
  7. What is function hoisting? What are the two ways of creating a function?
  8. What are the disadvantages of using inner HTML in JavaScript?
  9. What are screen objects? Define its properties?
  10. How does a cookie is read in JS?

1. What is the difference between JavaScript and AngularJS?

Language
JavaScript โ€“ It is a programing language.
AngularJS โ€“ It's a framework that's generally used for web and mobile apps.

Supports
JavaScript โ€“ It supports programming paradigms such as conditional statements, iterations, exceptions, etc.
AngularJS โ€“ It supports iterations or any other conditional constructs.

Filters
JavaScript โ€“ Filters are not supported in JS.
AngularJS โ€“ Filters are supported in AngularJS.

Filters
JavaScript โ€“ Filters are not supported in JS.
AngularJS โ€“ Filters are supported in AngularJS.

Built
JavaScript โ€“ It is used in building frameworks.
AngularJS โ€“ It is built by using JS.

2. What is the drawback of using a type of bar === "object" in order to determine if the bar is an object? What measures would you take to avoid this?

The above line may be used to determine whether a bar is an object, however it has the drawback of treating null as an object. As a result, the code will log true rather than false. As a result, doing a check for the null keyword is a better way to go.

console.log((bar != null) && (typeof bar === "object")); // false

3. Why do we wrap the content of the JS file into a single function block?

This is done to build a closure around the contents of the file, which provides a private namespace and aids in the resolution of name conflicts across modules and libraries.

4. Why do we use "use strict" at the beginning of the JS file?

This is a list of the most common Javascript Interview Questions that are asked during a Javascript interview. "use-strict" is a run-time mechanism for enforcing strict parsing and error handling. There may be some code faults that were previously overlooked, but they will now be thrown openly.

5. What is NaN? How do you intend to test the value if it is equal to NaN?

A value that isn't a number is represented by NaN. This might happen if either the numerator or denominator value is non-numeric (for example, xyz/9) or if the operation's outcome is non-numeric. You can use `isNaN()`, however this isn't the most dependable method. Another option is to test using `abc != abc` and `num.isNaN()`, which are thought to be more efficient.

6. What is closure in JavaScript?

A closure is an inner function that has access to the variables in the scope chain of the outer function. Three scoped variables are available to the closure.

  • Variables in their own scope
  • Variables in the functionโ€™s scope
  • Global variables

7. What is function hoisting? What are the two ways of creating a function?

Functions and variables are hoisted in JavaScript. The JS interpreter anticipates locating all essential declarations and hoisting them above the function, exactly where they are declared. In JavaScript, there are two methods for constructing functions.

  • Function Declaration
  • Function Expression

Function Declaration

The function declaration (function statement) specifies the parameters for a function.

function calcRectArea(width, height) {
  return width * height;
}

console.log(calcRectArea(5, 6));
// Output: 30

Function Expression

Within an expression, the function keyword can be used to define a function.

const getRectArea = function(width, height) {
  return width * height;
};

console.log(getRectArea(3, 4));
// Output: 12

8. What are the disadvantages of using inner HTML in JavaScript?

The following are some of the drawbacks of using inner HTML in JavaScript.

  • When compared to the rest of the variables, using inner HTML is significantly slower. Its content is gradually developed and separated into several elects, which takes time to re-parse.
  • When we use inner HTML in JavaScript, the content is replaced.
  • Inner HTML appends can't be utilised effectively.
  • The usage of inner HTML can be used to break a JS document. Using any form of inner HTML can be a suitable option because no validation is required. Broken HTML may cause a lot of issues.
  • The stuff that has been around for a long time can easily be changed.

9. What are screen objects? Define its properties?

Screen objects are used to read various sorts of data from a client's display. The following are the properties.

  • The screen's height is specified.
  • Images are also shown on the client's screen.
  • The screen's width is specified.
  • The overall height of the client screen is contained the taskbar and screen objects.
  • The taskbar is included in the screen's width, and the entire width is available to users.

Article: How to get the screen size in JavaScript

10. How does a cookie is read in JS?

A cookie is read by JS. A cookie is nothing more than the value of another object, commonly known as the document cookie. Cookies can be obtained using this string. The same string keeps track of the name lists and writes cookies in a variety of ways, including with a semicolon. A cookie is usually only a single value.

Thatโ€™s it for today.
Thank you for reading. Happy Coding..!! ๐Ÿ™‚