Skip to main content

Command Palette

Search for a command to run...

Why You Should Use Arrow Functions For Validation Functions

Published
3 min read
Why You Should Use Arrow Functions For Validation Functions
Y

Full Stack Developer | Freelancer | JavaScript | React | Nodejs.

Loves sharing knowledge through technical articles.

Dev.to: https://dev.to/myogeshchavan97

https://linktr.ee/myogeshchavan97

Arrow Functions are one of the greatest additions in ES6, whether it's regarding the this keyword or regarding the shorter syntax.

Today we will see, how using arrow functions makes our code much simpler and shorter while creating validation functions.

Suppose, you have written a function which returns true if the email is valid otherwise returns an error message as shown below:

const validateEmail = email => /^[^@ ]+@[^@ ]+\.[^@ \.]+$/.test(email);
const isValidEmail = function(value) {
    if (validateEmail(value)) {
        return true;
    } else {
        return "Please enter a valid email address";
    }
}

let isValid = isValidEmail('abc@gmail.com');
console.log(isValid); // true
isValid = isValidEmail('abc@@gmail.com');
console.log(isValid); // Please enter a valid email address

The same function can be written in just one line using ES6 arrow functions syntax as shown below:

const validateEmail = email => /^[^@ ]+@[^@ ]+\.[^@ \.]+$/.test(email);
const isValidEmail = value => validateEmail(value) || 'Please enter a valid email address.';

let isValid = isValidEmail('abc@gmail.com');
console.log(isValid); // true
isValid = isValidEmail('abc@@gmail.com');
console.log(isValid); // Please enter a valid email address

Suppose you have written a generic validation function that checks if the input field is empty or not as shown below:

const isNotEmpty = function(fieldName) {
  return function(fieldValue) {
    if (fieldValue.trim().length > 0) {
      return true;
    } else {
      return fieldName[0].toUpperCase() + fieldName.slice(1) + " is required.";
    }
  };
};

const fieldName = isNotEmpty('name');
let isValid = fieldName('David');
console.log(isValid); // true
isValid = fieldName('');
console.log(isValid); // Name is required

The same function can be written in just one line using ES6 arrow functions syntax as shown below:

const isNotEmpty = fieldName => fieldValue => fieldValue.trim().length > 0 || fieldName[0].toUpperCase() + fieldName.slice(1) + ' is required.';

const fieldName = isNotEmpty('name');
let isValid = fieldName('David');
console.log(isValid); // true
isValid = fieldName('');
console.log(isValid); // Name is required

These are just two examples of validation functions but as you can see, you can create all validation functions using this ES6 arrow function syntax which allows us to write code in a simpler and shorter way.

Thanks for reading!

Check out my recently published Mastering Redux course.

In this course, you will build 3 apps along with food ordering app and you'll learn:

  • Basic and advanced Redux
  • How to manage the complex state of array and objects
  • How to use multiple reducers to manage complex redux state
  • How to debug Redux application
  • How to use Redux in React using react-redux library to make your app reactive.
  • How to use redux-thunk library to handle async API calls and much more

and then finally we'll build a complete food ordering app from scratch with stripe integration for accepting payments and deploy it to the production.

Want to stay up to date with regular content regarding JavaScript, React, Node.js? Follow me on LinkedIn.

T

Yogesh Chavan, Great one sir.

1
Y

Thank you Tapas Adhikary Sir

More from this blog

Yogesh Chavan

125 posts

Full Stack Developer | JavaScript | React | Nodejs. Subscribe to my YouTube Channel: https://www.youtube.com/@codingmastery_dev