Top 40 JavaScript Interview Questions for 2021


Table of Contents

1. What is JavaScript?

JavaScript is a high-level scripting language that we use to design webpages especially for dynamic updating of content. We can also use it for video, animation, updating maps, scrolling, etc. It supports both client-side and server-side scripting. We can use it along with HTML and CSS as well which are the other 2 web designing programming languages.

2. What are the different data types of JavaScript?

Below are the different data types present in JavaScript:

  • Number
  • Object
  • String
  • Boolean
  • Arrays
  • Undefined
  • Null

3. What are the advantages of using JavaScript?

  • Faster performance
  • Interoperability
  • Simple
  • Rich interfaces
  • Minimal server interaction
  • Rich interfaces

4. Define undefined and undeclared variables

Undeclared variables are variables that are not declared and do not exist in the code. It throws a runtime error when we try to use undeclared variables.

Undefined variables are variables that are declared but not defined which means there is no value assigned. When we try to use undefined variables, it returns an undefined number.

5. What is the use of PromptBox?

PromptBox is used when a user needs to input any value. It is a dialog that contains an input field along with OK or CANCEL buttons.

6. What is the difference between == and === operator?

The equalsTo (==) operator checks if the values of both variables are equal.

The strictly equalsTo(===) operator not only checks the values but also its datatypes.

7. What is the parseInt function?

The parseInt() function converts from numbers of different bases to an integer number.

parseInt("4F",16);

8. What is the difference between void(0) and void()?

We use void(0) to prevent the page to refresh whereas void() is a keyword that indicates that the function does not return any value.

9. How can we group different data types in JavaScript?

We can group the data types as below:

  • Primitive data types
  • Reference data types

10. What is a Blur function?

A blur function removes the focus from a specific object.

11. What are the different types of errors in JavaScript?

  • Runtime errors: When HTML command is incorrect
  • Logical errors: Due to the wrong logics that we apply
  • Load time errors: When an error occurs due to incorrect syntax during webpage loading

12. What is the difference between SessionState and ViewState?

SessionState is applicable for specific user data across all web pages whereas ViewState is applicable only for a webpage of a session.

13. What is a push method in JavaScript?

The push method is used along with the Array data type to insert an element at the end. We can also use it to add multiple elements together.

14. How do we use a Callback function in JavaScript?

A Callback function is triggered during a specific state that contains some method and arguments.

15. How does exception handling work in JavaScript?

We can implement exception handling in JavaScripts using try, catch, finally, and throws keywords.

16. What is the use of isNan function?

Nan stands for Not a number. Hence this function checks if the argument passes is not a number and return true if the condition is met.

17. What is the disadvantage of creating true private methods in JavaScript?

The major drawback of using the true private method is, it creates a new method for every instance and hence results in poor memory efficiency.

18. What is closure in JavaScript?

A closure is a function inside a parent function that has access to all defined and declared variables within the parent scope.

19. What are the different types of JavaScript statements?

  • block statement
  • break statement
  • continue statement
  • do..while statement
  • for statement

20. What is a JavaScript object?

JavaScript object is a data type that has its own properties and methods while working with the data. Each variable is considered as an object in JavaScript.

21. What are the different types of JavaScript objects?

  • Array object: Used for data processing
  • String object: Used with String values
  • Date object: Used for date and time manipulation
  • Number object: Works with numeric data
  • Math object: Used for mathematical operations
  • RegExp object: For pattern text matching
  • Boolean object: Represents true or false values
  • Function object: Small piece of code that performs some functionality
  • object Object: creates custom objects

22. Is JavaScript case-sensitive?

Yes, JavaScript is a case-sensitive scripting language which means there is a difference between variable values when they are in different cases(lower or upper). For example, “java” and “Java” are not equal.

23. What is an anonymous function?

An anonymous function is a function that does not have any name. It resides within a variable name and hence we can call this only using the variable name.

24. What is a Generic function?

A Generic function in JavaScript checks if a string contains a null value or is empty.

25. What are the different types of popup boxes?

  • Confirmation box: Uses the method confirm()
  • Alert box: Uses the method alert()
  • Prompt box: Uses the method prompt()

26. What is the innerHTML property in JavaScript?

The innerHTML property in JavaScript changes the page’s content without refreshing the page. This means it can be used to modify the HTML content and provides faster user response.

27. What are cookies in JavaScript?

A cookie stores browser data as variables in the client’s computer. Whenever we browse a page, it retrieves and stores the cookies too which is used for faster and better user responses.

28. What is the chaining concept in JavaScript?

When we call one statement within another, we call it chaining or cascading.

29. What is the main difference between the “var” and “let” keyword?

The var keyword has scope within the function while the let keyword has scope within the block.

30. What is the output of the evaluation 3+8+”2″?

The output is 112. This is because it performs evaluation from left to right and appends the string to the result.

31. What do you mean by the Unshift method?

The Unshift method insert values to the front of the array and can be used to retrieve the length of the array.

32. Which method returns a character at the specified index?

We can use the charAt() method to retrieve a character at the specified index.

33. What is the use of this keyword in JavaScript?

This keyword refers to the current instance or object. When this keyword is used within a method, it refers to the owner object and within a function, it refers to the global object.

34. What are the different ways to access HTML elements in JavaScript?

Below are the different ways to access HTML elements in JavaScript:

  • getElementById(‘idname’)
  • getElementsByClass(‘classname’)
  • getElementsByTagName(‘tagname’)
  • querySelector()

35. What is negative infinity in JavaScript?

The number that we obtain by diving an integer by zero is called negative infinity.

36. How to submit a form using JavaScript?

We can use the below code to submit a form using JavaScript:

document.form[0].submit();

37. What are escape characters in JavaScript?

The escape character is represented by “\” backslash that is used to print special characters like double quotes, single quotes, apostrophe, and ampersand. We just need to place “\” before the required character.

38. How to find the operating system in the client machine using JavaScript?

We can use the Navigator.appversion to determine the operating system in the client machine.

39. What is the use of typeof operator?

The typeof operator returns the string description of the datatype of the variable.

40. What is variable typing?

Variable typing is the process of assigning a number and string to the same variable.

For example:

a = 45;
a = "JavaScript";

 

 

Array Interview Questions
Graph Interview Questions
LinkedList Interview Questions
String Interview Questions
Tree Interview Questions
Core Java Interview Questions