1D0-720 · Question #34
Which of the following is a way to define an anonymous function in JavaScript?
The correct answer is A. var x = function() { ... }. Option A is correct because it assigns a function expression to a variable - the function itself has no name, making it anonymous. Option B defines a named function declaration (x is the name, so it's not anonymous). Option C uses invalid syntax - JavaScript has no function…
Question
Which of the following is a way to define an anonymous function in JavaScript?
Options
- Avar x = function() { ... };
- Bfunction x() { ... }
- Cfunction: x() { ... }
- Dvar x = new Function() { ... };
How the community answered
(21 responses)- A95% (20)
- B5% (1)
Explanation
Option A is correct because it assigns a function expression to a variable - the function itself has no name, making it anonymous. Option B defines a named function declaration (x is the name, so it's not anonymous). Option C uses invalid syntax - JavaScript has no function: keyword, making it a red herring. Option D is nearly valid syntax but wrong: new Function() takes a string argument like new Function('return 1'), not a code block with { }.
Memory tip: An anonymous function has no name after the function keyword - in option A, function() has nothing between function and (), which is the hallmark of anonymous. If you see a name there, it's named.
Topics
Community Discussion
No community discussion yet for this question.