JavaScript Basics - variables, loops, arrays, functions, events, DOM interactions, alert-confirm-prompt, rollover images - One Line Questions

1. What is the difference between `var` and `let` when declaring variables? `var` has function scope or global scope, `let` has block scope
2. Consider the array `let fruits = ['apple', 'banana', 'cherry'];`. What will `fruits[1]` return? 'banana'
3. What is the result of `console.log(typeof 'Hello')`? 'string'
4. What is the output of `console.log(2 + 3 + '5')`? '235'
5. What is the value of `fruits.length` for the array `let fruits = ['apple', 'banana', 'cherry'];`? 3
6. What is an anonymous function in JavaScript? A function that is not declared with a name
7. What is an 'event' in the context of JavaScript and web development? A user action or system occurrence that can be detected by JavaScript
8. In JavaScript, what is an array? A special type of variable that can hold multiple values under a single name
9. What is a closure in JavaScript (related to functions)? A function's ability to access variables from its outer scope even after the outer function has finished executing
10. Which statement best describes the `prompt()` function's return value when the user clicks 'Cancel'? null
11. What is a 'rollover image' technique in web development? An image that changes when the mouse pointer hovers over it
12. How do you add an element to the end of a JavaScript array? array.push(element)
13. What is the basic syntax for accessing an element in a JavaScript array by its index? arrayName[index]
14. What is the scope of a variable declared with `var` inside a function? Function scope
15. Which JavaScript function displays a message box with an OK button? alert()
16. Which keyword is used to declare a variable in JavaScript that can be reassigned? let
17. What does the `const` keyword do in JavaScript? Declares a variable that cannot be reassigned
18. The `prompt()` function in JavaScript is used to: Display a message and get text input from the user
19. What does 'DOM' stand for in web development? Document Object Model
20. How can you change the text content of an HTML element using JavaScript? All of the above
21. To remove an HTML element using JavaScript, you can use: element.remove()
22. How do you add a new attribute to an HTML element using JavaScript? element.setAttribute('attributeName', 'value')
23. Which loop is guaranteed to execute at least once? do...while loop
24. Which loop is commonly used for iterating over the properties of an object? for...in loop
25. Which keyword is used to define a function in JavaScript? function
26. Which of the following is a way to declare a function expression in JavaScript? const myFunction = function() {}
27. Which DOM method is used to select all elements with a specific tag name? getElementsByTagName()
28. Which DOM method is used to select an HTML element by its ID? getElementById()
29. Which DOM method returns an HTMLCollection of all elements with a given class name? getElementsByClassName()
30. What event occurs when the value of an input element changes and the element loses focus? onchange
31. What is the event triggered when a user presses a key on the keyboard? onkeydown
32. In JavaScript, a function can: Return any data type, including other functions
33. Which event listener is triggered when a user clicks on an HTML element? onclick
34. What is the JavaScript method to remove the last element from an array? pop()
35. What does the `shift()` array method do? Removes the first element and returns it
36. What is the `unshift()` array method used for? Adding elements to the beginning
37. What does `document.querySelector('.my-class')` do? Selects the first element with the class 'my-class'
38. Which of the following is NOT a valid JavaScript data type? Character
39. What is the primary use of `alert()`, `confirm()`, and `prompt()`? Providing simple user interaction and feedback
40. What is an argument in a JavaScript function? The actual value passed to the function when it is called
41. What is a parameter in a JavaScript function? A variable listed inside the parentheses in the function definition
42. What is the primary advantage of using `let` and `const` over `var` in modern JavaScript? They provide block-scoping, reducing potential errors.
43. What is the purpose of the `confirm()` function in JavaScript? To display a message with OK and Cancel buttons
44. What is the purpose of the `while` loop in JavaScript? To execute a block of code as long as a specified condition is true
45. What is the primary purpose of a `for` loop in JavaScript? To iterate over a sequence of elements a specified number of times
46. What is the purpose of `addEventListener()`? To attach an event handler to an element
47. What is the `event.preventDefault()` method used for? To prevent the default action of an element (e.g., following a link)
48. To implement a rollover image, you typically need to: Use JavaScript to change the `src` attribute of the `<img>` tag on mouse events
49. Which statement about JavaScript variables is true? Variables declared with `var` are hoisted.
50. Which loop is most suitable for iterating over the elements of an array when the number of iterations is known? for loop