Skip to content

Try our new Crash Courses!

Buy one of our new Crash Courses, now hosted on Teachable.

Function expression – JavaScript

Length: 10 minutes

Summary

let myFunction = function() {

}

myFunction();

Details

Function expressions are another way of writing expressions. Instead of just writing the function and giving it a name like a function declaration, you save the function to a variable or constant.

To write a function expression, first write a variable or constant. The name you use will be what you use to call the function.

let myFunction

Next, write an equals sign, the word function, a pair of parentheses, and a pair of curly braces.

let myFunction = function() {

}

Calling the function is the same as calling a function declaration: you write the name of the function along with a pair of parentheses. The difference here is you have to call the function after the function expression (in other words, function expressions are not hoisted).

let myFunction = function() {

}

// This call can't be placed above the function expression
myFunction();

You can also have function expressions with one or more parameters.

let myFunction = function(param1, param2) {

}

myFunction(arg1, arg2);

Demo

Exercises

Try the following statements in the console:

let myFunction = function() {
  console.log('This is a function expression');
}

myFunction();

References

function expression on MDN

Back to: JavaScript Reference > JS Functions