Functions: Basics Flashcards
1
Q
What is the basic function syntax (no args)
A
function greet() { console.log("Hey there"); }
2
Q
What is the basic function syntax with arguments?
A
function greet(title, name) { console.log(`Hey there ${title} ${name}!`); }
3
Q
When should you use a return statement?
A
Whenever you want to reuse or store the result of a function (which is most of the time!)
You can write your return statement plus code on the same line.
4
Q
What is a function expression?
AKA an anonymous function.
A
When you assign a function to a const variable - you do not need to declare a function name.
You can then use this const with arguments in the same way you would use a function.
Eg:
// Function Statement function add(x, y) { return x + y; }
// Function Expression (Anonymous) const sum = function (x, y) { return x + y; }
5
Q
What is hoisting?
A
Hoisting allows you to access to variables and function declarations before they are defined.
This is considered bad practice however.