LEARNING JAVASCRIPT - Trang 63

1

This may change in the future: dedicated integer types are an oft-discussed language feature.

JavaScript is an unusual programming language in that it only has this one numeric

data type.

1

Most languages have multiple integer types and two or more floating-point

types. On one hand, this choice simplifies JavaScript, especially for beginners. On the

other hand, it reduces JavaScript’s suitability for certain applications that require the

performance of integer arithmetic, or the precision of fixed-precision numbers.
JavaScript recognizes four types of numeric literal: decimal, binary, octal, and hexa‐

decimal. With decimal literals, you can express integers (no decimal), decimal num‐

bers, and numbers in base-10 exponential notation (an abbreviation of scientific

notation). In addition, there are special values for infinity, negative infinity, and “not

a number” (these are not technically numeric literals, but they do result in numeric

values, so I am including them here):

let

count

=

10

;

// integer literal; count is still a double

const

blue

=

0x0000ff

;

// hexadecimal (hex ff = decimal 255)

const

umask

=

0o0022

;

// octal (octal 22 = decimal 18)

const

roomTemp

=

21.5

;

// decimal

const

c

=

3.0e6

;

// exponential (3.0 × 10^6 = 3,000,000)

const

e

=

-

1.6

e

-

19

;

// exponential (-1.6 × 10^-19 = 0.00000000000000000016)

const

inf

=

Infinity

;

const

ninf

=

-

Infinity

;

const

nan

=

NaN

;

// "not a number"

No matter what literal format you use (decimal, hexadecimal, expo‐

nential, etc.), the number that gets created is stored in the same for‐

mat: a double. The various literal formats simply allow you to

specify a number in whatever format is convenient. JavaScript has

limited support for displaying numbers in different formats, which

we’ll discuss in

Chapter 16

.

The mathematicians in the crowd might be calling foul: infinity is not a number!

Indeed, it isn’t; but of course, neither is

NaN

. These are not numbers you do computa‐

tion with; rather, they are available as placeholders.
In addition, there are some useful properties of the corresponding

Number

object that

represent important numeric values:

const

small

=

Number

.

EPSILON

;

// the smallest value that can be

// added to 1 to get a distinct number

// larger than 1, approx. 2.2e-16

const

bigInt

=

Number

.

MAX_SAFE_INTEGER

;

// the largest representable integer

const

max

=

Number

.

MAX_VALUE

;

// the largest representable number

const

minInt

=

Number

.

MIN_SAFE_INTEGER

;

// the smallest representable integer

const

min

=

Number

.

MIN_VALUE

;

// the smallest representable number

const

nInf

=

Number

.

NEGATIVE_INFINITY

;

// the same as -Infinity

Numbers | 39

Liên Kết Chia Sẽ

** Đây là liên kết chia sẻ bới cộng đồng người dùng, chúng tôi không chịu trách nhiệm gì về nội dung của các thông tin này. Nếu có liên kết nào không phù hợp xin hãy báo cho admin.