Understanding functions and scope is crucial for effective JavaScript programming. This set of 30 multiple-choice questions (MCQs) covers key concepts, from declaring and invoking functions to understanding various scopes in JavaScript. Test your knowledge and ensure you’re ready to work with parameters, return values, arrow functions, and scope types.
Declaring and Invoking Functions
Which keyword is used to declare a function in JavaScript?
A) func
B) declare
C) function
D) def
What is the correct syntax for invoking a function named myFunction?
A) myFunction{}
B) myFunction()
C) invoke myFunction
D) call myFunction
What will the following code output?
function greet() { console.log("Hello, World!"); } greet();
A) Hello, World!
B) greet
C) undefined
D) No output
Which of the following options is NOT a valid function name in JavaScript?
A) _myFunction
B) my-function
C) myFunction
D) $myFunction
In JavaScript, where can a function be called from if it is declared using function syntax?
A) Only below its declaration
B) Only within the same block
C) Anywhere in the code
D) Only above its declaration
Parameters and Return Values
How are parameters defined in a function?
A) Inside brackets []
B) Inside parentheses ()
C) Inside curly braces {}
D) Outside of function body
What will the following function return?
function add(a, b) { return a + b; } console.log(add(5, 3));
A) 53
B) 15
C) 8
D) undefined
What is the purpose of the return statement in a function?
A) To end a function
B) To skip to the next iteration
C) To send a value back to the calling location
D) To execute the function again
If a function does not contain a return statement, what does it return by default?
A) null
B) 0
C) undefined
D) An empty string
What is the correct syntax to pass parameters to a function?
A) function myFunc(param1, param2)
B) myFunc{param1, param2}
C) function myFunc[param1, param2]
D) function (param1, param2)
Function Expressions and Arrow Functions
How is a function expression assigned to a variable?
A) let myFunc = function() {}
B) let function = myFunc {}
C) function myFunc() = {}
D) let function myFunc() {}
Which of the following is the correct syntax for an arrow function?
A) let sum = (a, b) => { return a + b; }
B) let sum = (a, b) -> { return a + b; }
C) let sum = (a, b) { return a + b; }
D) let sum = (a, b) { return a + b; }
Arrow functions were introduced in which version of ECMAScript?
A) ES5
B) ES6
C) ES7
D) ES8
What does an arrow function with a single line of code automatically do?
A) Logs the value
B) Throws an error
C) Returns the value
D) Executes without returning
Which of the following is true about arrow functions in JavaScript?
A) They can be used with this keyword like regular functions.
B) They do not have their own this context.
C) They cannot take parameters.
D) They must always return a value.
Scope: Block Scope, Function Scope, Global Scope
Variables declared with var have which type of scope?
A) Block scope
B) Function scope
C) Module scope
D) Lexical scope
Which of the following keywords enforces block scope?
A) const
B) var
C) static
D) function
A variable declared outside any function or block is in which scope?
A) Block scope
B) Function scope
C) Global scope
D) Local scope
Which of the following can alter the value of a let variable declared inside a function from outside that function?
A) Another function
B) Global context
C) No access possible
D) const declaration
When a variable is defined with const, which of the following is true?
A) It can be re-assigned
B) It cannot be reassigned
C) It cannot be initialized
D) It can be reassigned if inside a loop
Miscellaneous Questions on Functions and Scope
What is the default scope for a function in JavaScript?
A) Block scope
B) Function scope
C) Module scope
D) Global scope
In which of the following is a let variable not accessible?
A) Within the block it was defined
B) Outside the block it was defined
C) Inside a nested function
D) Inside the global scope
Which function declaration method allows hoisting?
A) Function expressions
B) Arrow functions
C) Named function declarations
D) Variable declarations
Which of the following statements correctly explains “lexical scope”?
A) Variables are defined based on where they are used
B) Variables are defined based on where they are written in the code
C) Variables are accessible only inside functions
D) Only global variables are accessible
What will happen if you try to access a variable defined with let outside its block?
A) It will log undefined
B) It will throw a reference error
C) It will return null
D) It will automatically assign a new value
What happens when you redeclare a variable with var in the same scope?
A) Error is thrown
B) Redeclaration is ignored
C) The variable is redefined
D) Variable is hoisted again
How can you create a global variable within a function?
A) Declare with const
B) Declare without var, let, or const
C) Declare with let
D) Use an arrow function
What happens if two variables in different scopes have the same name?
A) Both are accessible with no issues
B) The innermost variable takes precedence
C) The outer variable takes precedence
D) Code throws an error
Which statement is true for closures in JavaScript?
A) They cannot access variables from outer functions
B) They remember variables from their parent function’s scope
C) They don’t work with arrow functions
D) They prevent variable mutation
Which of the following best describes a pure function?
A) A function that always produces the same output for the same inputs
B) A function that changes global variables
C) A function that requires no parameters
D) A function with side effects
Answer Key
Qno
Answer
1
C) function
2
B) myFunction()
3
A) Hello, World!
4
B) my-function
5
C) Anywhere in the code
6
B) Inside parentheses ()
7
C) 8
8
C) To send a value back to the calling location
9
C) undefined
10
A) function myFunc(param1, param2)
11
A) let myFunc = function() {}
12
A) let sum = (a, b) => { return a + b; }
13
B) ES6
14
C) Returns the value
15
B) They do not have their own this context
16
B) Function scope
17
A) const
18
C) Global scope
19
C) No access possible
20
B) It cannot be reassigned
21
B) Function scope
22
B) Outside the block it was defined
23
C) Named function declarations
24
B) Variables are defined based on where they are written in the code
25
B) It will throw a reference error
26
C) The variable is redefined
27
B) Declare without var, let, or const
28
B) The innermost variable takes precedence
29
B) They remember variables from their parent function’s scope
30
A) A function that always produces the same output for the same inputs