在javascript中键入检查 [英] type checking in javascript

查看:163
本文介绍了在javascript中键入检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检查变量当前是否为整数类型?我已经为此寻找了某种资源,我认为===运算符很重要,但我不确定如何检查变量是否为整数(或者是一个数组)

How can I check if a variable is currently an integer type? I've looked for some sort of resource for this and I think the === operator is important, but I'm not sure how to check if a variable is an Integer (or an Array for that matter)

推荐答案

变量永远不会是JavaScript中的整数类型 - 它不区分不同类型的数字。

A variable will never be an integer type in JavaScript — it doesn't distinguish between different types of Number.

您可以测试变量是否包含数字,以及该数字是否为整数。

You can test if the variable contains a number, and if that number is an integer.

(typeof foo === "number") && Math.floor(foo) === foo

如果变量可能是包含整数的字符串,你想看看是否是这种情况:

If the variable might be a string containing an integer and you want to see if that is the case:

foo == parseInt(foo, 10)

这篇关于在javascript中键入检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆