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 trialArvind Kevin Arjun Sharma
8,272 PointsHelp me please!!!!!!
Idk what to place there...
function warning() { alert('Warning, warning, warning!'); }
__________________;
and here...
function greeting( name ) { var message = "Hello " + name;
__________________;
??????
3 Answers
James Bradd
4,597 PointsSo I just retook this quiz again; first one is looking for you to call the function, second is looking for you to return a value. I'd review how to invoke a function and the basic structure of writing a function to return a value, I believe it's in the video prior to this quiz. Best of luck!
Kamal Nassouh
3,547 PointsTo call a function you use the function name and then parenthesis so that being said if i create a function:
function kamal() {
alert("hello hows it going");
}
How would I call that function ?
Kamal Nassouh
3,547 PointsBut as James suggested I would go back and do the course to get an understanding
Kevin Lozandier
Courses Plus Student 53,747 PointsHi, Arvind Arjun Sharma:
There's several ways to call a function in JavaScript, but the most common way—as some have pointed out—is to use a set of parenthesis ()
that is recognized by JavaScript parsers, engines tokenizers to be an intent to run a function (doing their parts accordingly).
If I have a function identified with the name foo
(or associated with a variable identifier of foo
) either via
// Function declaration
function foo() {
console.log("I'm to be called:");
}
or
let foo = function(){
console.log("I'm to be called:");
};
In order for the statements inside the function to be ran (in this case just console.log("I'm to be called")
, I can run merely foo()
.
If defined parameters for declared for a function such as the name
variable in the second example you provided, a value can be provided as an argument when you attempt to invoke the function like greetings("Kevin")
.
Other ways to invoke a function: call
& apply
If you're curious, the other ways of invoking or calling a function is to call
or apply
. The first argument of either enables you to change what the value of this
is.
After that the behavior of both change in very important ways. apply
allows you to pass an array of parameters, while call
enables you to add an infinite amount of arguments that are handled for you.
When using apply
, greetings
can be called greetings.apply(null, ['Kevin'])
; when using call
, greetings
can be called like greetings.call(null, 'Kevin')
. null
is used to make this
inside the function's body (where the code specifically for the function is written) be null
for demonstrative purposes.
These alternate ways of calling a function are especially useful when you have functions that accepts other functions as arguments towards calling it or returns other functions. These sort of functions are usually called high-order functions.
They're also useful when you want to call functions just-in-time with very specific, non-obvious values for this
and the other arguments the function accepts.
I hope this helps!
Barry Barnes
18,631 PointsI pity the foo.