本文翻译自:How to check if a variable is an integer in JavaScript?

How do I check if a variable is an integer in JavaScript, and throw an alert if it isn't? 如何检查JavaScript中的变量是否为整数,如果不是,则引发警报? I tried this, but it doesn't work: 我试过了,但是不起作用:

<html><head><script type="text/javascript">var data = 22;alert(NaN(data));</script></head>
</html>

#1楼

参考:https://stackoom.com/question/zPdA/如何在JavaScript中检查变量是否为整数


#2楼

Check if the variable is equal to that same variable rounded to an integer, like this: 检查变量是否等于舍入为整数的相同变量,如下所示:

if(Math.round(data) != data) {alert("Variable is not an integer!");
}

#3楼

First off, NaN is a "number" (yes I know it's weird, just roll with it), and not a "function". 首先,NaN是一个“数字”(是的,我知道这很奇怪,随它滚动),而不是一个“函数”。

You need to check both if the type of the variable is a number, and to check for integer I would use modulus. 您需要检查变量的类型是否为数字,并且要检查整数我将使用模数。

alert(typeof data === 'number' && data%1 == 0);

#4楼

You could check if the number has a remainder: 您可以检查数字是否有余数:

var data = 22;if(data % 1 === 0){// yes it's an integer.
}

Mind you, if your input could also be text and you want to check first it is not, then you can check the type first: 请注意,如果您输入的内容也可以是文本,并且您要首先检查它是否不是文本,那么可以先检查类型:

var data = 22;if(typeof data === 'number'){// yes it is numericif(data % 1 === 0){// yes it's an integer.}
}

#5楼

Use the === operator ( strict equality ) as below, 使用===运算符( 严格等于 ),如下所示,

if (data === parseInt(data, 10))alert("data is integer")
elsealert("data is not an integer")

#6楼

Assuming you don't know anything about the variable in question, you should take this approach: 假设您对所讨论的变量一无所知,则应采用以下方法:

if(typeof data === 'number') {var remainder = (data % 1);if(remainder === 0) {// yes, it is an integer}else if(isNaN(remainder)) {// no, data is either: NaN, Infinity, or -Infinity}else {// no, it is a float (still a number though)}
}
else {// no way, it is not even a number
}

To put it simply: 简而言之:

if(typeof data==='number' && (data%1)===0) {// data is an integer
}

如何在JavaScript中检查变量是否为整数?相关推荐

  1. 如何在 JavaScript 中检查数字是正数还是负数

    如何在 JavaScript 中检查数字是正数还是负数 前几天一个月薪35k的兄弟,给我推了一个人工智能学习网站,看了一段时间挺有意思的.包括语音识别.机器翻译等从基础到实战都有,很详细,分享给大家. ...

  2. regexp 好汉字符串_如何在JavaScript中使用RegExp确认字符串的结尾

    regexp 好汉字符串 by Catherine Vassant (aka Codingk8) 由凯瑟琳·瓦森(Catherine Vassant)(又名Codingk8) 如何在JavaScrip ...

  3. !! javascript_产量! 产量! 生成器如何在JavaScript中工作。

    !! javascript by Ashay Mandwarya ?️?? 由Ashay Mandwarya提供吗? 产量! 产量! 生成器如何在JavaScript中工作. (Yield! Yiel ...

  4. html类型转换函数,如何在JavaScript中转换数据类型?

    在JavaScript中,数据类型用于对一种特定类型的数据进行分类,确定可以分配给类型的值以及可以对其执行的操作.虽然由于类型强制,JavaScript会自动转换许多值,但为了达到预期的结果,通常最好 ...

  5. 如何在JavaScript中使用apply(?),call(?)和bind(➰)方法

    by Ashay Mandwarya ?️?? 由Ashay Mandwarya提供吗? 如何在JavaScript中使用apply(?),call(?)和bind(➰)方法 (How to use ...

  6. 如何在 JavaScript 中实现拖放

    来源:http://www.javaeye.com/post/152142 译者说明: 终于完成了全文的翻译,由于时间比较参促,文章没有过多的校正与润色,阅读过程中难免会有些许生硬或不准确的感觉,请大 ...

  7. 如何在JavaScript中声明名称空间?

    如何在JavaScript中创建名称空间,以使我的对象和函数不会被其他同名对象和函数覆盖? 我使用了以下内容: if (Foo == null || typeof(Foo) != "obje ...

  8. 如何在 JavaScript 中获取当前日期?

    问: 想要改进这篇文章?提供这个问题的详细答案,包括引文和解释为什么你的答案是正确的.没有足够细节的答案可能会被编辑或删除. 如何在 JavaScript 中获取当前日期? 答1: HuntsBot周 ...

  9. java+script+当前日期_如何在JavaScript中获取当前日期?

    如何在JavaScript中获取当前日期? #1楼 您可以使用扩展了 Date对象的Date.js库,从而可以使用.today()方法. #2楼 如果您想对日期格式进行更多的粒度控制,我强烈建议您查看 ...

最新文章

  1. 三方博弈matlab_小星星的读研日记之电商动态博弈知多少?
  2. jquery 使用animate来改变高度自动添加样式overflow:hidden的问题
  3. 利用ArcGIS Engine、VS .NET和Windows控件开发GIS应用
  4. Vb.net/VB 声明API功能父窗口功能
  5. 【华为云技术分享】如何设计高质量软件-领域驱动设计DDD(Domain-Driven Design)学习心得
  6. 几点减几点怎么列算式_洁净室平面设计怎么做?洁净室施工做到这几点就对了...
  7. python读取压缩文件的指定后缀的文件_python打包压缩、读取指定目录下的指定类型文件...
  8. MySQL查询语句格式总结
  9. 简述23种软件设计模式
  10. 离线发布bolg----使用Windows live writer客户端来发布CSDN的博客文章
  11. 极客大学产品经理训练营 产品思维和产品意识(上) 第3课总结
  12. 操作系统原理基础知识
  13. 亲戚B (普通并查集)
  14. margin外边距合并问题以及解决方式
  15. 大端与小端字节数据详解
  16. 第八届蓝桥杯JavaB组省赛真题
  17. Java--集合--经典七道综合练习题
  18. 基于单片机太阳能自动双轴追光电路仿真系统(毕设课设)
  19. excel控件只为简单写入数据表--github找到ExcelUtil笔记
  20. 愿世界永无 bug!再也不想熬夜加班了!

热门文章

  1. PHP安全编程:register_globals的安全性
  2. 休闲娱乐游戏-贱鸟跳跳
  3. 第一章,安装 composer
  4. IBM AIX创建lv
  5. 模板与泛型编程(二)
  6. Linq中字段数据类型转换问题(Linq to entity,LINQ to Entities 不识别方法System.String ToString()问题解决)...
  7. 利用React/anu编写一个弹出层
  8. 2017年4月14日
  9. WPF RichTextBox 控件常用方法和属性
  10. python-appium手机自动化测试(仅需安装包)前期准备(pydev-eclipse编辑器)