05 November 2011

Javascript function for checking a variables type or empty/unset variables - similar to PHP's empty()

I've been building on this one all week and I'm finding it more and more useful for quickly writing variable checks at the top of functions. You can use it to easily check if a var is defined or check if it is of a certain type. It's a work in progress but it's very handy. Check out the commented examples to get an idea of potential uses. Ideas for improvements/additions are welcome!

Please see note before copying my code>>

//CHECK TYPE OF PASSED OBJ:
function is(obj,type){
 switch(type){
  case 'array': case 'ar': //ARRAY:
   return(String(obj.constructor).indexOf('Array')>-1)
  break;
  case 'integer': case 'int': case 'number': //NUMBER:
   return((parseFloat(obj) == parseInt(obj)) && !isNaN(obj))
  break;
  default: //IS EMPTY:
   if(obj==null || obj==undefined || obj==' ' || obj==0 || obj==false || obj=="false" || obj=='' || obj=="\n") return(false);
   return(true)
 }
}
/*EXAMPLES:
returns true if myvar is a positive value (string, number, array, DOM element, etc):
if(is(myvar)){..continue with function...}

returns true if myvar is an array:
if(is(myvar,'array')){..continue with function...}

returns true if myvar is an integer or can be converted to an integer:
if(is(myvar,'int')){..continue with function...}
*/


//CHECKS IF ANY OF THE PASSED ARG(S) ARE BLANK OR FALSE OR 0 OR UNDEFINED:
function isempty(){
 for (var i = 0; i < arguments.length; i++) {
  if(!is(arguments[i])){return(true)}
 }  
 return(false)
}
/*EXAMPLE:
if(isempty(requiredVar1,requiredVar2,requiredVar3)){
       return(false);
}else{...continue with function...}
*/ 

9 comments:

Meiqing Xu said...

kd 9 shoes
fitflops
ralph lauren pas cher
ugg boots
longchamp outlet
red bottoms
louboutin uk
rolex watches uk
adidas yeezy
coach factory outlet
20161228caiyan

chenlili said...

coach outlet
michael kors outlet
nfl jerseys wholesale
christian louboutin outlet
coach outlet online
canada goose outlet
canada goose outlet store
canada goose outlet
christian louboutin sale
pandora outlet
shenyuhang20180608

anggita nirbaya said...

By taking the time to read a lot of information like this to add my insight . cara menggugurkan kandungan

Juliana Kho said...

Thank you for your information, please visit:
http://www.sbobetlb.net/
http://www.cbetandroid.org/
http://www.daftarcbet.com/
http://www.cbetbola.net/
http://www.sbobetblue.com/
http://www.bluebet333.com/
http://www.bluebet388.com/
http://www.daftar-sakong.com/

Juliana Kho said...

http://www.judi-sakong.com/
http://www.sbobetlb.com/
http://www.play1628games.com/
http://www.play-168.com/

Juliana Kho said...

cara daftar sabung ayam s128
bluebet66
cbet judi online
m.scr88.com
login osg777

daftar joker123 slot online

Unknown said...

By reading the article material very very well and this is very useful.
menggugurkan kandungan
penyebab sering keguguran
cara mengatasi telat haid
tanda tanda kehamilan 1 bulan
kalkulator masa subur akurat dan berhasil
masa subur wanita sebelum haid

Sophie Grace said...

This site is excellent and so is how the subject matter was explained. I also like some of the comments too. Waiting for the next post. Welcome go to my site 192.168.l.l

Klinik Raden Saleh said...

now we are here to help you to overcome your obstetric problems.

Biaya Aborsi

Post a Comment

Cron Design Studio: Dublin based web design & software development