Summary of JavaScript types, values, and variables

Source: Internet
Author: User
Tags arithmetic operators

The data types of JavaScript are divided into two categories: primitive type and object type. 5 Primitive types: Number, String, Boolean, null (NULL), undefined (undefined). An object is a collection of properties, each of which consists of a name/value pair (the value can be the original value, or it can be an object). 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 class.

  2.1 Numbers

  Integer values and floating-point values are not distinguished in JavaScript. JavaScript can recognize the decimal integer direct volume (the so-called direct volume, which is the data value directly used in the program), and 16 binary values (prefixed with 0x or 0X, which is the number 0 is not the letter O.) Think of the letter o, that a hexadecimal value is not an identifier? Although the ECMAScript standard does not support octal direct quantities, some implementations of JavaScript can represent integers in octal form (prefixed with the number 0), and I use octal to assign values to a variable in the three browsers of IE, Chrome, and FF on my computer. However, in the strict mode of ECMASCRIPT6, the octal direct volume is expressly forbidden.

There are two types of floating-point direct quantities. ① traditional Real number notation: There is an integer part, a decimal point and a fractional component; ② Index counting method: That is, the real numbers followed by the letter E or E, followed by the sign, followed by an integer index.

   Overflow of 2.1.1 arithmetic operations

  The arithmetic operations in JavaScript do not get an error when the overflow (overflow), underflow (underflow), or divisible by 0.

Overflow: When the result of the operation exceeds the upper limit of the number that JavaScript can represent, the result is a positive infinity Infinity or a negative infinity large-infinity. The infinite worth of behavioral traits also conform to the reality: based on their addition, subtraction, multiplication, and divide operation results or infinite values (of course preserving their sign); 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 the special value "negative 0" when a negative number is underflow. The negative 0 and the whole 0 are basically equal (can even be tested with strict equality = = =), except as a divisor:

  

var zero = 0;    Positive zero value var Negz =-0;    Negative 0 value Zero = = Negz    //Expression return value = True1/zero = = = 1/negz    //Expression return value false, equivalent to determining whether positive infinity and negative infinity are strictly equal

  

Divisible by 0 returns either a positive infinity or a negative infinity value. But 0 divided by 0 will return Nan (the value of the Nan property of the JavaScript predefined object number). There are four cases of returning Nan: ①0 divided by 0② infinity by Infinity ③ to any negative number for the root operation ④ 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 if a variable x is nan: ① using the function isNaN () ② is determined by using the X! = x, and the expression result is true only if X is Nan. JavaScript also has a similar function, Isfinite (), that returns True when the parameter is not Nan, infinity, or-infinity.

  2.1.2 Binary Floating-point 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, a binary notation that can accurately represent fractions such as 1/2, 1/8, and 1/1024, but decimal fractions 1/10, 1/10, and so on, cannot be accurately represented. 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.2 Text

  2.2.1 String, Character set

  A string is a set of immutable ordered sequences of 16-bit values, each of which is usually derived from the Unicode character set. The length of the string is the number of 16 bits it contains. JavaScript uses string types to represent text. 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 uses the UTF-16 encoded Unicode character set, which is a sequence of unsigned 16-bit values. Unicode characters that cannot be represented as 16-bit are followed by the UTF-16 encoding rule-a sequence of two 16-bit values (or "surrogate pairs"). This means that a JavaScript string of length 2 may represent a Unicode character. Note: The various methods of manipulating strings defined by JavaScript work on 16-bit values, not characters, and do not handle surrogate pairs separately. The book sees here, and in conjunction 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 quotation marks. These two forms of delimiters can be nested, but cannot be nested in multiple layers (for example, double quotes can contain single quotes, and single quotes cannot contain double quotes again). As mentioned in the previous article, a string value can be split into several lines, each line must end with a backslash (\), when both the backslash and the line terminator are not string contents, that is, the string itself is not multi-line, but written in the form of multiple lines.

Note: ① strings are fixed in JavaScript (unless re-assigned), methods like replace () and toUpperCase () return new strings, and the original string itself does not change; ② string can be used as a reading group. In addition to using the Charat () method to query a single character, 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.2 escape character

escape character meaning  
\o nul character (\u0000)
\b backspace (\u0008)
\ t horizontal tab (\u0009)
\ n line break (\u000a)
\v vertical tab (\u000b)
\f page Break (\u000c)
\ r carriage return (\u000d)
\ " double quotation marks (\u0022)
\ ' apostrophe or single quotation mark (\u0027)
\ \ backslash (\u005c)
\xxx The Latin-1 character specified by the two-bit hexadecimal number xx
\uxxxx Unicode characters specified by the 4-bit hexadecimal number xxxx

                         ,         &NB Sp                          ,         &NB Sp                          ,         &NB Sp                          ,         &NB Sp                          ,         &NB Sp                          ,         &NB Sp                          ,         &NB Sp                                                            ,         &N Bsp                          ,         &NB Sp                      

Note: "\" is ignored if the "\" character is in front of characters that are not listed in the table. 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.

  2.3 Boolean value

The values in JavaScript can be converted to Boolean values. Where null, undefined, 0,-0, NaN, "" (empty string), these 6 values 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 those values are appropriately called " Truth ". 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.

null, undefined, and the contents of the object.

Summary of JavaScript types, values, and variables

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.