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