menu

JavaScript Functions


1. What is the purpose of the "this" keyword in a JavaScript function?

To refer to the function itself

To refer to the global object

To refer to the object that the function is a method of

To refer to the previous function in the call stack


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 difference between a named function and an anonymous function in JavaScript?

Named functions can be called from anywhere in the code, while anonymous functions cannot be called

Anonymous functions are defined without a name, while named functions have a name

Named functions are hoisted, while anonymous functions are not hoisted

Anonymous functions can be assigned to variables, while named functions cannot be assigned to variables


4. What is the difference between "call" and "apply" methods in JavaScript?

There is no difference between "call" and "apply"

Call method accepts an array of arguments, while "apply" method accepts individual arguments

Call method accepts individual arguments, while "apply" method accepts an array of arguments

Call method is used for asynchronous functions, while "apply" method is used for synchronous functions


5. What is a spread operator in JavaScript?

An operator that combines two or more arrays into a single array

An operator that assigns a new value to a variable

An operator that creates a new object with a specified prototype object and properties

An operator that allows an iterable object to be expanded into individual elements


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

static

prototype

class

function


7. What is the purpose of the "arguments" object in a JavaScript function?

To specify a function's input parameters

To store a function's local variables

To create a new array of arguments passed to a function

To access all arguments passed to a function as an array-like object


8. Which of the following is not a JavaScript function declaration method?

Function constructor

Function expression

Function literal

Function prototype


9. What is the syntax to declare a JavaScript function?

function myFunction() {}

let myFunction = function() {}

myFunction = function() {}

let myFunction() {}


10.

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