Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Loops Working with 'for' Loops The Refactor Challenge – Duplicate Code

Function Expression : how can we assign a function to a constant?

It looks like the constant variable is assigned a function and its value changes every time the function is called. Aren't constants supposed to have a fixed value?

1 Answer

Functions don't change and therefore they can be declared as constants. They behave the same way every time they are called. The only change is the values passed to the function as parameters.

Three ways to declare functions (all valid):

function add(num1, num2) {
  return num1 + num2;
}

const add = function(num1, num2) {
  return num1 + num2;
}

const add = (num1, num2) => {
  return num1 + num2;
}

The third version uses the ES6 arrow function shorthand syntax.

More info: https://dmitripavlutin.com/6-ways-to-declare-javascript-functions/

I hope that helps (and is not TMI/overwhelming). Happy coding!