Q
Which of these is the correct way to define and immediately invoke a function?

Answer & Solution

Answer: Option B
Solution:
An IIFE is defined and invoked as (function() { return 1; })();.
Related Questions on Average

What is the output of function sayHi() { return 'Hi'; } alert(sayHi());?

A). sayHi

B). Hi

C). undefined

D). alert

How do you define and invoke a function to calculate the square of a number in one step?

A). var square = function(n) { return n * n; }; square(5);

B). (function(n) { return n * n; })(5);

C). function square(n) { return n * n; } square(5);

D). var square = (n) { return n * n; }; square(5);

What does return without a value return?

A). 0

B). null

C). undefined

D). NaN

What will function greet() { console.log('Hi'); } var x = greet; x(); output?

A). Hi

B). undefined

C). x

D). greet

Which of the following correctly invokes a function with parameters?

A). function myFunc(a, b) { }; myFunc(a b);

B). function myFunc(a, b) { }; myFunc(a, b);

C). function myFunc(a b) { }; myFunc(a, b);

D). function myFunc(a, b) { }; myFunc[];

What happens if a function is invoked without parentheses in an event handler like onclick='myFunction'?

A). The function is executed immediately

B). The function reference is assigned

C). An error is thrown

D). The function does not work

What is the output of function test() { return 2 + 2; } console.log(test());?

A). 22

B). 2 + 2

C). 4

D). test

What is the output of function sum(a, b) { return a + b; } console.log(sum(3, 4));?

A). 34

B). 7

C). undefined

D). sum

How can you invoke a function named init only once when the script runs?

A). init;

B). (init)();

C). init();

D). function init() {}();

How can you invoke a function using the call method?

A). myFunction.call();

B). myFunction.call(this);

C). call.myFunction();

D). call(myFunction);