JavaScript types, values, and variables summary _javascript tips

Source: Internet
Author: User
Tags arithmetic arithmetic operators

The data types of JavaScript are divided into two categories: original type and object type. 5 Original types: Number, String, Boolean, null (empty), undefined (undefined). An object is a collection of attributes, each of which is made up of "name/value pairs" (values can be original values, or can be objects). Three more special objects: global objects, arrays, functions. The JavaScript language core also defines three useful classes: the date class, the regular (RegExp) class, the error (the error) class.

1 numbers

JavaScript does not distinguish between integer values and floating-point values. JavaScript can recognize the decimal integer direct (the so-called direct amount, the data value that is used directly in the program), and the 16 binary value (prefixed by 0x or 0X, which is the number 0 is not the letter O.) Think that if it is the letter O, the hexadecimal value is not an identifier. Although the ECMAScript standard does not support direct octal, some implementations of JavaScript can use octal notation for integers (prefixed by number 0), and the author uses octal to assign values to a variable in three browsers, ie, Chrome, and FF on his computer. However, in the strict mode of ECMASCRIPT6, the octal direct volume is expressly prohibited.

There are two ways of writing floating-point direct quantity. ① traditional Real-number writing: There are integer parts, decimal and fractional components; ② exponential notation: that is, after the real number followed by the letter E or E, followed by the positive sign, followed by an integer index.

1.1 Overflow of arithmetic operations

Arithmetic operations in JavaScript do not error when an overflow (overflow), underflow (underflow) or 0 is divisible.

Overflow: When the result of the operation exceeds the upper limit of the number that JavaScript can represent, the result is positive infinity Infinity or negative infinity-infinity. The infinite worthy behavior characteristic also conforms to the reality: based on their addition, subtraction, multiplication and addition, the results are still infinitely large (with their positive and negative numbers); Underflow: Occurs when the result of the operation is infinitely close to 0 and is smaller than the minimum value that JavaScript can represent. In this case, 0 will be returned. Returns a special value of "negative 0" when a negative number occurs underflow. Minus 0 and whole 0 are basically equal (and can even be tested with strictly equal = = =), except as a divisor:

var zero = 0;  Positive zero value
var negz =-0;  Minus 0 value
zero = = Negz  //Expression return value is true
1/zero = = 1/negz  
//Expression return value false, equivalent to judge whether positive infinity and negative infinity are strictly equal

Divisible by 0 returns either positive infinity or negative infinity value. But 0 divided by 0 returns Nan (the value of the Nan attribute of the JavaScript predefined object number). There are four cases in which Nan is returned: ①0 divided by 0② infinity divided by Infinity ③ for arbitrary negative numbers ④ arithmetic operators are used with operands that are not numbers or cannot be converted to numbers.

The Nan value is a little special: it is not equal to any value, including itself. Two ways to determine whether a variable x is Nan: ① uses the function isNaN () ② to use x!= x to determine if and only if X is Nan, the expression results are true. There is also a similar function isfinite () in JavaScript that returns True when the parameter is not Nan, infinity, or-infinity.

1.2 Binary floating-point numbers and rounding errors

There are countless real numbers, but JavaScript can only represent a limited number of them in the form of floating-point numbers. That is, when a real number is used in JavaScript, it is often just an approximate representation of a real value. JavaScript uses the IEEE-754 floating-point notation, which is a binary notation that can accurately represent fractions such as 1/2, 1/8, and 1/1024, but decimal points 1/10 and 1/10 cannot be accurately represented. For example:

var x = 0.3-0.2;  x=0.09999999999999998
var y = 0.2-0.1;  y=0.1
x = = y       //false
x = = 0.1      //false
y = = 0.1      //true 0.1
= 0.1     //true
var z = x + y;   z=0.19999999999999998

2 text

2.1 String, Character set

A string is a set of immutable ordered sequences consisting of 16-bit values, each of which typically comes from a Unicode character set. The length of the string is the number of 16 bits it contains. JavaScript represents text by string type. Note: There is no "character" in JavaScript that represents a single character. To represent a 16-bit value, simply assign it to a string variable.

JavaScript takes the UTF-16 encoded Unicode character set, and the JavaScript string is a sequence of unsigned 16-bit values. Unicode characters that cannot be represented as 16 bits follow the UTF-16 encoding rule--a sequence (or "surrogate pair") that consists of two 16-bit values. This means that a JavaScript string of length 2 may represent a Unicode character. Note: JavaScript-defined string manipulation methods work on 16-bit values, not characters, and do not deal with surrogate pairs individually. The book is seen here, and combined with the http://www.alloyteam.com/2013/12/js-calculate-the-number-of-bytes-occupied-by-a-string/described above, Finally the Unicode character set, UTF-8, UTF-16 slightly understand.

The delimiter of a string can be either single or double quotes. These two forms of delimiters can be nested, but not nested (for example, double quotes can contain single quotes, and double quotes cannot be included in single quotes). As mentioned in the previous article, a string value can be split into several lines, each line must end with a backslash (\), where the backslash and line terminator are not strings, that is, the string itself is not a multiline, but is written in multiple lines.

Note: The strings in JavaScript are fixed (unless ①), and methods like replace () and toUpperCase () return the new string, and the original string itself does not change; the ② string can be used as a reading group, In addition to querying a single character using the Charat () method, you can also use square brackets to access a single character (16-bit value) in a string, for example:

s = "Hello, world";
S[0]//=> "h"

2.2 Escape characters

Escape character meaning
\o nul character (\u0000)
\b Backspace (\u0008)
\ t Horizontal tab (\U0009)
\ n line feed (\u000a)
\v vertical tab (\U000B)
\f Page Feed (\u000c)
\ r return character (\u000d)
\ double quotation mark (\u0022)
\ ' apostrophe or single quotation mark (\u0027)
\ Reverse slash (\u005c)
\xxx the Latin-1 character specified by the two-digit hexadecimal number XX
\uxxxx Unicode characters specified by the 4-bit hexadecimal number xxxx
                                                                                                                                                                                                                                                                                                                                                
Note: If the "\" character is located before a character that is not listed in the table, the "\" is ignored. For example, "\#" and "#" are equivalent. Don't forget that the backslash also has a function of using backslashes at the end of each line in a multiline string.

3 Boolean values

Values in JavaScript can be converted to Boolean values. of which, null, undefined, 0,-0, NaN, "" (empty string), these 6 values will be converted to False,false and these six values are sometimes called "false value"; all other values, including objects (arrays), are converted to true,true and these values are referred to as the corresponding " Truth value ". 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 mentioned is the entire content of this article, I hope you can enjoy.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.