SoFunction
Updated on 2025-03-01

Javascript Simple and efficient judgment of data types Series functions By

illustrate:
Some time ago, when I mastered ASP VBScript almost the same, I turned to learning Javascript/Jscript, mainly learning Jscript.
However, there is basically no difference between the two. The only difference is that Jscript does not have the concept of a client.

At the beginning, I found that there are many practical functions of VBS, Js, formatNumber, isArray, isDate, etc.
There is also a very strange date object. You cannot add or subtract directly. You need to set***...

However, when you master Javascript/Jscript to a certain level, you will find that he is N times stronger than VBS. The stronger part is that he is free in grammar. A certain function that VBS does not have, can fully realize the same function by just creating a prototype or building a judgment function in Js. Another thing that is more obvious is that the rules are available everywhere.

Oh, there is a lot of nonsense, let’s take the call.

Table of contents:
1. Determine whether it is an array type
2. Determine whether it is a string type
3. Determine whether it is a numerical type
4. Determine whether it is a date type
5. Determine whether it is a function
6. Determine whether it is an object


2006-11-13
 /btbtd

1. Determine whether it is an array type

linenum 
<script type="text/javascript"> 
//<![CDATA[ 
    var a=[0]; 
        (isArray(a),'<br/>'); 
    function isArray(obj){ 
        return (typeof obj=='object')&&==Array; 
    } 
//]]> 
</script> 


2. Determine whether it is a string type

linenum 
<script type="text/javascript"> 
//<![CDATA[ 
    (isString('test'),'<br/>'); 
    (isString(10),'<br/>'); 
    function isString(str){ 
        return (typeof str=='string')&&==String; 
    } 
//]]> 
</script> 


3. Determine whether it is a numerical type

linenum 
<script type="text/javascript"> 
//<![CDATA[ 
    (isNumber('test'),'<br/>'); 
    (isNumber(10),'<br/>'); 
    function isNumber(obj){ 
        return (typeof obj=='number')&&==Number; 
    } 
//]]> 
</script> 


4. Determine whether it is a date type

linenum 
<script type="text/javascript"> 
//<![CDATA[ 
    (isDate(new Date()),'<br/>'); 
    (isDate(10),'<br/>'); 
    function isDate(obj){ 
        return (typeof obj=='object')&&==Date; 
    } 
//]]> 
</script> 


5. Determine whether it is a function

linenum 
<script type="text/javascript"> 
//<![CDATA[ 
    (isFunction(function test(){}),'<br/>'); 
    (isFunction(10),'<br/>'); 
    function isFunction(obj){ 
        return (typeof obj=='function')&&==Function; 
    } 
//]]> 
</script> 


6. Determine whether it is an object
<script type="text/javascript">

linenum 
//<![CDATA[ 
    (isObject(new Object()),'<br/>'); 
    (isObject(10),'<br/>'); 
    function isObject(obj){ 
        return (typeof obj=='object')&&==Object; 
    } 
//]]> 
</script>