0% found this document useful (0 votes)
1 views7 pages

Javascript Interview Questions

Uploaded by

Akash K.n
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views7 pages

Javascript Interview Questions

Uploaded by

Akash K.n
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Core JavaScript Interview Questions and Answers

1. What are the different data types in JavaScript?

Answer:

• Primitive: String, Number, Boolean, Null, Undefined, Symbol, BigInt


• Non-primitive: Object (including arrays, functions)

2. Difference between var , let , and const ?

Answer:

• var : function-scoped, can be re-declared and updated.


• let : block-scoped, can be updated but not re-declared in same scope.
• const : block-scoped, cannot be updated or re-declared.

3. What is hoisting?

Answer: Hoisting is JavaScript's default behavior of moving declarations to the top of the current scope.

4. Difference between == and === ?

Answer:

• == : compares values after type coercion.


• === : compares values and types.

5. What is a closure?

Answer: A closure is a function that remembers its outer variables even after the outer function has
finished executing.

6. What is scope?

Answer: Scope determines the accessibility of variables. JavaScript has global, function, and block scopes.

7. What are truthy and falsy values?

Answer:

• Falsy: false, 0, '', null, undefined, NaN


• Everything else is truthy.

1
8. null vs undefined ?

Answer:

• null : explicitly assigned to indicate no value.


• undefined : variable declared but not assigned a value.

9. typeof vs instanceof ?

Answer:

• typeof : checks data type.


• instanceof : checks if an object is an instance of a specific constructor.

10. Template literals?

Answer: ES6 feature using backticks (``) allowing embedded expressions: Hello, ${name}

11. Function declaration vs function expression?

Answer:

• Declaration: hoisted.
• Expression: not hoisted.

12. Arrow function vs regular function?

Answer: Arrow functions:

• No this binding.
• Cannot be used as constructors.

13. What is this ?

Answer: this refers to the object that is executing the current function.

14. What is the call stack?

Answer: A data structure that tracks function calls in LIFO order.

15. Event loop?

Answer: Handles asynchronous code by managing the call stack and callback queue.

2
16. call() , apply() , bind() ?

Answer:

• call() : invokes function with arguments individually.


• apply() : invokes function with arguments as an array.
• bind() : returns new function with bound context.

17. First-class functions?

Answer: Functions can be treated as variables, passed as arguments, or returned from other functions.

18. Currying?

Answer: Transforming a function with multiple arguments into a sequence of functions each taking one
argument.

19. Loop through array/object?

Answer:

• Arrays: for , for...of , forEach


• Objects: for...in , Object.keys()

20. for...in vs for...of ?

Answer:

• for...in : iterates over keys.


• for...of : iterates over values.

21. Array methods?

Answer:

• map() : returns new array.


• filter() : returns filtered array.
• reduce() : returns single value.
• forEach() : iterates over array, no return.

22. Destructuring?

Answer: Extracting values from arrays/objects into variables.

3
23. Clone object/array?

Answer:

• Shallow copy: Object.assign({}, obj) , [...arr]


• Deep copy: JSON.parse(JSON.stringify(obj))

24. Callbacks?

Answer: Functions passed as arguments to be called later.

25. Promises?

Answer: An object representing the eventual completion/failure of an async operation.

26. async / await ?

Answer: Syntax to write asynchronous code that looks synchronous.

27. Error handling in async?

Answer: Use try...catch with async/await , or .catch() with Promises.

28. Microtasks vs macrotasks?

Answer:

• Microtasks: Promise.then() , queueMicrotask()


• Macrotasks: setTimeout() , setInterval()

29. Spread vs rest operator?

Answer:

• Spread: ... to expand.


• Rest: ... to collect.

30. Default parameters?

Answer: Function parameters with default values: function(a = 1) {}

31. ES6 destructuring?

Answer: Extracting data from arrays/objects: const {a} = obj or const [a] = arr

4
32. Set and Map ?

Answer:

• Set : stores unique values.


• Map : stores key-value pairs with any type of key.

33. Optional chaining & nullish coalescing?

Answer:

• ?. : safely access deeply nested properties.


• ?? : fallback only if null or undefined .

34. DOM interaction?

Answer: Using methods like getElementById , querySelector , etc.

35. Event bubbling vs capturing?

Answer:

• Bubbling: event moves from target to root.


• Capturing: event moves from root to target.

36. Event delegation?

Answer: Attaching event handler to parent instead of individual elements.

37. preventDefault() & stopPropagation() ?

Answer:

• preventDefault() : stops default action.


• stopPropagation() : stops event from bubbling up.

38. localStorage vs sessionStorage vs cookies?

Answer:

• localStorage : persistent until cleared.


• sessionStorage : cleared on tab close.
• cookies : sent with HTTP requests.

5
39. [] + {} vs {} + [] ?

Answer:

• [] + {} = '[object Object]'
• {} + [] = 0 (interpreted as code block + array)

40. IIFE?

Answer: Immediately Invoked Function Expression: (function(){})()

41. Debounce vs throttle?

Answer:

• Debounce: delay execution until inactivity.


• Throttle: limit function calls to once per interval.

42. Memoization?

Answer: Caching the result of function calls.

43. Object.freeze() vs Object.seal() ?

Answer:

• freeze() : no changes allowed.


• seal() : can't add/delete, but can modify.

44. Reverse a string?

function reverse(str) {
return str.split('').reverse().join('');
}

45. Flatten nested array?

function flatten(arr) {
return arr.flat(Infinity);
}

6
46. Palindrome check?

function isPalindrome(str) {
return str === str.split('').reverse().join('');
}

47. Debounce function?

function debounce(fn, delay) {


let timeout;
return function(...args) {
clearTimeout(timeout);
timeout = setTimeout(() => fn.apply(this, args), delay);
}
}

48. Factorial?

function factorial(n) {
return n <= 1 ? 1 : n * factorial(n - 1);
}

49. Find duplicates in array?

function findDuplicates(arr) {
return arr.filter((item, index) => arr.indexOf(item) !== index);
}

50. Deep vs shallow copy?

let shallow = {...obj};


let deep = JSON.parse(JSON.stringify(obj));

You might also like