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 trialRichard Otabil
Courses Plus Student 603 PointsI have a question . is it possible to call one parameter of a function and use it? am just curious .
i am learning about javascript functions .
I learnt that i can add parameters to a functions .
so eg. if i have to display items from a database (i am using a phonegap framework to build a mobile app) , and i want to call some items from a function like this -
function channeldetails (name , image, category , description ) {
}
is there a way i can call each one of them if i have a block of code in the function?
2 Answers
Jaco Burger
20,807 PointsYes it is, you're talking about a default parameter. But I think it's better to have a fallback so to speak, so inside your function you can have something like
... { if (description === undefined) { description = 0; } }
Somebody correct me if I'm wrong :)
Robert Richey
Courses Plus Student 16,352 PointsHi Richard,
I'm unclear on your question, so I'll do my best to explain function parameters. They are variables that are local to the function and are initialized when the function is called by passing in arguments.
var width = 3;
var height = 4;
// functions are defined with parameters
function areaRectangle(a, b) {
return a * b;
}
// functions are called with arguments
var area1 = areaRectangle(width, height); // 12
var area2 = areaRectangle(5, 7); // 35
// if you need a function with default parameters, consider the following
function areaCircle(p1) {
var radius = p1 || 1;
return Math.PI * radius * radius;
}
var area3 = areaCircle(); // 3.14...
var area4 = areaCircle(2); // 12.56...
Please let me know if this helps or not.
Cheers