Js determines the data type whether it is an array whether it is a string and so on

  • 2020-03-30 01:21:36
  • OfStack

1 determines whether it is an array type
2 determines whether it is a string type
3 determine whether it is a numeric type
Determine whether it is a date type
5 is a function
Determine whether it is an object

1 determines whether it is an array type
 
<script type="text/javascript"> 
//<![CDATA[ 
var a=[0]; 
document.write(isArray(a),'<br/>'); 
function isArray(obj){ 
return (typeof obj=='object')&&obj.constructor==Array; 
} 
//]]> 
</script> 

2 determines whether it is a string type
 
<script type="text/javascript"> 
//<![CDATA[ 
document.write(isString('test'),'<br/>'); 
document.write(isString(10),'<br/>'); 
function isString(str){ 
return (typeof str=='string')&&str.constructor==String; 
} 
//]]> 
</script> 

3 determine whether it is a numeric type
 
<script type="text/javascript"> 
//<![CDATA[ 
document.write(isNumber('test'),'<br/>'); 
document.write(isNumber(10),'<br/>'); 
function isNumber(obj){ 
return (typeof obj=='number')&&obj.constructor==Number; 
} 
//]]> 
</script> 

Determine whether it is a date type
 
<script type="text/javascript"> 
//<![CDATA[ 
document.write(isDate(new Date()),'<br/>'); 
document.write(isDate(10),'<br/>'); 
function isDate(obj){ 
return (typeof obj=='object')&&obj.constructor==Date; 
} 
//]]> 
</script> 

5 is a function
 
<script type="text/javascript"> 
//<![CDATA[ 
document.write(isFunction(function test(){}),'<br/>'); 
document.write(isFunction(10),'<br/>'); 
function isFunction(obj){ 
return (typeof obj=='function')&&obj.constructor==Function; 
} 
//]]> 
</script> 

Determine whether it is an object
 
<script type="text/javascript"> 
//<![CDATA[ 
document.write(isObject(new Object()),'<br/>'); 
document.write(isObject(10),'<br/>'); 
function isObject(obj){ 
return (typeof obj=='object')&&obj.constructor==Object; 
} 
//]]> 
</script> 

Related articles: