Function
function() {}
function greatWorls() {
console.log("Hello world");
}
greatWorls();
function getReminder() {
console.log("Water the palnet");
}
getReminder();
function getInSpanish() {
console.log("Buenas Tardes");
}
getInSpanish();
Parameters and Arguments
calculateArea(20, 10);
function calculateArea(width, height) {
console.log(width * height);
}
sayThanks("Ashraf");
sayThanks("Siddik");
sayThanks("Jahangir");
sayThanks("Alam");
function sayThanks(name) {
console.log("Thank you for your parchase " + name + "! We appreciate your business." );
}
Default Parameters
function animal(name = "Tiger") {
console.log(`Animal is ${name}`);
}
animal("Cow"); // Outpur: Animal is Tiger
animal(); // Output: Animal is Tiger
makeShoppingList();
function makeShoppingList(item1="Milk", item2="Bread", item3="Eggs") {
console.log(`List: ${item1}, ${item2}, ${item3}`)
}
makeShoppingList("Rice", "Alo", "Potato");
function makeShoppingList(item1="Milk", item2="Bread", item3="Eggs") {
console.log(`List: ${item1}, ${item2}, ${item3}`)
}
Return
without return
function rectangleArea(width, hight) {
let area = width * hight
}
console.log(rectangleArea(5, 7)) // Prints: Undefined
with return
function rectangleArea(width, hight) {
let area = width * hight;
return area;
console.log("hello");
}
console.log(rectangleArea(5, 7));
const numOfMonitors = monitorCount(5, 4);
console.log(numOfMonitors);
function monitorCount(rows, columns) {
return rows * columns;
}
Helper Function
Function Expressions
const plantNeedsWater = function(day) {
if(day === 'wednesday') {
return true;
} else {
return false
}
}
console.log(plantNeedsWater("Thuesday"));
console.log(plantNeedsWater("wednesday"));
Arrow Expressions
const plantNeedsWater = (day) => {
if(day === 'wednesday') {
return true;
} else {
return false
}
}
console.log(plantNeedsWater("Thuesday"));
console.log(plantNeedsWater("wednesday"));
Concise Body Arrow Funciton
const plantNeedsWater = (day) => day === 'wednesday'
console.log(plantNeedsWater("Thuesday"));
console.log(plantNeedsWater("wednesday"));