A function has two parameters and is called with three arguments, and later with one. What happens?
JavaScript does not check argument counts. Use default parameters or validation if a value is required.
Functions · Exercise 1 of 4
Fifteen multiple-choice questions on JavaScript functions: declarations and expressions, hoisting, default and rest parameters, arguments, fn.length, return values, callbacks, higher-order functions, IIFE, pass by sharing and pure functions. Pick an option to check your answer instantly.
A function has two parameters and is called with three arguments, and later with one. What happens?
JavaScript does not check argument counts. Use default parameters or validation if a value is required.
In const fact = function calc(n) { … };, where can the name calc be used?
The name of a function expression is bound only inside its own body, so calc(n - 1) works inside, while typeof calc outside is "undefined". It also appears in stack traces.
Which function is pure?
A pure function returns the same output for the same input and has no side effects. Logging and changing counter are side effects; Date.now() depends on outside state.
In setTimeout(showAlert, 1000), what is showAlert?
setTimeout calls showAlert after the delay. Writing showAlert() would call it immediately and pass its result instead.
For function add(item, list = []) { list.push(item); return list; }, what do add(1) and then add(2) return?
Unlike Python, JavaScript evaluates default expressions each time the default is needed, so calls never share a default object.
Timed online tests with instant scores and explanations.