JavaScript.The.Definitive.Guide—Core Javascript-basic Data Types

來源:互聯網
上載者:User
Table 3-1. Special numeric constants

Constant

Meaning

Infinity

Special value to represent infinity

NaN

Special not-a-number value

Number.MAX_VALUE

Largest representable number

Number.MIN_VALUE

Smallest (closest to zero) representable number

Number.NaN

Special not-a-number value

Number.POSITIVE_INFINITY

Special value to represent infinity

Number.NEGATIVE_INFINITY

Special value to represent negative infinity

Table 3-2. JavaScript escape sequences

Sequence

Character represented

\0

The NUL character (\u0000).

\b

Backspace (\u0008).

\t

Horizontal tab (\u0009).

\n

Newline (\u000A).

\v

Vertical tab (\u000B).

\f

Form feed (\u000C).

\r

Carriage return (\u000D).

\"

Double quote (\u0022).

\'

Apostrophe or single quote (\u0027).

\\

Backslash (\u005C).

\xXX

The Latin-1 character specified by the two hexadecimal digits XX.

\uXXXX

The Unicode character specified by the four hexadecimal digits XXXX.

\XXX

The Latin-1 character specified by the octal digits XXX, between 1 and 377. Not supported by ECMAScript v3; do not use this escape sequence.

Converting Numbers to Strings

一、var n_as_string = n + "";

二、var string_value = String(number);

三、string_value = number.toString( );

convert numbers in other bases

var n = 17;  

binary_string = n.toString(2); // Evaluates to "10001"

octal_string = "0" + n.toString(8); // Evaluates to "021"

hex_string = "0x" + n.toString(16); // Evaluates to "0x11"

 

Converting Strings to Numbers

var product = "21" * "2"; // product is the number 42.

var number = string_value - 0;

var number = Number(string_value);  

parseInt("3 blind mice"); // Returns 3  

parseInt("11", 2); // Returns 3 (1*2 + 1)

parseInt("zz", 36); // Returns 1295 (35*36 + 35 

parseFloat("3.14 meters"); // Returns 3.14

 

object

image.width  

image.height

image["width"]  

image["height"]

document.write("this is a test");

var point = new Object( );  

point.x = 2.3; point.y = -1.2;

var point = { x:2.3, y:-1.2 };

var rectangle = { upperLeft: { x: 2, y: 2 }, lowerRight: { x: 4, y: 4} };

var square = { "upperLeft": { x:point.x, y:point.y }, 'lowerRight': { x:(point.x + side), y:(point.y+side) }}; 

 

Array

var arr=new Array();

arr[0]="a";

arr[1]="b";  

 

 

Table 3-3. Automatic datatype conversions

Value

Context in which value is used

String

Number

Boolean

Object

Undefined value

"undefined"

NaN

false

Error

null

"null"

0

false

Error

Nonempty string

As is

Numeric value of string or NaN

TRue

String object

Empty string

As is

0

false

String object

0

"0"

As is

false

Number object

NaN

"NaN"

As is

false

Number object

Infinity

"Infinity"

As is

true

Number object

Negative infinity

"-Infinity"

As is

TRue

Number object

Any other number

String value of number

As is

true

Number object

true

"true"

1

As is

Boolean object

false

"false"

0

As is

Boolean object

Object

toString( )

valueOf( ), toString( ), or NaN

true

As is

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.