This article mainly introduces the types, values, and variable Summary of JavaScript. You can refer to the following: JavaScript data types are divided into two types: primitive type and object type. Five primitive types: Numbers, strings, Boolean values, null (null), undefined (undefined ). An object is a set of attributes. Each attribute is composed of a "name/value pair" (the value can be the original value or object. Three special objects: Global Objects, arrays, and functions. The core JavaScript language also defines three useful classes: Date, RegExp, and Error.
1 digit
The integer and floating point values are not distinguished in JavaScript. JavaScript can identify the direct amount of a decimal INTEGER (the so-called direct amount is the data value directly used in the Program), and hexadecimal values (prefixed with 0x or 0X, that is the number 0, not the letter o. Think about it. If the letter o is used, the hexadecimal value cannot be an identifier ). Although the ECMAScript standard does not support the direct quantity of Octal numbers, some implementations of JavaScript can use the octal representation of Integers (prefixed with numbers 0 ), I use octal to assign values to a variable in IE, Chrome, and FF browsers on my computer. However, in the strict mode of ECMAScript6, the direct quantity of octal nodes is explicitly prohibited.
There are two writing methods for the floating point type direct volume. ① Traditional real number Writing: it consists of an integer, a decimal point, and a decimal point. ② exponential counting: that is, the real number is followed by the letter e or E, followed by the plus or minus sign, and then an integer index.
1.1 Arithmetic overflow
In JavaScript, no error is reported when arithmetic operations overflow, underflow, or are divisible by 0.
Overflow: When the calculation result exceeds the upper limit of the number expressed by JavaScript, the result is positive Infinity or negative Infinity-Infinity. The positive infinity behavior is also in line with the reality: Based on their addition, subtraction, multiplication, and Division operations, the result is still an infinite value (of course keep their plus and minus signs); underflow: this happens when the computation result is infinitely close to zero and smaller than the minimum value expressed by JavaScript. In this case, 0 is returned. When a negative number overflows, a special value "negative zero" is returned ". Negative zero and integer zero are basically equal (or even strictly equal = can be used for testing), except for the Division:
Var zero = 0; // positive zero value var negz =-0; // negative zero value zero === negz // return value of expression true1/zero === 1/negz // return value of expression false, it is equivalent to determining whether positive infinity and negative infinity are strictly equal.
After Division by 0, positive infinity or negative infinity values are returned. However, dividing 0 by 0 will return NaN (the value of the NaN attribute of the Number object predefined by JavaScript ). There are four cases for returning NaN: ① 0 divided by 0 ② infinity divided by infinity ③ open-side operation for any negative number ④ the arithmetic operator is used together with an operand that is not a number or cannot be converted to a number.
The NaN value is special: it is not equal to any value, including itself. Two methods to determine whether the variable x is NaN: ① use the function isNaN () ② use x! = X indicates that the expression is true only when x is NaN. There is also a function like isFinite () in JavaScript, which returns true if the parameter is not NaN, Infinity, or-Infinity.
1.2 binary floating point number and rounding error
There are countless real numbers, but JavaScript can only represent a limited number in the form of floating point numbers. That is to say, when using a real number in JavaScript, it is often just an approximate representation of a real value. JavaScript uses the IEEE-754 floating point representation, which is a binary representation that accurately represents scores such as 1/2, 1/8, and 1/1024, however, decimal scores of 1/10 and 1/10 cannot be accurately expressed. For example:
var x = 0.3 -0.2; //x=0.09999999999999998var y = 0.2 - 0.1; // y=0.1x == y //falsex == 0.1 //falsey == 0.1 //true0.1 == 0.1 //truevar z = x + y; //z=0.19999999999999998
2 Text
2.1 string and Character Set
A string is an unchangeable sequence of 16-bit values. Each character is generally from the Unicode Character Set. The length of a string is the number of 16 bits. JavaScript represents text by string type. Note: JavaScript does not indicate the "character type" of a single character ". To represent a 16-bit value, you only need to assign it to the string variable.
JavaScript uses the Unicode Character Set encoded by a UTF-16. a JavaScript string is a sequence of unsigned 16-bit values. Unicode characters that cannot be expressed as 16 bits follow the UTF-16 encoding rules-a sequence of two 16 bits (or a "proxy pair. This means that a JavaScript string with a length of 2 may represent a Unicode character. Note: The operation methods of various types of strings defined by JavaScript apply to 16-bit values, instead of characters, and do not separately process the proxy item. The book is here, combined with NLP.
The delimiters of a string can be single or double quotation marks. These two forms of delimiters can be nested, but they cannot be nested in multiple layers (for example, double quotes can contain single quotes, and single quotes cannot contain double quotes ). As mentioned in the previous article, a string value can be split into several rows. Each row must end with a backslash (\). At this time, both the backslash and the row Terminator are not strings, that is, the string itself is not a multi-row, but is written in the form of multiple rows.
Note: ① In JavaScript, strings are fixed (unless re-assigned), and methods similar to replace () and toUpperCase () return new strings. The original strings are not changed; ② A string can be used as a read-only array. Besides using the charAt () method to query a single character, you can also use square brackets to access a single character (16-bit value) in the string. For example:
s = "hello, world"; s[0] //=>"h"
2.2 escape characters
Escape Character meaning
\ O NUL character (u0000)
\ B Return character (\ u0008)
\ T horizontal tab (\ u0009)
\ N linefeed (\ u000A)
\ V vertical tab (\ u000B)
\ F page feed (\ u000C)
\ R carriage return (\ u000D)
\ "Double quotation marks (\ u0022)
\ 'Marker or single quotation mark (\ u0027)
\ Backslash (\ u005C)
\ XXX is a string of Latin-1 characters specified by two hexadecimal numbers XX.
\ UXXXX Unicode characters specified by 4-digit hexadecimal number XXXX
Note: If the "\" character is before the character not listed in the table, ignore "\". For example, "\ #" is equivalent. Don't forget that the backslash also serves to use the backslash at the end of each line in a multi-line string.
3 Boolean Value
Values in JavaScript can be converted to boolean values. The values of null, undefined, 0,-0, NaN, and "(null String) are converted to false, false and these six values are sometimes called "false values". All other values, including objects (arrays), are converted to "true". true and these values are called "true ". Note: The Boolean value contains the toString () method, so you can use this method to convert the string to "true" or "false", but it does not contain other useful methods.
The above is all the content of this article. I hope you will like it.