Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Start a free Courses trial
to watch this video
Testing for Number Arguments
Resources
- isNaN() – MDN
- 3 Ways to Check if Variable is a Number in JavaScript
- The throw statement – MDN
- Error objects – MDN
- First-class Function – MDN
Functions as First-class Citizens
Functions are often referred to as "first-class citizens" in JavaScript, which means that you have the ability to do almost anything with functions.
You can store a function in a variable:
const multiply = function(a, b) {
return a * b;
};
Pass a function as an argument to other functions:
function sayYay() {
return 'Yay';
}
/*
- The argument passed for 'func' should be a reference to a function
- That function gets called in console.log
*/
function greeting(func, name) {
console.log( `${func()}, ${name}!` );
}
/*
- A reference to the sayYay function gets passed to the greeting function as an argument
- sayYay returns the string 'Yay', so the greeting function logs 'Yay, Lee!'
*/
greeting(sayYay, 'Lee'); // Yay, Lee!
You can even set a function to return another function:
function sayYay() {
return function() {
console.log('Yay!');
}
}
This lets you take advantage of powerful design patterns you'll learn about in later courses.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up