JavaScript Functions & Parameters Quiz
Explore 50 JavaScript function questions covering declarations, expressions, arrow functions, scopes, parameters, defaults, rest arguments, return values, closures, and execution flow.
Question 1
What does this function log when called?
function greet() {
console.log('Hi')
}
greet()Question 2
What value is logged when the function returns a string?
function say() {
return 'hello'
}
console.log(say())Question 3
What is printed when a function has no return statement?
function add(a, b) {
const sum = a + b
}
console.log(add(2, 3))Question 4
How many times does this function execute console.log?
function run() {
console.log('X')
}
run(); run();Question 5
What is logged when passing parameters into a function?
function print(x) { console.log(x) }
print('A')Question 6
What happens when calling a function expression before it is defined?
console.log(fn())
const fn = function(){ return 1 }Question 7
What is printed when calling a hoisted function declaration?
console.log(sum(2, 3))
function sum(a, b){ return a + b }Question 8
What does this arrow function return?
const fn = () => 10
console.log(fn())Question 9
What value is printed when using an arrow function with block braces but missing return?
const fn = () => { 3 }
console.log(fn())Question 10
What is printed here using a function expression?
const go = function() { return 'ok' }
console.log(go())Question 11
What is logged using a default parameter?
function speak(msg = 'hi'){ console.log(msg) }
speak()Question 12
What value prints for this default parameter function?
function calc(a = 5, b = 3){ return a + b }
console.log(calc(2))Question 13
What is logged when passing undefined to a default parameter?
function get(x = 'A'){ return x }
console.log(get(undefined))Question 14
What does this return with null and a default param?
function fn(a = 5){ return a }
console.log(fn(null))Question 15
What is the output when extra arguments are passed?
function f(a){ return a }
console.log(f(1, 2, 3))Question 16
What does this rest parameter function return?
function sum(...nums){ return nums.length }
console.log(sum(1,2,3))Question 17
Which value is logged using rest parameters?
function first(...items){ return items[0] }
console.log(first('A','B'))Question 18
What does this function log with both normal and rest params?
function wrap(x, ...y){ console.log(y.length) }
wrap(1,2,3,4)Question 19
What happens if rest params are not last?
function bad(...x, y){}Question 20
What is printed when calling a function with no arguments but expecting rest params?
function fn(...a){ console.log(a.length) }
fn()Question 21
What does this function log using the arguments object?
function f(){ console.log(arguments.length) }
f(1,2,3)Question 22
What does accessing arguments[0] return?
function f(){ return arguments[0] }
console.log(f('A', 'B'))Question 23
What value prints if no arguments are passed?
function f(){ return arguments[1] }
console.log(f())Question 24
Which keyword cannot be used inside arrow functions?
Question 25
What value does this arrow function return?
const fn = () => arguments
console.log(fn(1,2))Question 26
What value is returned here?
function test(){ return }
console.log(test())Question 27
Which value is logged when a return appears before a statement?
function f(){ return 1; console.log(2) }
console.log(f())Question 28
What is returned when a return statement spans lines incorrectly?
function f(){
return
5
}
console.log(f())Question 29
What value prints when returning an object literal incorrectly?
function f(){ return { a: 1 } }
console.log(f().a)Question 30
What value is returned in this example with parentheses?
function f(){ return (
2
) }
console.log(f())Question 31
What does this closure return?
function outer(){
let x = 1
return function(){ return x }
}
console.log(outer()())Question 32
Which value is logged when reassigning the closed-over variable?
function make(){
let n = 5
return ()=>{ return n }
}
const f = make();
n = 10
console.log(f())Question 33
What does this nested function log?
function a(){
const x = 'A'
function b(){ console.log(x) }
b()
}
a()Question 34
What is printed using block scope variables?
let x = 'global'
function f(){
let x = 'local'
return x
}
console.log(f())Question 35
What value prints when accessing a variable before declaration inside a function?
function f(){ console.log(a); var a = 1 }
f()Question 36
Which value is logged inside this arrow function?
const obj = {
x: 10,
f: () => this.x
}
console.log(obj.f())Question 37
What does this regular function return for this.x?
const obj = {
x: 10,
f: function(){ return this.x }
}
console.log(obj.f())Question 38
What does this arrow function return inside setTimeout?
const obj = {
x: 7,
run(){ setTimeout(()=>console.log(this.x), 0) }
}
obj.run()Question 39
What prints when calling a method detached from its object?
const obj = {
x: 3,
f(){ return this.x }
}
const detached = obj.f
console.log(detached())Question 40
Which value is logged when binding this explicitly?
function show(){ return this.v }
const obj = { v: 20 }
console.log(show.bind(obj)())Question 41
What is logged when a function returns another function?
function outer(){ return ()=>5 }
console.log(outer()())Question 42
What does this mapping function return?
const arr = [1,2]
const out = arr.map(x => x * 2)
console.log(out[1])Question 43
Which output is logged when passing a function as a callback?
function run(cb){ cb() }
run(()=>console.log('X'))Question 44
What does this reducer function return?
const nums = [1,2,3]
const total = nums.reduce((a,b)=>a+b,0)
console.log(total)Question 45
What is logged when calling a function that returns a function with arguments?
function make(x){ return y => x + y }
console.log(make(3)(4))Question 46
Why can functions be passed around as values?
Question 47
Why should deeply nested function chains be avoided?
Question 48
Why can default parameters improve clarity in APIs?
Question 49
Why can mutating parameters directly cause confusion?
Question 50
Why is documenting function behavior important?
