Function = a section of reusable code.

  • declare the code once, use it whenever you want
  • call the function to execute the code

Basic example(with parameter):

function happyBirthday(username, age){ //the parameter
	console.log("happy birthday to you!");
	console.log("happy birthday to you!");
	console.log(`happy birthday to ${username}!`);
	console.log("happy birthday to you!");
	console.log(`you are ${age} years old`);
}
 
happyBirthday("BroCode", 25); //the parameter will pass to function
happyBirthday("Spongebob", 30);
happyBirthday("Patrick", 37);

return example:

function add(x, y){
	let result = x + y; 
	return result; // it can return a variable
}
 
function subtract(x, y){
	return x - y; // or it can directly return a number
}
 
function multiply(x, y){
	return x * y;
}
 
function divide(x, y){
	return x / y;
}
 
function isEven(number){
	return number % 2 === 0 ? true : false;
}
 
function isValidEmail(email){
	if(email.includes("@")){
		return true;
	}
	else{
		return false;
	}
}