function testScope() {
console.log(a);
var a = 5;
return a;
}
JavaScript Functions and Scope Quiz
Want to learn more than this quiz offers you? Have a look at my Frontend web
development courses.
Create an account and save your quiz results
Login and save your results
OR
Question 1/15
What does the following code return?
function testScope() {
console.log(a);
var a = 5;
return a;
}
Select your answer
Question 2/15
What will be returned by the arrow function in the following code?
let add = (a, b) => a + b;
let result = add(3, 4);
Select your answer
Question 3/15
What will the function `returnSum` return when called with no arguments?
function returnSum(a = 1, b = 1) {
return a + b;
}
Select your answer
Question 4/15
Given the function declaration, which option correctly calls the function with 3 as an argument?
function printNumber(n) {
console.log(n);
}
Select your answer
Question 5/15
What value will the variable `result` hold?
function multiplier(a, b = 2) {
return a * b;
}
let result = multiplier(4);
Select your answer
Question 6/15
What will the following code log in the console?
let car = {
make: 'Toyota',
year: 2019,
};
car.getAge = function() {
const currentYear = new Date().getFullYear();
return currentYear - this.year;
};
console.log(car.getAge());
Select your answer
Question 7/15
What is the impact of calling `comparison(5, '5')` in this code?
function comparison(x, y) {
return x == y;
}
Select your answer
Question 8/15
What will be the output of this immediately invoked function expression?
(function() {
var a = b = 5;
})();
console.log(typeof a);
console.log(typeof b);
Select your answer
Question 9/15
Consider the below code. What will be logged to the console?
let y = 10;
function modifyVariable() {
y += 5;
console.log(y);
}
modifyVariable();
Select your answer
Question 10/15
What does the following function return when called with argument 5?
function increment(x) {
return ++x;
}
Select your answer
Question 11/15
What will be logged after running the following code?
function logger(x = 'default') {
console.log(x);
}
logger();
Select your answer
Question 12/15
In the code snippet below, what is the value of result?
function square(x) {
return x * x;
}
let result = square(3) + square(4);
Select your answer
Question 13/15
In the following code, what will `greet()` log to the console?
function greet() {
console.log(message);
var message = 'Hello, World!';
}
Select your answer
Question 14/15
For this code, what is returned by `compute()`?
function compute() {
let a = 3;
let b = 4;
return a + b;
}
Select your answer
Question 15/15
Which statement is correct about the `outerFunction` in this code?
function outerFunction() {
var x = 'outer-local';
function innerFunction() {
var x = 'inner-local';
console.log(x);
}
innerFunction();
}
outerFunction();
Select your answer
Your Results
You did not answer any questions correctly.
Your Answers
Question 1/15
😊 Your
answer was correct
🙁 Your
answer was incorrect
What does the following code return?
Available answers
Due to JavaScript's hoisting, the variable `a` is hoisted to the top of the function with an initial value of `undefined`. Thus, console.log(a) prints `undefined` before `a` is assigned to 5.
Question 2/15
😊 Your
answer was correct
🙁 Your
answer was incorrect
What will be returned by the arrow function in the following code?
let add = (a, b) => a + b;
let result = add(3, 4);
Available answers
The arrow function add takes two arguments and returns their sum. When called with 3 and 4, it returns 7.
Question 3/15
😊 Your
answer was correct
🙁 Your
answer was incorrect
What will the function `returnSum` return when called with no arguments?
function returnSum(a = 1, b = 1) {
return a + b;
}
Available answers
Both parameters have default values of 1. If called with no arguments, `returnSum` returns 1 + 1, which is 2.
Question 4/15
😊 Your
answer was correct
🙁 Your
answer was incorrect
Given the function declaration, which option correctly calls the function with 3 as an argument?
function printNumber(n) {
console.log(n);
}
Available answers
`printNumber(3);` correctly calls the function with 3 as an argument. The other options are either incorrect syntax or misassignments.
Question 5/15
😊 Your
answer was correct
🙁 Your
answer was incorrect
What value will the variable `result` hold?
function multiplier(a, b = 2) {
return a * b;
}
let result = multiplier(4);
Available answers
The function `multiplier` defaults `b` to 2 if not provided. The call `multiplier(4)` is thus 4 * 2, resulting in 8.
Question 6/15
😊 Your
answer was correct
🙁 Your
answer was incorrect
What will the following code log in the console?
let car = {
make: 'Toyota',
year: 2019,
};
car.getAge = function() {
const currentYear = new Date().getFullYear();
return currentYear - this.year;
};
console.log(car.getAge());
Available answers
`car.getAge` method calculates and returns the difference between the current year and the car's manufacture year, 2019.
Question 7/15
😊 Your
answer was correct
🙁 Your
answer was incorrect
What is the impact of calling `comparison(5, '5')` in this code?
function comparison(x, y) {
return x == y;
}
Available answers
The loose equality `==` allows type coercion, so `comparison(5, '5')` evaluates to true.
Question 8/15
😊 Your
answer was correct
🙁 Your
answer was incorrect
What will be the output of this immediately invoked function expression?
(function() {
var a = b = 5;
})();
console.log(typeof a);
console.log(typeof b);
Available answers
In the IIFE, `b` is assigned to 5 in the global scope because it's not declared with var, let, or const. `a` is only defined within the function's scope, so it is undefined outside. The console logs 'undefined' and 'number'.
Question 9/15
😊 Your
answer was correct
🙁 Your
answer was incorrect
Consider the below code. What will be logged to the console?
let y = 10;
function modifyVariable() {
y += 5;
console.log(y);
}
modifyVariable();
Available answers
Since y is declared outside the function with a value of 10, and the function modifyVariable adds 5 to y, the logged value will be 15.
Question 10/15
😊 Your
answer was correct
🙁 Your
answer was incorrect
What does the following function return when called with argument 5?
function increment(x) {
return ++x;
}
Available answers
The function uses the prefix increment operator, which increases the value of x by one before the value is returned. So, calling increment(5) returns 6.
Question 11/15
😊 Your
answer was correct
🙁 Your
answer was incorrect
What will be logged after running the following code?
function logger(x = 'default') {
console.log(x);
}
logger();
Available answers
Function `logger` is called without arguments, making `x` default to 'default', which is logged.
Question 12/15
😊 Your
answer was correct
🙁 Your
answer was incorrect
In the code snippet below, what is the value of result?
function square(x) {
return x * x;
}
let result = square(3) + square(4);
Available answers
The function square returns the square of its argument. Thus, square(3) returns 9 and square(4) returns 16. Hence, result is 9 + 16, which is 25.
Question 13/15
😊 Your
answer was correct
🙁 Your
answer was incorrect
In the following code, what will `greet()` log to the console?
function greet() {
console.log(message);
var message = 'Hello, World!';
}
Available answers
Due to hoisting, the variable `message` is declared at the top of the function but not initialized. Thus, `console.log(message)` prints `undefined`.
Question 14/15
😊 Your
answer was correct
🙁 Your
answer was incorrect
For this code, what is returned by `compute()`?
function compute() {
let a = 3;
let b = 4;
return a + b;
}
Available answers
`compute` evaluates the sum of `a` and `b`, which is 3 + 4, resulting in 7.
Question 15/15
😊 Your
answer was correct
🙁 Your
answer was incorrect
Which statement is correct about the `outerFunction` in this code?
function outerFunction() {
var x = 'outer-local';
function innerFunction() {
var x = 'inner-local';
console.log(x);
}
innerFunction();
}
outerFunction();
Available answers
`innerFunction` accesses its own local variable `x` which is 'inner-local'. Thus, `outerFunction` logs 'inner-local'.