Js functions are a section of reusable code. Declare it once and use it wherever you want. Call the function to execute that code.
function happyBirthday(){
console.log("Happy birthday to you!");
console.log("Happy birthday to you!");
console.log(`Happy birthday dear ${username}!`);
console.log("Happy birthday to you!");
console.log(`You are ${age} years old!`);
}
function checkValidEmail(email)
{
return email.includes("@") ;
}
happyBirthday();
console.log(checkValidEmail("fakeemail@fakesite.com"));
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
https://www.w3schools.com/js/js_functions.asp
1. Assigned to a variable (Function Expression)
You can store an anonymous function in a variable, which then acts as the function's identifier for calling it later.
const greet = function() {
console.log("Hello!");
};
greet(); // Output: Hello!
let x = 10;
let y = 20;
let z = (x,y)=> {return x + y;}
console.log(z(2,3));
2. As an argument (Callback Function)
They are commonly passed as arguments to higher-order functions like
setTimeout(), map(), filter(), or event handlers.
No comments:
Post a Comment