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

Tao Zhang
Tao Zhang
1,869 Points

Is assigning a variable to a function a form of calling a function?

The IsFieldEmpty function is not called explicitly or assigning fieldTest to this function part of the process?

" function isFieldEmpty () { const field = document.querySelector ('#info'); if (field.value === '') { return true; } else { return false; } }

const fieldTest = isFieldEmpty();

if (fieldTest === true) { alert('You need to provide info'); }"

1 Answer

Steven Parker
Steven Parker
232,176 Points

This isn't "assigning a variable to a function", it is assigning a variable with the value returned from a function.

But yes, the function gets called to get the value to assign to the variable.

Tao Zhang
Tao Zhang
1,869 Points

Thanks for the clarification. are there any other ways to call a function besides using "function ();" and assigning a variable to a function?

Steven Parker
Steven Parker
232,176 Points

You'll always use the function name followed by parentheses when calling a function. But there are many was to use one besides assigning a variable with it. For examples:

  • otherFunc(isFieldEmpty()) :point_left: passing the return value as an argument to another function
  • resetValues() :point_left: calling a function that does something but does not return any value
  • (function { console.log("I'm an IIFE!") })() :point_left: calling it at the same time it is defined