JavaScript variable two (data type, number,string,object)

Source: Internet
Author: User

Number type:

The number type is the most interesting and complex type of JavaScript. This type uses the IEEE754 format to represent both integer and floating-point value values (float numbers are also known as double-precision values in some languages). To support various numeric types, ECMA-262 defines different numeric literal formats.

Mainly divided into decimal, octal, hexadecimal. In addition to decimal notation, integers can be represented by octal (base 8) or hexadecimal (base 16) self-par value, where the first digit of the octal literal must be (0), followed by the octal number sequence (0-7). If the value in the literal value is out of range, the preceding 0 is ignored. Take a look at the following example:

<script>var intnum=10;  integer var octalnum1=01010; 520var octalnum2=018 in decimal; Invalid octal-parse to; </script>
A floating-point value is the value that must contain a decimal point and must have at least one digit after the decimal. Although there can be no integers before the decimal point, this is not recommended; Here are a few examples of floating-point values:

<script>var Floatnum1=1.1;var Floatnum2=0.1;var floatnum3=.1; Valid but not recommended </script>
Because the memory space required to hold a floating-point value is twice times the value of the saved integer, ECMAScript will lose no chance in converting the floating-point value to an integer value. Obviously, if the decimal point is not followed by any number, then the value can be saved as an integer value. Similarly, if the floating-point value itself represents an integer (such as 1.0), then the value is also converted to an integer;

For those large or small values, a floating-point value represented by the E notation (i.e. scientific method) can be used. The value represented by the E notation equals the number in front of e multiplied by the exponential power of 10. You can also use the E notation to represent very small values. such as 0.000000000000000001, this value can be used 1E-7. The highest precision of a floating-point value is 17 decimal places, but it is far less accurate than an integer in arithmetic calculations. For example, the result of 0.1 plus 0.2 is not 0.3,

But other values. This small error will cause us to be unable to test specific floating-point values.

For example

<script>var F1=0.1;var f2=0.2;if (f1+f2==0.3) {alert ("This is Wrong");} </script>
If we have to decide how to judge, we can also use the following methods;

var r = (0.3-(0.1 + 0.2)). toFixed (2);//R is the -0.00//of string and can then convert it to float type var f = parsefloat (r); alert (f);//Get F Yes -0, but with 0 is equal f = = = 0

range of values:

Due to memory limitations, ECMAScript cannot save all the values in the world. The minimum value that ECMAScript can represent is saved in Number.min_value-the value is 5e-324 in most browsers, the maximum value that can be represented is saved in Number.MAX_VALUE-in most browsers, This value is 1.7976931348623157e+308. If the result of a calculation results in a value that exceeds the range of the change, the value is automatically converted to a special infinity value. In particular, positive infinity and negative infinity;

NaN:

NaN, or non-numeric (not a number) is a special value that is used to indicate that the operand that would have returned a numeric value did not return a numeric value (so that no error is thrown). In Java, for example, any number divided by 0 results in an error, which stops code execution. In JavaScript, however, any count divided by 0 will return Nan, so it does not affect the execution of other code.

Nan itself has two unusual characteristics, first, any operation involving Nan will return Nan, and then any value is not equal to Nan, including the Nan itself.

Referring to these two characteristics of Nan, JS defines the isNaN () function. This function takes a parameter, and the argument can be any type, and the function attempts to convert it to a numeric value, such as "10" for a string type, or a Boolean value, which is true;

Numeric conversions:

There are three functions that convert a non-numeric value to a value: Number (), parseint, and parsefloat (), the first function can be used for any data type, and the other two functions are only applicable to strings.

String type:

The string type is used to represent a sequence of characters consisting of 0 or more 16-bit Unicode characters, the string. The string can be represented by double quotation marks (") or single quotation marks (');

1. Character literals

The string data type contains some special character literals, also called escape sequences, that represent nonprinting characters, or characters that have other uses.

\ n line-break; \ T tabulation; \b space; \ R enter; \f into the paper; \ \ Slash;

2. Characteristics of strings

The strings in JS are not to be made, that is, once the strings are created, their values cannot be changed. To change the string saved by a variable, first destroy the original string, and then fill the change with another string containing the new value;

Object type:

The object in JS is actually a set of data and functions. An object can be created by executing the new operator followed by the name of the object type to be created. Creating an instance of an object type and adding properties and methods to it, you can create a custom method;

var o=new Object ();

This is much like Java, but if we do not pass arguments to the constructor, we can omit the pair of parentheses that follow.

The most common use of this function is the valuyeof () and ToString () methods, which are either strings, numeric values, or Boolean representations of the returned objects, usually the same as the return value of the ToString () method.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

JavaScript variable two (data type, number,string,object)

Related Article

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.