JavaScript中是否存在整数类型? [英] Is there or isn't there an integer type in JavaScript?

查看:503
本文介绍了JavaScript中是否存在整数类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始学习Javascript,我立即对Mozilla的一个看似矛盾的陈述感到困惑重新介绍JavaScript(JS教程)

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中没有整数这样的东西,所以如果你使用的话,你必须要小心b $ b以C
或Java计算数学。

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

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

On the other hand (immediately after that paragraph):


在实践中,整数值被视为32位整数(并且在某些浏览器实现中以这种方式存储
),这可以是
对于按位操作很重要。

"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."


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

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

那么,JavaScript中存在整数还是不存在?

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

推荐答案

只有 Number 数据类型。

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

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

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

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

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

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_INTEGER and Number.MAX_SAFE_INTEGER correspondingly.

这篇关于JavaScript中是否存在整数类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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