What is a function?
A function is a block of code designed to perform a particular task, this task is executed when invoked:
const greetings = function(){
console.log('Hello User');
};
greetings();
What is a Method?
A method is quite similar to a function, however the way they are invoked and defined differ:
const welcome = `Welcome ${userName}`
let userName = 'Daniel';
let welcomeAlert = welcome.toUpperCase();
//* the line of code above uses a method called toUpperCase *//
console.log(welcomeAlert);
What is an argument?
Function parameters are the names listed in the function definition. Function arguments are the real values passed to the function:
const speak = function(name = 'Daniel', time = 'night'){
//* the parameters are the variables "name" and "time" *//
console.log(`Good ${time} ${name} ); }
speak('Alejandro', 'morning')
//* the code above is the argument that alters the function's result *//
How to return a value:
const calcArea = function(radius){
let area = 3.14 * radius**2;
return area;
}
console.log(area);
//* This function will log the results into the console *//
Arrow Functions
Arrow functions allow us to write shorter function syntax. If the function has only one statement, and the statement returns a value, you can remove the brackets and the return keyword. This only works if the functions has one statement.
const calcArea = (radius) = {
return 3.14 * radius**2;
}
//* This function is the same as the one above, it is more compact *//
What is the Foreach Method?
This method calls a function once for each element in an array, in order:
let group = ['Carolina', 'Daniel', 'Piper']
const logGroup= (group, index) => {
console.log(`${index} - hello ${group}`);
group.forEach(logGroup);
Callback Functions
A callback is a function passed as an argument to another function:
function myAlarm(){
console.log('Wake UP!')};
function warning(){
console.log('You've got 10 minutes left!')};
warning();
myAlarm();
Resources
www.w3schools.com
The Net Ninja's Udemy Course
Top comments (0)