JavaScript Basics - variables, loops, arrays, functions, events, DOM interactions, alert-confirm-prompt, rollover images - Question Bank
1. What is the primary advantage of using `let` and `const` over `var` in modern JavaScript?
2. Which statement best describes the `prompt()` function's return value when the user clicks 'Cancel'?
3. What is the `event.preventDefault()` method used for?
4. To remove an HTML element using JavaScript, you can use:
5. Which DOM method returns an HTMLCollection of all elements with a given class name?
6. What event occurs when the value of an input element changes and the element loses focus?
7. What is a closure in JavaScript (related to functions)?
8. Which loop is commonly used for iterating over the properties of an object?
9. What is the output of `console.log(2 + 3 + '5')`?
10. Which statement about JavaScript variables is true?
11. What is the primary use of `alert()`, `confirm()`, and `prompt()`?
12. How do you add a new attribute to an HTML element using JavaScript?
13. Which DOM method is used to select all elements with a specific tag name?
14. What is the event triggered when a user presses a key on the keyboard?
15. What is an anonymous function in JavaScript?
16. Which of the following is a way to declare a function expression in JavaScript?
17. What is the scope of a variable declared with `var` inside a function?
18. What is the value of `fruits.length` for the array `let fruits = ['apple', 'banana', 'cherry'];`?
19. Consider the array `let fruits = ['apple', 'banana', 'cherry'];`. What will `fruits[1]` return?
20. Which loop is guaranteed to execute at least once?
21. What is the purpose of the `while` loop in JavaScript?
22. To implement a rollover image, you typically need to:
23. What is a 'rollover image' technique in web development?
24. What does `document.querySelector('.my-class')` do?
25. How can you change the text content of an HTML element using JavaScript?
26. Which DOM method is used to select an HTML element by its ID?
27. What is the purpose of `addEventListener()`?
28. Which event listener is triggered when a user clicks on an HTML element?
29. What is an argument in a JavaScript function?
30. What is a parameter in a JavaScript function?
31. In JavaScript, a function can:
32. What is the `unshift()` array method used for?
33. What does the `shift()` array method do?
34. What is the JavaScript method to remove the last element from an array?
35. How do you add an element to the end of a JavaScript array?
36. What is the result of `console.log(typeof 'Hello')`?
37. Which of the following is NOT a valid JavaScript data type?
38. What does the `const` keyword do in JavaScript?
39. What is the difference between `var` and `let` when declaring variables?
40. Which loop is most suitable for iterating over the elements of an array when the number of iterations is known?
41. What is the basic syntax for accessing an element in a JavaScript array by its index?
42. The `prompt()` function in JavaScript is used to:
43. What is the purpose of the `confirm()` function in JavaScript?
44. Which JavaScript function displays a message box with an OK button?
45. What does 'DOM' stand for in web development?
46. What is an 'event' in the context of JavaScript and web development?
47. Which keyword is used to define a function in JavaScript?
48. In JavaScript, what is an array?
49. What is the primary purpose of a `for` loop in JavaScript?
50. Which keyword is used to declare a variable in JavaScript that can be reassigned?