100 Javascript interview Questions with answers

100 Javascript interview Questions with answers

  1. What is closure in JavaScript?
  2. What is the difference between null and undefined in JavaScript?
  3. Can you explain the difference between == and === in JavaScript?
  4. Can you explain hoisting in JavaScript?
  5. Can you explain how to implement inheritance in JavaScript?
  6. What is the difference between let and const in JavaScript?
  7. Can you explain how to implement a Promise in JavaScript?
  8. Can you explain how to handle errors in JavaScript?
  9. Can you explain the event bubbling and capturing mechanism in JavaScript?
  10. Can you explain the difference between synchronous and asynchronous code in JavaScript?
  11. Can you explain the concept of prototypes in JavaScript?
  12. Can you explain the concept of a callback function in JavaScript?
  13. Can you explain how to implement the Singleton pattern in JavaScript?
  14. Can you explain how to implement a Factory pattern in JavaScript?
  15. Can you explain how to implement the Observer pattern in JavaScript?
  16. Can you explain the difference between a forEach loop and a for loop in JavaScript?
  17. Can you explain how to implement a Map in JavaScript?
  18. Can you explain how to implement a Set in JavaScript?
  19. Can you explain how to implement a WeakMap in JavaScript?
  20. Can you explain how to implement a WeakSet in JavaScript?
  21. Can you explain the concept of the this keyword in JavaScript?
  22. Can you explain how to implement a decorator in JavaScript?
  23. Can you explain how to implement a class in JavaScript?
  24. Can you explain how to implement a module in JavaScript?
  25. Can you explain how to implement a mixin in JavaScript?
  26. Can you explain how to implement a generator function in JavaScript?
  27. Can you explain how to implement an async/await function in JavaScript?
  28. Can you explain how to implement a recursive function in JavaScript?
  29. Can you explain how to implement a memoization in JavaScript?
  30. Can you explain how to implement a tail call optimization in JavaScript?
  31. Can you explain how to implement a destructuring assignment in JavaScript?
  32. Can you explain how to implement a spread operator in JavaScript?
  33. Can you explain how to implement a rest parameter in JavaScript?
  34. Can you explain how to implement a default parameter in JavaScript?
  35. Can you explain how to implement a template literal in JavaScript?
  36. Can you explain how to implement a tagged template literal in JavaScript?
  37. Can you explain how to implement a typeof operator in JavaScript?
  38. Can you explain how to implement an instanceof operator in JavaScript?
  39. Can you explain how to implement a new operator in JavaScript?
  40. Can you explain how to implement an Object.create method in JavaScript?
  41. Can you explain how to implement an Object.assign method in JavaScript?
  42. Can you explain how to implement an Object.entries method in JavaScript?
  43. Can you explain how to implement an Object.values method in JavaScript?
  44. Can you explain how to implement an Object.keys method in JavaScript?
  45. Can you explain how to implement an Array.map method in JavaScript?
  46. Can you explain how to implement an Array.filter method in JavaScript?
  47. Can you explain how to implement an Array.reduce method in JavaScript?
  48. Can you explain how to implement an Array.sort method in JavaScript?
  49. Can you explain how to implement an Array.reverse method in JavaScript?
  50. Can you explain how to implement an Array.concat method in JavaScript?
  51. Can you explain how to implement an Array.slice method in JavaScript?
  52. Can you explain how to implement an Array.splice method in JavaScript?
  53. Can you explain how to implement an Array.push method in JavaScript?
  54. Can you explain how to implement an Array.pop method in JavaScript?
  55. Can you explain how to implement an Array.shift method in JavaScript?
  56. Can you explain how to implement an Array.unshift method in JavaScript?
  57. Can you explain how to implement an Array.indexOf method in JavaScript?
  58. Can you explain how to implement an Array.includes method in JavaScript?
  59. Can you explain how to implement an Array.find method in JavaScript?
  60. Can you explain how to implement an Array.findIndex method in JavaScript?
  61. Can you explain how to implement a Math.max method in JavaScript?
  62. Can you explain how to implement a Math.min method in JavaScript?
  63. Can you explain how to implement a Math.random method in JavaScript?
  64. Can you explain how to implement a Math.floor method in JavaScript?
  65. Can you explain how to implement a Math.ceil method in JavaScript?
  66. Can you explain how to implement a Math.round method in JavaScript?
  67. Can you explain how to implement a parseInt function in JavaScript?
  68. Can you explain how to implement a parseFloat function in JavaScript?
  69. Can you explain how to implement a isNaN function in JavaScript?
  70. Can you explain how to implement a isFinite function in JavaScript?
  71. Can you explain how to implement a Number method in JavaScript?
  72. Can you explain how to implement a String method in JavaScript?
  73. Can you explain how to implement a Boolean method in JavaScript?
  74. Can you explain how to implement a Symbol method in JavaScript?
  75. Can you explain how to implement a RegExp method in JavaScript?
  76. Can you explain how to implement a Date method in JavaScript?
  77. Can you explain how to implement a JSON.stringify method in JavaScript?
  78. Can you explain how to implement a JSON.parse method in JavaScript?
  79. Can you explain how to implement a setTimeout method in JavaScript?
  80. Can you explain how to implement a setInterval method in JavaScript?
  81. Can you explain how to implement a clearTimeout method in JavaScript?
  82. Can you explain how to implement a clearInterval method in JavaScript?
  83. Can you explain how to implement a function expression in JavaScript?
  84. Can you explain how to implement a function declaration in JavaScript?
  85. Can you explain how to implement a function constructor in JavaScript?
  86. Can you explain how to implement a function bind method in JavaScript?
  87. Can you explain how to implement a function call method in JavaScript?
  88. Can you explain how to implement a function apply method in JavaScript?
  89. Can you explain how to implement a named function in JavaScript?
  90. Can you explain how to implement an anonymous function in JavaScript?
  91. Can you explain how to implement a self-invoking function in JavaScript?
  92. Can you explain how to implement a function closure in JavaScript?
  93. Can you explain how to implement a function memoization in JavaScript?
  94. Can you explain how to implement a function currying in JavaScript?
  95. Can you explain how to implement a function composition in JavaScript?
  96. Can you explain how to implement a function pipe in JavaScript?
  97. Can you explain how to implement a function chaining in JavaScript?
  98. Can you explain how to implement a function partial application in JavaScript?
  99. Can you explain how to implement a function higher-order function in JavaScript?
  100. Can you explain how to implement a function decorator in JavaScript?

ANSWERS

What is closure in JavaScript? 

A closure is a function that has access to variables in its outer scope, even after the outer function has returned. This allows for the preservation of state and the ability to create private variables within a function.

Real life use case: A closure can be used to create a private counter in a function, which can only be modified and accessed by that function.

function counter(){let count = 0; return function(){count++; return count; }}const myCounter = counter(); console.log(myCounter()); // 1 console.log(myCounter()); // 2 console.log(myCounter()); // 3

 

What is the difference between null and undefined in JavaScript? 

null is a value that represents the intentional absence of any object value. It is often used to indicate that an object reference has been intentionally cleared. undefined is a value that indicates that a variable has been declared but has not yet been assigned a value.

Real life use case: A variable can be declared as null to indicate that it will be used later to reference an object, while a variable that has not been assigned a value will be undefined.

let obj = null; let name; console.log(obj); // null console.log(name); // undefined

Can you explain the difference between == and === in JavaScript? 

== performs type coercion, meaning that it will attempt to convert the operands to the same type before performing the comparison. === performs strict equality, meaning that it will only return true if the operands are of the same type and have the same value.

Real life use case: In most cases, it is recommended to use === instead of == for reliable and predictable comparison results.

console.log(1 =='1'); // true console.log(1 ==='1'); // false

 

Can you explain hoisting in JavaScript?

 Hoisting is a behavior in JavaScript where variables and function declarations are moved to the top of their respective scopes, before code execution. This means that variables and functions can be used before they are declared in the code.

Real life use case: Consider the following code, where the variable x is declared after it is used in the code:

console.log(x); // undefined var x = 5;

Due to hoisting, the JavaScript engine will interpret the code as follows:

var x; console.log(x); // undefined x = 5;

 

Can you explain how to implement inheritance in JavaScript? 

Inheritance in JavaScript can be implemented using prototypes. A prototype is an object that is used as a blueprint for creating new objects. When a new object is created, it can inherit properties and methods from its prototype.

Real life use case: Consider the following code, where a Person prototype is used to create a new Developer object that inherits properties and methods from the Person prototype:

function Person(name, age){this.name = name; this.age = age; }Person.prototype.getInfo = function(){return `Name: ${this.name}, Age: ${this.age}`; }function Developer(name, age, skills){Person.call(this, name, age); this.skills = skills; }Developer.prototype = Object.create(Person.prototype); Developer.prototype.constructor = Developer; Developer.prototype.getSkills = function(){return this.skills; }const john = new Developer('John Doe', 30, ['JavaScript', 'Node.js', 'React']); console.log(john.getInfo()); // Name: John Doe, Age: 30 console.log(john.getSkills()); // [ 'JavaScript', 'Node.js', 'React' ]

Masynctech Coding School

Article by Masynctech Coding School

Published 12 Feb 2023