First-Class Functions

Assign Function to a Variable

const sayBye = () => {
return 'Goodbye my friend';
};
console.log(sayBye());
// "Goodbye my friend"

Function Passed as an Argument

const sayByeToFriend = (greeter, friend) => {
return greeter() + ' ' + friend;
};
console.log(sayByeToFriend(sayBye, 'Steve'));
// Goodbye my friend Steve

Returning a Function From Another Function

const goodbyeMaker = greeting => {
return friend => {
return greeting + ' ' + friend;
};
};
const sayByeToFriend = greeterMaker('Bye');
const sayAdiosToFriend = greeterMaker('Howdy');
console.log(sayHelloToPerson('Jane'));
// "Goodbye my friend Jane"
console.log(sayAdiosTof=Friend('Sam'));
// "Adios mi amigo Same"

Object Validation

const isUsernameLong = obj => {
return obj.username.length >= 8;
};
const arePasswordSame= obj => {
return obj.password === obj.confirmPassword;
};
//Function to check valid or not
const objectIsValid = (obj, ...funcs) => {
for (let i = 0; i < funcs.length; i++) {
if (funcs[i](obj) === false) {
return false;
}
}
return true;
};
const obj1 = {
username: 'ahmed123',
password: 'welcome',
confirmPassword: 'welcome',
};
const obj1Valid = objectIsValid(obj1, usernameLongEnough, passwordsMatch);
console.log(obj1Valid);
// true
const obj2 = {
username: 'habibi',
password: 'hello',
confirmPassword: 'hello',
};
const obj2Valid = objectIsValid(obj2, usernameLongEnough, passwordsMatch);
console.log(obj2Valid);
// false

Conclusion

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Build Super Fast Apps in Node.js using Redis Cache

NodeJS Tutorial — 01 — Creating your first server + Nodemon

5+2 Phenomenal FREE Platforms To Learn Coding

People photo created by wayhomestudio — www.freepik.com

YouTube Video | Indian State Details Search app

Difference between Array / Set/ Tuples

Email Notifications With Firebase Cloud Functions

How to use React Hook Form

How to create a Video Consultation Healthcare App in React Native Using Twilio!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ahmed A.

Ahmed A.

More from Medium

Looping objects: object keys, values, and entries

Technologic capacity creating catastrophic risk

Java, in case you hadn’t heard

Improving Go Service Reliability with Static Analysis

Charts showing counts of Go and Node services that are in compliance