JavaScript Function Quiz Quiz

Challenge your understanding of JavaScript function definitions, syntax, and concepts with this easy quiz, ideal for beginners expanding their coding skills.

  1. Function Declaration Syntax

    Which line shows the correct syntax for declaring a function named 'greet' in JavaScript?

    1. function: greet() { }
    2. def greet() { }
    3. func greet() end
    4. function greet() { }

    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.

  2. Calling a Function

    After defining a function called 'sayHello', how would you call it with no arguments?

    1. call sayHello;
    2. sayHello[];
    3. sayHello();
    4. call(sayHello);

    Explanation: 'sayHello();' correctly calls the function with parentheses. 'call sayHello;' and 'call(sayHello);' are not valid JavaScript syntax, while 'sayHello[];' uses incorrect brackets.

  3. Return Keyword

    Which keyword is used within a JavaScript function to send a value back to the code that called it?

    1. output
    2. return
    3. giveback
    4. reply

    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.

  4. Anonymous Function

    What is an 'anonymous function' in JavaScript?

    1. A function inside an object
    2. A function with multiple arguments
    3. A function that returns undefined
    4. A function without a name

    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.

  5. Function Parameters

    In JavaScript, what are values named inside the parentheses of a function definition called?

    1. Arguments
    2. Identifiers
    3. Parameters
    4. Returns

    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.

  6. Function Expression

    How can you assign a function to a variable in JavaScript?

    1. const sayHi = function() { };
    2. var sayHi() = function { };
    3. assign sayHi() { };
    4. function = sayHi() { };

    Explanation: A function expression assigns a function to a variable. 'function = sayHi() { };', 'assign sayHi() { };', and 'var sayHi() = function { };' do not follow correct JavaScript syntax.

  7. Default Function Return Value

    What value does a JavaScript function return if there is no return statement?

    1. NaN
    2. null
    3. 0
    4. undefined

    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.

  8. Arrow Function

    Which is the correct way to define a simple arrow function called 'sum' that adds two numbers?

    1. let sum = (a, b) > a + b;
    2. sum = function a, b => a + b;
    3. function sum (a b) => a + b;
    4. const sum = (a, b) => a + b;

    Explanation: The first option uses correct arrow function syntax. The others use incorrect operators, misplaced keywords, or missing commas.

  9. Function Scope

    What best describes variable scope within a JavaScript function?

    1. Variables declared inside a function are only accessible within that function.
    2. Variables inside a function can override global variables permanently.
    3. Variables outside a function can always be changed from inside.
    4. Variables inside a function are accessible everywhere.

    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.

  10. Naming Functions

    Which of the following is a valid function name in JavaScript?

    1. function-xyz
    2. var
    3. _calculateSum
    4. 123function

    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'.