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 Basics (Retired) Creating Reusable Code with Functions Passing an Argument to a Function

brayant benitez
brayant benitez
7,885 Points

function vs var

Does the "function: keyword work like the "Var" in naming the function? is it defined or undefined? for example:

function returnValue(hello) { return hello; }

var echo = returnValue('greeting');

is returnValue defined?

1 Answer

Ioannis Leontiadis
Ioannis Leontiadis
9,828 Points

Hello Brayant,

when assigning a value to a variable, the expression on the left is evaluated and then assigned.

So in,

var echo = returnValue('greeting');

the function returnValue() is called, it returns the string "greeting" and then the string is assigned to echo just fine.

When using,

function returnValue(hello){
        return hello;
}       

you follow the syntax of,

function NameOfTheFunction(Parameters){
        /*Function Code*/
        return Value;
}       

which declares a function which can be called by,

NameOfTheFunction(Arguments);

You can also declare a function like this,

var myFunction = function(Parameters) {
        /*Function Code*/
        return Value;
}       

which first creates an anonymous function and then assigns it to myFunction variable. You can then call it like this,

myFunction(Arguments);

As you see these things are quite different. The main difference is that in the first case you have a named function and in the second case you have an unnamed one stored in a variable which is used for reference.

Hope that helped!