Declaring Function and Variables and giving it a name


#1

I am confused with the definitions used in the Intro to Function JS. It seems like the word function and variable are used interchangeably.

In the Section 3. Function syntax there is this example:
var sayHello = function(name) {
console.log('Hello ' + name);
};
It says: "we declare a function using var, and then give it a name sayHello."
So to me, this means: Var declares a function. sayHello is the name of the function.

But then in Section 4. in this example:
var functionName = function( ) {
// code code code
// code code code
// (more lines of code)
};
It says:"The var keyword declares a variable named functionName."
So, to me, this says: Var declares a variable. functionName is the name of the variable.

Am I getting something messed up here?? please help.


3/13 Function Syntax
#2

So, to me, this says: Var declares a variable. functionName is the name of the variable.

That's pretty much it. The syntax for declaring a variable is:

var name;

and you give this variable a type by assigning a value which is called definition:

var name = value;

if you now have a look at your function syntax:

var functionName = function( ) {
    // code code code
    // code code code
    // (more lines of code)
};

you can see that this is exactly what your doing. You create a variable functionName and then you store a rather interesting value (function) inside of it. So as from now on this function is accessible by the variable name these terms might be used interchangeably.


#3

I am still a bit confused. Are you saying 'function' and 'variable' are used interchangeably?

In this example:
var sayHello = function(name) {
the tutorial says: "we declare a function using var, and then give it a name sayHello."

var = keyword to declare a variable
sayHello = variable name
function(name) = value of the variable

I don't get how we declare a function using var, var is to declare a variable?!?!


#4

What exercise does this correspond to?
I mean it's pretty clear that functions and variables are 2 different things but that these anonymous functions, which are used here, are tightly bond to their variable and therefore might be addressed in a similar way, right?
But I give you the point this line might really be worded differently:

Nevertheless this is correct:

var = keyword to declare a variable
sayHello = variable name
function(name) = value of the variable

in case you're still in doubt.


#5

I am having trouble with syntax.

In this example:
var sayHello = function (name){
console.log ("Hello " + name};
};

sayHello("Emily");

Does the keyword 'var' declare the function or does the keyword 'var' declare a variable?
And 'sayHello' is the name of the function?


#6

The variable references to a function(it has no name) you can also name the function within the variable. sayHello is not the name of the function just the variable that references it.


#7

Thanks so much for replying. Yeah, I get the point about them being anonymous functions. What is confusing me is the word "declare".

The exercise that these examples came from (I also put it below):
3. Function Syntax : First we declare a function using var, and then give it a name sayHello
var sayHello = function(name) {
console.log('Hello ' + name);
};

**Here it seems to say var = keyword to declare a function but the next example it says that var = keyword that declares a variable.

  1. How Does A Function Work: The var keyword declares a variable named functionName.
    var functionName = function( ) {
    // code code code
    // code code code
    // (more lines of code)
    };

** functionName is a variable name not a function name?


#8

I've googled a bit and there are two ways to write functions:

function(){...};

which is called function expression (and works with an anonymous) function. And

function name(){...}

where name is really the function name and which is called function declaration. http://www.ecma-international.org/ecma-262/5.1/Ecma-262.pdf page 110.

For the first the function expression is a value that is either directly called (more used in the JQuery track than in vanilla JS) or stored into a variable. So what is declared here is the variable not the function and the name is the name of the variable not the name of the function. In the second one a function is declared and name is really the name of the function. Nevertheless in the end you have, with both ways, a function that can be called by name(). So as the name of the variable is the name by which you address the function, it's somehow the name of the function and as you declared the variable that holds the function you somehow declared the function, handwavingly. You need to keep in mind that this is an introduction to programming and not an accurate CS course. Not saying that it should not get it's terms straight just that this doesn't always go into great detail.

Last but not least you can also do a mixture of both:

var name = function name(){...};

where name is both the name of the function and the name of the variable and of course the names can differ.