Go Back   CodingForums.com > :: Client side development > JavaScript programming > Post a JavaScript

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-25-2012, 09:25 PM   PM User | #1
Darkranger85
New Coder

 
Join Date: Mar 2012
Posts: 14
Thanks: 1
Thanked 0 Times in 0 Posts
Darkranger85 is an unknown quantity at this point
Empty function script

(I did indeed read the rules of this forum and while I'm not a "seasoned" javascript coder I thought that this script was simple enough to not matter. If that is unacceptable I apologize and you can feel free to delete this post)

Here is a function that allows you to use a PHP like 'empty()' function. It helped me a lot because it combines several other JS functions into one neat package.

Code:
function empty(obj) {
    if (typeof obj == 'undefined' || obj === null || obj === '') return true;
    if (typeof obj == 'number' && isNaN(obj)) return true;
    if (obj instanceof Date && isNaN(Number(obj))) return true;
    return false;
}
Hope this helps people as much as it helps me!
Darkranger85 is offline   Reply With Quote
Old 03-26-2012, 02:22 AM   PM User | #2
Nile
Regular Coder

 
Nile's Avatar
 
Join Date: Jun 2008
Posts: 280
Thanks: 2
Thanked 46 Times in 46 Posts
Nile is an unknown quantity at this point
You can be less repetitive like this:
Code:
function empty(obj){
   return (typeof obj == 'undefined' || obj === null || obj === '') || (typeof obj == 'number' && isNaN(obj)) || (obj instanceof Date && isNaN(Number(obj)));
}
Nile is offline   Reply With Quote
Old 03-26-2012, 02:08 PM   PM User | #3
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,554
Thanks: 9
Thanked 480 Times in 463 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
Quote:
Originally Posted by Nile View Post
You can be less repetitive like this:
Code:
function empty(obj){
   return (typeof obj == 'undefined' || obj === null || obj === '') || (typeof obj == 'number' && isNaN(obj)) || (obj instanceof Date && isNaN(Number(obj)));
}
you can be less repetitive and properly handle arrays and objects like this :
Code:
function empty(obj){
   var u;
   if((''+obj)==="[object Object]"){for(var i in obj);return !i;}
   return  !+obj && (!{'0':1,false:1}[obj] );
}
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:15.2% IE7:0.5% IE8:8.4% IE9:8.5% IE10:8.5%
rnd me is offline   Reply With Quote
Old 03-26-2012, 07:39 PM   PM User | #4
Alex Vincent
Moderator


 
Join Date: May 2002
Location: Hayward, CA
Posts: 1,428
Thanks: 1
Thanked 19 Times in 17 Posts
Alex Vincent is on a distinguished road
I actually prefer Nile's version here, because it's more readable.
__________________
"The first step to confirming there is a bug in someone else's work is confirming there are no bugs in your own."
June 30, 2001
author, Verbosio prototype XML Editor
author, JavaScript Developer's Dictionary
https://alexvincent.us/blog
Alex Vincent is online now   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:28 AM.


Advertisement
Log in to turn off these ads.