JavaScript custom empty() function similar to empty() in PHP

The most common error that arises in the programming world is the usage of “empty” variables in the code logic.

In PHP , an empty variable is considered if any of the following conditions are met :

  • it’s null
  • it’s an empty string ( “” or ” ” or ”      ” )
  • it’s a Boolean false value
  • it’s an array or an object with no keys or values

PHP provides a very simple way to check for an empty value by using empty() function.  However , JavaScript does not provides any such implementation .

Following is the custom empty() function written in JavaScript that helps checking of “empty” variables and sanitation of code.

The above function works very similar to that of PHP empty() function and is extremely handy for checking variable existence and validation before using them straightway in the JavaScript code.

Happy Hacking ! 🙂

Hello world!

Welcome to WordPress. This is your first post. Edit or delete it, then start writing!

© 2016 Arpit Gaur

Theme by Anders NorenUp ↑