javascript判断数据类型
function isInt(val){if((typeof(val)=='number')||(val instanceof Number)){ if(/^\d*$/.test(val)){ return true }else{ return false }}else{ return false}}
浮点数:
function isFloat(val){if((typeof(val)=='number')||(val instanceof Number)){ if(/^\d*\.\d*$/.test(val)){ return true }else{ return false }}else{ return false}}
字符串:
function isString(val){if((typeof(val)=='string')||(val instanceof String)){ return true}else{ return false}}
布尔值:
function isBoolean(val){if((typeof(val)=='boolean')||(val instanceof Boolean)){ return true}else{ return false}}
数组:
function isArray(val){if(val instanceof Array){ return true}else{ return false}}
对象:
function isObject(val){if(val instanceof Object && typeof val.length == 'undefined'){ return true}else{ return false}}
测试:
a. 代码:
function isInt(val){if((typeof(val)=='number')||(val instanceof Number)){ if(/^\d*$/.test(val)){ return true }else{ return false }}else{ return false}}function isFloat(val){if((typeof(val)=='number')||(val instanceof Number)){ if(/^\d*\.\d*$/.test(val)){ return true }else{ return false }}else{ return false}}function isString(val){if((typeof(val)=='string')||(val instanceof String)){ return true}else{ return false}}function isBoolean(val){if((typeof(val)=='boolean')||(val instanceof Boolean)){ return true}else{ return false}}function isArray(val){if(val instanceof Array){ return true}else{ return false}}function isObject(val){if(val instanceof Object && typeof val.length == 'undefined'){ return true}else{ return false}}var a = new Number(1)var b = new Number(1.1)var c = new String('a')var d = new Boolean(true)var e = new Array(1,2,3)var f = new Object({name:'lee'})var aa = 1var bb = 1.1var cc = 'f'var dd = falsevar ee = [1,2,3]var ff = {name:'lee'}console.log(isInt(a))console.log(isFloat(b))console.log(isString(c))console.log(isBoolean(d))console.log(isArray(e))console.log(isObject(f))console.log(isInt(aa))console.log(isFloat(bb))console.log(isString(cc))console.log(isBoolean(dd))console.log(isArray(ee))console.log(isObject(ff))
b. 输出:
truetruetruetruetruetruetruetruetruetrue
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。