Cyberithub

Top 250+ JavaScript Interview Questions and Answers in 2022

Table of Contents

Advertisements

In this article, I will take you through Top 250+ JavaScript Interview Questions and Answers that can be asked in 2022. JavaScript is a web based programming language which finds its use in almost all the major Global IT Organizations. It is very extensively used to manage the behavior of web pages. Many modern libraries like jQuery, ReactJS, Reflection.js etc are developed in JavaScript only hence making it the most widely and commonly used web programming language.

Top 250+ JavaScript Interview Questions and Answers in 2022

Top 250+ JavaScript Interview Questions and Answers in 2022

Also Read: Top 250+ Google Cloud(GCP) Interview Questions and Answers Part - 2

1. What is JavaScript ?

Ans. JavaScript is a high level web scripting language which enables us to generate interactive web pages. It can be used both client as well as Server side.

2. How to tell if a variable is global or local ?

Ans. If a variable is defined outside of any JavaScript function then it is global or else it is local.

3. Does the value of Global variable is retained if the web page is reloaded ?

Ans. No. When the page is reloaded, the JavaScript environment is reset and new global variables are created.

4. Is it possible to declare a global variable inside JavaScript function ?

Ans. Yes. A global variable can be declared inside a JavaScript function using window object.

5. How to create variables in JavaScript ?

Ans. Using var keyword

6. Do long variable names slow down the JavaScript programs ?

Ans. No

7. Is variable defined with let keyword can be redeclared ?

Ans. No

8. Which JavaScript function can be used to convert a string into a numeric value ?

Ans. Number function

9. What is the difference between undefined and NaN ?

Ans. A variable is “undefined” if it has been created but it has not been assigned a value yet. The value NaN (not a number) is used to represent a situation when a calculation did not create a numeric result.

10. Which of the JavaScript methods can be used to extract a part of a String ?

Ans. There are three methods which can be used to extract a part of a String:-

  • slice(start,end)
  • substring(start,end)
  • substr(start,length)

11. What is the difference between substring() and slice() method ?

Ans. slice() accepts negative indexes where as substring() does not accept negative indexes.

12. Which method can be used to remove whitespace from both sides of a string ?

Ans. trim() method

13. How to find the size of an array ?

Ans. By using length property of array variable.

14. Does array always store similar kind of objects ?

Ans. No. An array can have multiple types of objects like numbers, strings and references to other objects.

15. What happen to the indexes of an array where value is not assigned ?

Ans. It will be set to undefined

16. What is the use of 'this' keyword in JavaScript ?

Ans. this keyword refers to an object which is executing the current set of code.

17. In which character set JavaScript Programs are written ?

Ans. Unicode

18. Which JavaScript engine Chrome browser uses ?

Ans. V8 Engine

19. Which JavaScript engine Firefox browser uses ?

Ans. SpiderMonkey

20. Which JavaScript method can be used to display all sorts of data within Console View ?

Ans. Console.log()

21. What are lvalues in JavaScript ?

Ans. Variables, Properties of objects, and elements of arrays are lvalues in JavaScript.

22. What is the use of instanceof operator in JavaScript ?

Ans. The instanceof operator is used to check the type of an object at the run time in JavaScript.

23. What is the use of delete operator in JavaScript ?

Ans. delete is an unary operator which is used to delete the object property or array element specified as its operand.

24. What is the use of await operator in JavaScript ?

Ans. The await operator is used to wait for a Promise . It can only be used inside an async function within regular JavaScript code. More on Mozilla website.

25. What is the syntax of using throw statement in JavaScript ?

Ans. throw expression

26. Can we use the with statement in strict mode ?

Ans. No, with statement is not allowed in strict mode.

27. What is Object Serialization ?

Ans. It is the process of converting an object’s state to a string from which it can later be restored.

28. Which functions can be used to serialize and restore JavaScript objects ?

Ans. JSON.stringify() and JSON.parse()

29. What is the difference between valueOf() and toString() method ?

Ans. The valueOf() method is much like the toString() method, but it is called when JavaScript needs to convert an object to some primitive type other than a string, a number.

30. How to Create a new Symbol in JavaScript ?

Ans. By calling the Symbol() factory function

31. Can you add a new property to an existing object constructor ?

Ans. No

32. Which is the top level prototype from where all the JavaScript objects inherits its properties and methods ?

Ans. Object.prototype

33. What is NodeList object ?

Ans. It is a collection of nodes extracted from a document.

34. Is NodeList an Array ?

Ans. No, NodeList looks very similar to an array but none of the array methods work on NodeList.

35. What is Destructuring ?

Ans. It is a JavaScript expression which allows us to fetch the elements of an array or values of an object.

36. What are Arrow Functions ?

Ans. Arrow Functions provides an alternative way to write functions expressions.

37. Which of the JavaScript methods can be used to convert variables to numbers ?

Ans. There are three methods which can be used to convert variables to numbers:-

  • Number() method
  • parseInt() method
  • parseFloat() method

38. Which method can be used to sort an array alphabetically ?

Ans. sort()

39. Which method can be used to reverse the elements in an array ?

Ans. reverse()

40. Which array method can be used to make a shallow copy of a portion of an existing array ?

Ans. Array.slice() method

41. Which array method can be used to check whether the passed variable is an array object ?

Ans. Array.isArray() method

42. Which array method can be used to find all the items in an array that match a certain condition ?

Ans. Array.filter() method

43. What is the easiest to remove all the elements from an array ?

Ans. Set the length property of that array to 0.

44. What is the easiest way to remove all the duplicates from an array ?

Ans. Create a new Set object and fill it with your array.

45. Which array method can be used to flatten a two dimensional array to a one-dimensional list ?

Ans. Array.flat() method

46. Which of the popular array methods can be used to search an array for a specific value ?

Ans. There are mainly three array searching methods that can be used:-

  • indexOf()
  • lastIndexOf()
  • includes()

47. Which array method can be used to search an object in an array ?

Ans. Array.find()

48. How to declare a generator function in JavaScript ?

Ans. By Replacing function with function*

49. What is Partial Application in JavaScript ?

Ans. The technique of wrapping one function in another function to lock down one or more argument values is called partial application.

50. Which method can be used to freeze an object properties against any changes ?

Ans. Object.freeze()

Leave a Comment