JavaScript numeric and string, special characters

A, JavaScript values

1, integer and floating point

According to the international standard IEEE 754,64-bit 64-bit binary floating-point format, the first bits 0 to 51 significant digits storage portion 52 to store exponent part of 62, bit 63 is the sign bit, n denotes 0 number 1 indicates negative.

(Photo: Sea code song)

 

1), inside the JavaScript, all numbers are stored as 64-bit floating-point number, true even integer. So, it is equal to 1 and 1.0, 1.0 and 1 plus or get an integer, they will deduce the type, not as a decimal become like JAVA.

2), as far as possible not to use decimal comparison decimal inaccurate

1 === 1.0 // true
1 + 1.0 // 2
0.1 + 0.2 === 0.3//false

2 is divided by an integer binary computes remainders 8421 yards i.e., decimal: the integer and fractional part separated by 2, fractional count method.

5:

5 * 2 = more than 1 1

1/2 = 0 I 1

1

-> write down: 101

0.3:

0.3*2=0.6

0.6*2=1.2

0.2*2=0.4

0.4*2=0.8

0.8*2=1.6

0.6*2=1.2….

2, notation

Basic: 1.2 scientific notation: E or E

123e3 // 123000
123e-3 // 0.123

3, hex

When using literal (literal), JavaScript provides four decimal integer representation: decimal, hexadecimal, octal, binary.

Decimal: no leading zero.

Octal: There are leading 0, and only use seven digits 0-7, 8-9 if the decimal is 8-9.

Hex: prefix 0x or 0X, use the ten Arabic numerals, and af and A-F12 English letters.

Binary: prefix 0b or 0B, used only two digits 0 and 1.

15

0xf // 15

017 // 15

0b1111 //15

09 // 9 -> Invalid octal

4, the special value

NaN: indicates "not a number" (Not a Number), mainly in the case of the character string parsed into a digital error.

1) NaN is not equal to any value, including itself, because it is not a value

2) NaN with any number (including its own) operations, are obtained NaN.

3) isNaN method can be used to determine whether a value is NaN.

. 1-ABC 
0/0 
NaN3 to false // === NaN3 
NaN3 +. 1 
isNaN (NaN3) to true // 
isNaN (123) to false // 

Infinity: infinity 
. 1 / -0 // -Infinity 
. 1 / Infinity // +0 

/ / isFinite method of determining whether the normal value. 
isFinite (Infinity) to false // 
isFinite (-1) to true // 
isFinite (to true) to true // 
isFinite (NaN3) to false //

  

5, using parseInt parseFloat Number conversion,

parseInt('123') // 123
parseInt('123px') //常用  123
parseFloat("3.14");    //3.14
Number(null) // 0

  

Second, the special character string

1, the string is a sequence of characters, a plurality of characters, using the '' or ''

'chrome'

"good"

"It's a long journey"

2, use the + character splicing

'chrome' +'is' +'very' +'good'

3, special characters, escape -> Common to remember

\ 0 represents the character with no content (\ u0000)

\ N newline character (\ u000A)

\ T tab (\ u0009)

\ 'Single quote (\ u0027)

\ "Double quote (\ u0022)

4, Base64 transcoding

Base64 is a coding method, any character can be converted into printable character. With this encoding method, primarily not for encryption, but does not appear to special characters, to simplify the processing procedures.

btoa (): Base64 string or binary value coded into

atob (): Base64 encoded into the original coding

String = var 'the Hello World!'; 

btoa (String) // "SGVsbG8gV29ybGQh" 
atob ( 'SGVsbG8gV29ybGQh') // "! the Hello World" 

// is not suitable for Chinese, encodeURIComponent Chinese must be encoded. 
b64Encode function (STR) { 
    return btoa (unescape (the encodeURIComponent (STR))); 
} 

function b64Decode (STR) { 
    return decodeURIComponent (Escape (atoB (STR))); 
} 

b64Encode ( 'Hello') // "5L2g5aW9" 
b64Decode ( '5L2g5aW9') // "Hello"

  

Shanghai Shangxue Tang java training finishing release, please pay attention to more public code number song Hey, thank you

Guess you like

Origin www.cnblogs.com/shsxt/p/11411744.html
Recommended