menu

JavaScript Functions


1. Which of the following is a way to create a closure in JavaScript?

Returning a function from another function

Using the "with" statement

Using the "debugger" statement

Using the "switch" statement


2. What is a callback function in JavaScript?

A function that is called when an event occurs

A function that is called at the end of a program's execution

A function that is passed as an argument to another function and is executed when the other function is finished

A function that is used to create objects


3.

What is the output of the following code snippet?

function myFunction(x) {
x = x + 1;
return x;
}
var y = 2;
myFunction(y);
console.log(y);

2

3

undefined

Error


4.

What is the output of the following code snippet?

function myFunction(x, y) {
console.log(x + y);
}
myFunction(2, 3, 4);

5

9

11

Error


5.

What is the output of the following code snippet?

function myFunction(x) {
return function(y) {
return x + y;
};
}
var addFive = myFunction(5);
var result = addFive(10);
console.log(result);

5

10

15

20


6. What is the purpose of the "new" keyword in JavaScript?

To create a new variable

To create a new object instance

To declare a new function

To import a module


7. What is the purpose of the "return" statement in a JavaScript function?

To specify a function's input parameters

To declare variables inside a function

To specify a function's name

To return a value from a function


8. What is a higher-order function in JavaScript?

A function that returns a boolean value

A function that takes another function as an argument or returns a function as a result

A function that performs a specific task

A function that is called when an event occurs


9. Which keyword is used to define a JavaScript function that can be called without creating an instance of the object?

static

prototype

class

function


10. What is the purpose of a function parameter?

To pass values into a function

To declare variables inside a function

To define a function's name

To specify a function's return value