Self Invoking Functions

I want to set variable 'testValue' to 3 using a Self Invoking Function, can you help me?

var testValue;
function test() { testValue = 3; }();

What is the result of executing the previous code?

SyntaxError: Unexpected token )
__match_answer_and_solution__

What is the value of variable 'testValue'?

undefined
__match_answer_and_solution__

Why?

The value of testValue is undefined because the function has not been autoexecuted.
__match_answer_and_solution__

Write the code to execute this function adding only one more character to the sentence.

var testValue;
function test() { testValue = 3; }();
var testValue;
!function test() { testValue = 3; }();
assert(testValue == 3);

Last updated

Was this helpful?