JavaScript Function Fundamentals Quiz
Four day-one essentials about functions: how to define them, when to choose declaration vs expression vs arrow, default parameters, and rest/spread.
Question Bank
Easy
JavaScript
4 questions
quiz
functions
fundamentals
js-arrow-functions
770 views
19
Write a greet(name) function declaration that returns 'Hello, <name>!'. Then call it and log the result.
Examples
Example 1:
Input: greet('Mohit')
Output: 'Hello, Mohit!'
Explanation: A function declaration creates a named binding and a callable body in one statement. Hoisting makes it usable before its source position.function greet(name) {
// your code here
}
console.log(greet('Mohit'));Compare function declaration, function expression, and arrow function in three sentences. Highlight this binding, hoisting, and arguments.
Examples
Example 1:
Input: function f() {}, const f = function () {}, const f = () => {}
Output: f1 hoisted + own `this` + own `arguments`; f2 not hoisted + own `this` + own `arguments`; f3 not hoisted + lexical `this` + no `arguments`.
Explanation: Arrow functions inherit `this` from the enclosing lexical scope and do not get their own `arguments` object.// function f() {}
// const f = function () {};
// const f = () => {};Write greet(name = 'World') so calling it with no arg returns 'Hello, World!' and with 'Alex' returns 'Hello, Alex!'. Then explain what value of name skips the default.
Examples
Example 1:
Input: greet(), greet('Alex'), greet(undefined), greet(null)
Output: 'Hello, World!', 'Hello, Alex!', 'Hello, World!', 'Hello, null!'
Explanation: Default parameter values only apply when the argument is `undefined`. Passing `null` keeps `null` as the value.function greet(name = 'World') {
// your code here
}Write max(...nums) returning the largest of any number of arguments. Then call it both with positional args and with a spread array.
Examples
Example 1:
Input: max(1, 5, 3), max(...[10, 20, 15])
Output: 5, 20
Explanation: Rest collects positional args into `nums`; spread unpacks an array at the call site.function max(/* ...nums */) {
// your code here
}