JavaScript 中是否有整数类型?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33773296/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 16:53:01  来源:igfitidea点击:

Is there or isn't there an integer type in JavaScript?

javascriptinteger

提问by Jay Souper

I am just starting to learn Javascript and I immediately got confused by seemingly contradictory statements in Mozilla's A re-introduction to JavaScript (JS tutorial).

我刚开始学习 Javascript,我立即被 Mozilla 的A re-introduction to JavaScript (JS tutorial) 中看似矛盾的陈述弄糊涂了。

One one hand:

一只手:

"There's no such thing as an integerin JavaScript, so you have to be a little careful with your arithmetic if you're used to math in C or Java."

“ JavaScript 中没有整数这样的东西,所以如果你习惯于用 C 或 Java 进行数学运算,你必须小心你的算术。”

On the other hand (immediately after that paragraph):

另一方面(紧接在该段之后):

"In practice, integer values are treated as 32-bit ints (and are stored that way in some browser implementations), which can be important for bit-wise operations."

“实际上,整数值被视为 32 位整数(并在某些浏览器实现中以这种方式存储),这对于按位运算很重要。”

and

"You can convert a string to an integer using the built-in parseInt() function."

“您可以使用内置的 parseInt() 函数将字符串转换为整数。”

So, is there such thing as integer in JavaScript or isn't there?

那么,JavaScript 中是否有整数之类的东西?

采纳答案by zerkms

There is only the Numberdata type in JS that represents numbers.

JS 中只有Number数据类型代表数字。

Internally it is implemented as IEEE 754double precision floating point number.

它在内部实现为IEEE 754双精度浮点数。

What it means is that - technically there is no dedicated data type that represents integer numbers.

这意味着 - 从技术上讲,没有表示整数的专用数据类型。

Practically it means that we can safely use only numbers that are safely representable by the aforementioned standard. And it includes integer values in the range: [-9007199254740991; 9007199254740991]. Both values are defined as constants: Number.MIN_SAFE_INTEGERand Number.MAX_SAFE_INTEGERcorrespondingly.

实际上,这意味着我们只能安全地使用上述标准可以安全表示的数字。它包括范围内的整数值:[-9007199254740991; 9007199254740991]。两个值都被定义为常量:Number.MIN_SAFE_INTEGER并且Number.MAX_SAFE_INTEGER相应地。

回答by Mathias Lykkegaard Lorenzen

I should mention that there is actually a type called BigIntwhich represents a true integer.

我应该提一下,实际上有一种称为BigInt真正整数的类型。

However, because it can't be used with Numberand is generally only a good idea to use for larger numbers, I wouldn't advise it.

但是,因为它不能Number用于较大的数字并且通常只是一个好主意,所以我不建议它。

I thought it was worth a mention though.

不过我认为值得一提。

回答by sabbir

I believe questioner has already found his/her answer. But for others like me, you can check number is integeror not in JavaScript by using Number.isInteger()method. MDN

我相信提问者已经找到了他/她的答案。但是对于像我这样的其他人,您可以使用Number.isInteger()方法在 JavaScript 中检查数字是否为整数MDN

回答by Aiman Al-Eryani

Ah yes, this can be quite confusing. In Javascript, the type of a variable is implicitly defined.

啊,是的,这可能很令人困惑。在 Javascript 中,变量的类型是隐式定义的。

the function parseIntwill simply not take the decimal point into account and the type of the result would be an int.

该函数parseInt不会将小数点考虑在内,结果的类型将为 int。