4.
Which of the following statements about anonymous functions in JavaScript is true?
Anonymous functions cannot have a return statement.
Anonymous functions are often used as callback functions.
Anonymous functions cannot be assigned to variables.
Anonymous functions cannot have parameters.
Correct: B
Anonymous functions in JavaScript are often used as callback functions, which means they are passed as arguments to other functions and executed at a later time. This allows for more flexibility and dynamic functionality in our code. Anonymous functions can have parameters, can be assigned to variables, and can have a return statement, just like named functions.
const numbers = [1, 2, 3, 4, 5];
numbers.forEach(function(number) {
console.log(number);
}); // Output: 1 2 3 4 5