Q
How do you assign a function to a variable in JavaScript?

Answer & Solution

Answer: Option A
Solution:
The correct syntax to assign a function to a variable is var greet = function() { alert('Hello'); };.
Related Questions on Average

How can you invoke a function stored in a variable doSomething?

A). doSomething();

B). call doSomething;

C). doSomething[];

D). invoke(doSomething);

What is the output of the following code? var person = { name: 'Alice', greet: function() { return 'Hello, ' + this.name; } }; console.log(person.greet());

A). Hello, Alice

B). Hello, undefined

C). Hello, name

D). undefined

How can you pass a function as an argument to another function?

A). performAction(function() { alert('Action!'); });

B). performAction(function { alert('Action!'); });

C). performAction(function() alert('Action!'););

D). performAction(function() alert('Action!'))

What will be the output of the following code? var add = new Function('a', 'b', 'return a + b'); console.log(add(2, 3));

A). 5

B). add

C). undefined

D). 2 3

What will be the output of the following code? var double = function(x) { return x * 2; }; console.log(double(5));

A). 10

B). 5

C). undefined

D). double

What will be the output of the following code? var counter = (function() { var count = 0; return function() { return ++count; }; })(); console.log(counter()); console.log(counter());

A). 1, 2

B). 0, 1

C). undefined, undefined

D). 2, 4

How do you define a function using a function expression?

A). var myFunc = function() { alert('Hello'); };

B). function myFunc() { alert('Hello'); }

C). var myFunc = function { alert('Hello'); };

D). var myFunc() { alert('Hello'); }

Which of the following is an example of an arrow function assigned to a variable?

A). var add = (a, b) => { return a + b; };

B). var add = (a, b) => return a + b;

C). var add = (a, b) => { a + b };

D). var add = (a, b) { return a + b; };

What will be the output of the following code? var multiply = (x, y) => x * y; console.log(multiply(3, 4));

A). 12

B). 34

C). multiply

D). undefined

Which of the following is true about functions in JavaScript?

A). Functions can be assigned to variables

B). Functions can be passed as arguments to other functions

C). Functions can be returned from other functions

D). All of the above