MusclesGlassez
MusclesGlassez

Reputation: 319

Trouble calling a function

This is my first time posting after looking through for a while and I am new to javascript, but I am having trouble figuring this out.

I am trying to call a function I have created, and everytime I attempt to call it...i am told I am not calling the function.

What am I missing?

//Simple greeting function
var greeting = function (name) {
console.log("Great to see you," + " " + name);
};

//Calling function
var greeting = function(name) {
console.log("Hello" + "" + name);
} // this was missing in the original question

or if I try

functionName = function(name)

I'll get a syntax error

Upvotes: 1

Views: 1218

Answers (5)

ktm5124
ktm5124

Reputation: 12123

What?! Try this:

var greeting = function(name) {
   console.log('Hello ' + name);
}

greeting('ktm');

So there are two ways to define functions.

(1) Function Expression

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

(2) Function Declaration

function greeting(name) { ... }

There is a simple way to call functions.

greeting('hello');

You can actually pass it any number of arguments, but if the function expects an argument, and you pass it none, it will treat it as undefined.

Upvotes: 1

klewis
klewis

Reputation: 8340

or you can do...

/*here i am declaring my function*/
function greeting(name) {
console.log(name);
}

/*here i am calling it*/
greeting('ktm');

function greeting(){} is a function declaration. Function declarations are probably easier to pick up and learn before you go diving-in, into storing functions into variables. Especially if you're learning this stuff for the first time.

Upvotes: 0

Kilizo
Kilizo

Reputation: 3382

Here is how to define your function:

function greeting(name) {

    console.log("Great to see you," + " " + name);

}

and call it:

greeting("Tomas");

Upvotes: 0

Petah
Petah

Reputation: 46040

Try:

//Simple greeting function
var greeting = function (name) {
    console.log("Great to see you," + " " + name);
};

greeting('foo bar');

Upvotes: 0

Michael Geary
Michael Geary

Reputation: 28850

You're not calling your function at all.

A call to your function would look like this:

greeting( "Mike" );

Upvotes: 1

Related Questions