FahmidasClassroom

Learn by easy steps

Js

1. What is JavaScript?

JavaScript is a high-level, interpreted programming language used to make web pages interactive.

2. What are the key features of JavaScript?

Features include: dynamic typing, prototype-based object-orientation, functions as first-class objects, and single-threaded event-driven programming.

3. What are the different data types in JavaScript?

JavaScript has six primitive data types: string, number, boolean, null, undefined, and symbol. Objects (object) and functions (function) are also types.

4. What is the difference between undefined and null?

undefined means a variable has been declared but has not yet been assigned a value. null is an assignment value representing a lack of a value.

5. How do you check if a variable is an array in JavaScript?

You can use the Array.isArray() method: Array.isArray(myVar).

6. What is a closure in JavaScript?

A closure is a function that retains access to its lexical scope, even when the function is executed outside that lexical scope.

7. What is the difference between == and === in JavaScript?

== checks for equality of value, while === checks for equality of value and type.

8. How do you declare a variable in JavaScript?

You can declare variables using var, let, or const. Example: var myVar;, let myVar;, const myVar = 10;.

9. What is the use of the ‘this’ keyword in JavaScript?

‘this’ refers to the object it belongs to. In a method, this refers to the owner object. In a function, this refers to the global object (in non-strict mode) or undefined (in strict mode).

10. Explain the event delegation in JavaScript.

Event delegation is a technique involving adding event listeners to a parent element instead of adding them to the descendant elements. The listener waits and captures events bubbling up from the child elements.

11. What is the difference between let, var, and const?

var has function scope or global scope, let has block scope, and const declares a constant whose value cannot be changed once assigned.

12. What is a promise in JavaScript?

A promise is an object representing the eventual completion or failure of an asynchronous operation and its resulting value.

13. How do you handle errors in JavaScript?

Errors in JavaScript can be handled using try, catch, and finally blocks. Example:

try {

// code that may throw an error
} catch (error) {

// handle the error

} finally {

// execute code after try/catch

}




14. What is a callback function in JavaScript?

A callback function is a function passed into another function as an argument to be executed later.

15. Explain the concept of hoisting in JavaScript.

Hoisting is JavaScript’s default behavior of moving all declarations to the top of the current scope (global or function scope).

16. What is the use strict mode in JavaScript?

use strict is a directive introduced in ECMAScript 5 that enables stricter parsing and error handling in JavaScript.

17. How do you clone an object in JavaScript?

You can clone an object using various methods such as Object.assign(), spread syntax (…), or JSON.parse(JSON.stringify(obj)).

18. What is event bubbling and event capturing in JavaScript?

Event bubbling and capturing are two ways of event propagation in the DOM. Bubbling occurs from the innermost target element up to the outermost ancestor. Capturing occurs from the outermost ancestor down to the target element.

19. What is the difference between map and forEach methods in JavaScript?

map creates a new array with the results of calling a provided function on every element in the array, while forEach executes a provided function once for each array element.

20. How does JavaScript handle asynchronous code?

JavaScript uses mechanisms such as callbacks, promises, and async/await to handle asynchronous code execution.

21. What is the difference between window.onload and document.ready in jQuery?

window.onload waits for all assets to be loaded before executing, while document.ready (in jQuery) waits for the DOM to be ready.

22. Explain the concept of event loop in JavaScript.

The event loop is responsible for executing code, collecting and processing events, and executing queued sub-tasks.

23. What are arrow functions in JavaScript?

Arrow functions are a concise way to write function expressions in JavaScript, using the => syntax.

24. How do you compare two objects in JavaScript?

Objects in JavaScript are compared by reference, so two objects are only equal if they reference the same underlying object.

25. What are the rest parameters in JavaScript?

Rest parameters (…args) allow a function to accept an indefinite number of arguments as an array.

26. What is the prototype property in JavaScript?

The prototype property allows you to add methods or properties to an object constructor function, making those methods and properties available to all instances created with that constructor function.

27. Explain the difference between synchronous and asynchronous functions.

Synchronous functions execute in sequence and block the execution of subsequent code until they return. Asynchronous functions allow the execution of other code while waiting for a response from an asynchronous operation.

28. How can you get query string parameters from the URL in JavaScript?

You can use the URLSearchParams object or parse the query string manually using split and forEach.

29. What are modules in JavaScript?

Modules are reusable pieces of code that can be exported from one program and imported into another program.

30. How do you handle exceptions in async/await functions?

You can use try/catch blocks to handle exceptions in async/await functions just like synchronous code.

31. What is the difference between push() and pop() methods in JavaScript?

push() adds elements to the end of an array, while pop() removes and returns the last element of an array.

32. What is the Event.preventDefault() method used for?

Event.preventDefault() prevents the default action of the event from being triggered.

33. How do you convert JSON data to a JavaScript object?

You can use JSON.parse() to convert JSON data (a string) into a JavaScript object.

34. Explain the concept of memorization in JavaScript.

Memorization is an optimization technique where the return value of a function is cached based on its input parameters, so that subsequent calls with the same parameters can be retrieved from the cache instead of recalculating.

35. What is the difference between splice() and slice() methods in JavaScript?

splice() changes the contents of an array by removing or replacing existing elements and/or adding new elements, while slice() returns a shallow copy of a portion of an array into a new array object selected from begin to end (end not included).

36. What is the map() method in JavaScript used for?

map() creates a new array populated with the results of calling a provided function on every element in the calling array.

37. How do you detect if JavaScript is enabled in a browser?

You can check typeof window !== ‘undefined’ or use <noscript> tags as fallbacks in HTML.

38. Explain the concept of currying in JavaScript.

Currying is a technique of translating the evaluation of a function that takes multiple arguments into evaluating a sequence of functions, each with a single argument.

39. What is the includes() method in JavaScript used for?

includes() is used to determine whether an array includes a certain value among its entries, returning true or false as appropriate.

40. What are template literals in JavaScript?

Template literals are string literals allowing embedded expressions. They are enclosed by backticks (“) instead of quotes.

41. How do you iterate over a list of elements using JavaScript?

You can use various methods like forEach for arrays or a simple for loop for iterating over lists or arrays.

42. What is the difference between find() and filter() methods in JavaScript?

find() returns the first element in an array that satisfies a provided testing function, while filter() creates a new array with all elements that pass the test implemented by the provided function.

43. What are the different ways to create objects in JavaScript? Objects can be created

using object literals {}, constructor functions, Object.create(), and ES6 classes.

44. Explain the concept of prototypal inheritance in JavaScript. Prototypal inheritance is

the mechanism by which objects in JavaScript inherit properties and methods from other objects. Every object has a prototype property, which makes inheritance possible.

45. What is the difference between document.getElementById() and document.querySelector()?

document.getElementById() selects an element by its id attribute, returning the element, while document.querySelector() selects the first element that matches a specified CSS selector.

46. How do you add an element to the DOM dynamically in JavaScript?

You can create an element using document.createElement(), modify its attributes and content, and then append it to an existing element using methods like appendChild().

47. What is event delegation in JavaScript?

Event delegation refers to the technique of attaching a single event listener to a parent element in order to manage events for multiple child elements.

48. Explain the difference between innerText and textContent.

innerText retrieves and sets the text content of an element, while textContent gets or sets the text content of a node and its descendants.

49. What is a cookie in JavaScript?

A cookie is a small piece of data stored on the client-side, which can be accessed and modified by the server-side scripts.

50. What is a web worker in JavaScript?

Web Workers allow you to run JavaScript code in background threads, allowing for concurrent execution and better performance for tasks like heavy computations or network operations.

These questions cover a wide range of topics that are typically important for JavaScript developers to know in interviews.