js如何判断输入的数据是整数还是小数

js如何判断输入的数据是整数还是小数,第1张

可以转换为整数parseInt(number)和原来的数比较。

也可以转换成字符串判断。

var str = number+"";

if(strindexOf("")==-1){

alert("整数")

}else{

alert("小数");

}

扩展资料:

判断是否数字

function isNumber(str){

var n = Number(str);

if (!isNaN(n)){

return true;

}else{

return false;

}

}

判断是否对象

function isObj(obj){

if(typeof obj=="object"){

return true;

}else{

return false;

}

}

判断是否为空

function emptyFun(obj){

var obj=obj;

if(obj==""||obj==null||obj==undefined||obj=="null"||obj=="undefined"){

return true;

}else{

return false;

}

}

获取地址栏参数

function GetQueryString(name){

var reg = new RegExp("(^|&)"+ name +"=([^&])(&|$)");

var r = windowlocationsearchsubstr(1)match(reg);

if(r!=null)return  unescape(r[2]); return null;

}

1、在javascript中判断两个变量值是否相等,可使用等同 *** 作符或恒等 *** 作符;

2、等同 *** 作符,可简单判断两个变量值是否相等,变量类型不一致时,会做类型转换,即忽略变量的类型进行比较;

3、恒等 *** 作符,比较两个变量类型是否一致,不一致返回false,一致时,再次比较两个变量的值是否相等,即在同一变量类型下进行比较。

1 在js中,未定义过的变量都是undefined类型

2 判断一个变量的类型可以用typeof方法

3 如果a从未定义过,则typeof(a)的返回值是"undefined', 一定要注意这里的返回值是字符串

4 示例如下

如何判断js中的数据类型:typeof、instanceof、 constructor、 prototype方法比较<br><br>如何判断js中的类型呢,先举几个例子:<br><br>var a = "iamstring";<br><br>var b = 222;<br><br>var c= [1,2,3];<br><br>var d = new Date();<br><br>var e =<br>function(){alert(111);};<br><br>var f =<br>function(){thisname="22";};<br><br>最常见的判断方法:typeof<br><br>alert(typeof a)<br> ------------> string<br><br>alert(typeof b)<br> ------------> number<br><br>alert(typeof c)<br> ------------> object<br><br>alert(typeof d)<br> ------------> object<br><br>alert(typeof e)<br> ------------> function<br><br>alert(typeof f)<br> ------------> function<br><br>其中typeof返回的类型都是字符串形式,需注意,例如:<br><br>alert(typeof a == "string")<br>-------------> true<br><br>alert(typeof a == String)<br>---------------> false<br><br>另外typeof<br>可以判断function的类型;在判断除Object类型的对象时比较方便。<br><br>判断已知对象类型的方法: instanceof<br><br>alert(c instanceof Array)<br>---------------> true<br><br>alert(d instanceof<br>Date) <br><br>alert(f instanceof Function)<br>------------> true<br><br>alert(f instanceof function)<br>------------> false<br><br>注意:instanceof<br>后面一定要是对象类型,并且大小写不能错,该方法适合一些条件选择或分支。<br><br>根据对象的constructor判断:<br>constructor<br><br>alert(cconstructor ===<br>Array) ----------> true<br><br>alert(dconstructor === Date)<br>-----------> true<br><br>alert(econstructor ===<br>Function) -------> true<br><br>注意: constructor 在类继承时会出错<br><br>eg,<br><br>function A(){};<br><br>function B(){};<br><br>Aprototype = new B(); //A继承自B<br><br>var aObj = new A();<br><br>alert(aobjconstructor === B) -----------><br>true;<br><br>alert(aobjconstructor === A) -----------><br>false;<br><br>而instanceof方法不会出现该问题,对象直接继承和间接继承的都会报true:<br><br>alert(aobj instanceof B) ----------------><br>true;<br><br>alert(aobj instanceof B) ----------------><br>true;<br><br>言归正传,解决construtor的问题通常是让对象的constructor手动指向自己:<br><br>aobjconstructor = A;<br>//将自己的类赋值给对象的constructor属性<br><br>alert(aobjconstructor === A) -----------><br>true;<br><br>alert(aobjconstructor === B) -----------><br>false; //基类不会报true了;<br><br>通用但很繁琐的方法: prototype<br><br>alert(ObjectprototypetoStringcall(a) === ‘[object String]’)<br>-------> true;<br><br>alert(ObjectprototypetoStringcall(b) === ‘[object Number]’)<br>-------> true;<br><br>alert(ObjectprototypetoStringcall(c) === ‘[object Array]’)<br>-------> true;<br><br>alert(ObjectprototypetoStringcall(d) === ‘[object Date]’)<br>-------> true;<br><br>alert(ObjectprototypetoStringcall(e) === ‘[object Function]’)<br>-------> true;<br><br>alert(ObjectprototypetoStringcall(f) === ‘[object Function]’)<br>-------> true;<br><br>大小写不能写错,比较麻烦,但胜在通用。<br><br>通常情况下用typeof<br>判断就可以了,遇到预知Object类型的情况可以选用instanceof或constructor方法,简单总结下,挖个坑,欢迎补充!

JS判断字符串包含的方法具体如下:
1 例子:
var tempStr = "tempText" ;
var bool = tempStrindexOf("Texxt");
//返回大于等于0的整数值,若不包含"Text"则返回"-1。
if(bool>0){
documentwrite("包含字符串");
}else{
documentwrite("不包含字符串");
}
2 indexOf用法:
strObjindexOf(subString[, startIndex])
JavaScript中indexOf函数方法返回一个整数值,指出 String 对象内子字符串的开始位置。如果没有找到子字符串, 则返回 -1。如果 startindex 是负数,则 startindex 被当作零。如果它比最大的字符位置索引还大,则它被当作最大的可能索引。
参数:
strObj : 必选项,String 对象或文字。
subString :必选项,要在 String 对象中查找的子字符串。
starIndex :可选项,该整数值指出在 String 对象内开始查找的索引。如果省略,则从字符串的开始处查找;
如果 startindex 是负数,则 startindex 被当作零。如果它比最大的字符位置索引还大,则它被当作最大的可能索引。
3 与lastIndexOf的区别:
lastIndexOf() 方法则是从字符串的结尾开始检索子串。

最常见的判断方法:typeof
alert(typeof a) ------------> string
alert(typeof b) ------------> number
alert(typeof c) ------------> object
alert(typeof d) ------------> object
alert(typeof e) ------------> function
alert(typeof f) ------------> function
其中typeof返回的类型都是字符串形式,需注意,例如:
alert(typeof a == "string") -------------> true
alert(typeof a == String) ---------------> false
另外typeof 可以判断function的类型;在判断除Object类型的对象时比较方便。

判断已知对象类型的方法: instanceof
alert(c instanceof Array) ---------------> true
alert(d instanceof Date)
alert(f instanceof Function) ------------> true
alert(f instanceof function) ------------> false
注意:instanceof 后面一定要是对象类型,并且大小写不能错,该方法适合一些条件选择或分支。

根据对象的constructor判断: constructor
alert(cconstructor === Array) ----------> true
alert(dconstructor === Date) -----------> true
alert(econstructor === Function) -------> true
注意: constructor 在类继承时会出错
eg,
function A(){};
function B(){};
Aprototype = new B(); //A继承自B
var aObj = new A();
alert(aobjconstructor === B) -----------> true;
alert(aobjconstructor === A) -----------> false;
而instanceof方法不会出现该问题,对象直接继承和间接继承的都会报true:
alert(aobj instanceof B) ----------------> true;
alert(aobj instanceof B) ----------------> true;
言归正传,解决construtor的问题通常是让对象的constructor手动指向自己:
aobjconstructor = A; //将自己的类赋值给对象的constructor属性
alert(aobjconstructor === A) -----------> true;
alert(aobjconstructor === B) -----------> false; //基类不会报true了;

通用但很繁琐的方法: prototype
alert(ObjectprototypetoStringcall(a) === ‘[object String]’) -------> true;
alert(ObjectprototypetoStringcall(b) === ‘[object Number]’) -------> true;
alert(ObjectprototypetoStringcall(c) === ‘[object Array]’) -------> true;
alert(ObjectprototypetoStringcall(d) === ‘[object Date]’) -------> true;
alert(ObjectprototypetoStringcall(e) === ‘[object Function]’) -------> true;
alert(ObjectprototypetoStringcall(f) === ‘[object Function]’) -------> true;

检测简单的数据类型的方法

typeof方法用于检测简单的数据类型如typeof 12

instanceof的实例方法检测如[] instanceof Array // true

arrconstructor == Array判断arr的构造函数是否为数组,如果是则arr是数组

ArrayisArray([])判断是否是数组

精确判断数据类型ObjectprototypetoStringcall(arr)


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/13169465.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-06-15
下一篇 2023-06-15

发表评论

登录后才能评论

评论列表(0条)

保存