Challenge your understanding of JavaScript function definitions, syntax, and concepts with this easy quiz, ideal for beginners expanding their coding skills.
Which line shows the correct syntax for declaring a function named 'greet' in JavaScript?
Explanation: The correct JavaScript function declaration starts with 'function', followed by the function name and parentheses. 'def' is used in Python, 'func' is not a JavaScript keyword, and 'function:' is not the correct syntax.
After defining a function called 'sayHello', how would you call it with no arguments?
Explanation: 'sayHello();' correctly calls the function with parentheses. 'call sayHello;' and 'call(sayHello);' are not valid JavaScript syntax, while 'sayHello[];' uses incorrect brackets.
Which keyword is used within a JavaScript function to send a value back to the code that called it?
Explanation: 'return' is the standard keyword for outputting a value from a function in JavaScript. The words 'reply', 'output', and 'giveback' are not valid in this context.
What is an 'anonymous function' in JavaScript?
Explanation: Anonymous functions don't have a name identifier. Functions inside objects are called methods, functions with multiple arguments or those returning undefined are unrelated to being anonymous.
In JavaScript, what are values named inside the parentheses of a function definition called?
Explanation: The named values in a function's definition are called parameters. Arguments are the values passed when calling the function, 'returns' refers to output, and identifiers are broader than just parameters.
How can you assign a function to a variable in JavaScript?
Explanation: A function expression assigns a function to a variable. 'function = sayHi() { };', 'assign sayHi() { };', and 'var sayHi() = function { };' do not follow correct JavaScript syntax.
What value does a JavaScript function return if there is no return statement?
Explanation: A JavaScript function returns 'undefined' by default if no return statement is used. 'null', 'NaN', and '0' are specific values that must be explicitly returned.
Which is the correct way to define a simple arrow function called 'sum' that adds two numbers?
Explanation: The first option uses correct arrow function syntax. The others use incorrect operators, misplaced keywords, or missing commas.
What best describes variable scope within a JavaScript function?
Explanation: Function scope means variables inside a function are not accessible outside. They do not always override global variables, nor are they available everywhere. Changing outer variables is only possible if accessible and if not shadowed.
Which of the following is a valid function name in JavaScript?
Explanation: Function names can start with underscores, letters, or dollar signs. Names can't begin with numbers, contain hyphens, or use reserved words like 'var'.