function-runtime

In JavaScript, a function is not a “magical language structure”, but a special kind of value.

The syntax that we used before is called a Function Declaration:

function sayHi() {
alert( “Hello” );
}

There is another syntax for creating a function that is called a Function Expression.

It looks like this:

let sayHi = function() {
alert( “Hello” );
};

Here, the function is created and assigned to the variable explicitly, like any other value. No matter how the function is defined, it’s just a value stored in the variable sayHi.

The meaning of these code samples is the same: “create a function and put it into the variable sayHi”.

We can even print out that value using alert:

function sayHi() {
alert( “Hello” );
}

alert( sayHi ); // shows the function code