JavaScript data type Learning note _ javascript tips-js tutorial

Source: Internet
Author: User
This article focuses on the study notes of JavaScript data types. For more information, see five simple data types in ECMAscript, also known as basic data types: undefined, Null, Boolean, Number, and String. There is also a complex data type-Object.

Undefined type

When var is used to declare a variable but it is not initialized, the value of this variable is undefined. For example:

var number;document.write(number); //undefined

If no variable is declared, the following error occurs. For example:

Document. write (str); // Error

However, the undefined value is returned no matter whether it is declared or not when typeof is used. For example:

var num;document.write(typeof num); //undefineddocument.write(typeof str); //undefined

Null type

If typeof is used to detect null, "object" is returned ". For example:

var num = null;document.write(typeof num); //object

In fact, the undefined value is derived from the null value, so they all return true if they are equal. For example:

alert(null == undefined); //true

Note that as long as the variable to save the object has not actually saved the object, you should explicitly let the variable Save the null value, which helps distinguish between null and undefined. For example:

var num1 = null;var num2;document.write(typeof num1 + " " + typeof num2); //object undefined

Boolean Type

To convert a value to its corresponding Boolean value, you can use the transformation function Boolean (). For example:

var str = "helloworld";document.write(Boolean(str)); //true

3.4.4 In JavaScript advanced programming design provides tables about various data types and their corresponding conversion rules. There are probably the following types:

  • If the data type is String, any non-null String will be converted to true;
  • When the data type is Number, all values except 0 and NaN are converted to false;
  • If the data type is Object, all values except null are converted to true;
  • When the data type is Undefined, n/a is converted to true, and undefined is converted to false;

Note that if statements often use Boolean conversions that automatically execute responses as conditions. For example:

var str = "helloworld";if (str){  document.write("hellothere");} //hellothere

Number Type

In addition to decimal representation, integers can also be expressed by octal or hexadecimal literal values. The first of octal values must be 0, followed by a sequence of Octal numbers (0 ~ 7 ). For example:

Var num1 = 070; // 56var num2 = 032 of the octal node; // 32var num3 = 079 of the octal node; // The error octal node (> 7) var num4 = 09; // error octal (> 7)

The first two digits of the hexadecimal literal value must be 0x, followed by any hexadecimal number (0 ~ 9 and ~ F ). For example:

Var num1 = 0xA; // hexadecimal 10var num1 = 0xa; // It is also a hexadecimal value of 10 (uppercase and lowercase letters)

Floating point value

Note that you should never compare specific floating point values. For example:

Var a = 0.1; var B = 0.2; if (a + B = 0.3) {document. write ("you are right")} // because 0.1 plus 0.2 is actually equal to 0.30000000000000004

Value Range

The maximum and minimum values that ECMAScript can represent are stored in Number. MAX_VALUE and Number. MIN_VALUE. To determine whether a numeric value is poor, you can use the isFinite () function. For example:

document.write(isFinite(Number.MAX_VALUE + Number.MAX_VALUE)); //false

NaN

If 0 is divided by 0, NaN is returned. If a positive number is divided by 0, Infinity is returned. If a positive number is divided by 0,-Infinity is returned. Secondly, NaN is not equal to any value, including itself. In addition, the isNaN () function helps us determine whether this parameter is "not a value ". For example:

document.write(isNaN("a")); //true;document.write(isNaN("324")); //false;document.write(isNaN(true)); //false;document.write(isNaN(false)); //false;document.write(isNaN(NaN)); //true;

Numerical Conversion

There are three functions that can convert non-numeric values to numeric values: Number (), parseInt (), and parseFloat ().

The conversion rules of Number () are as follows:

If it is a Boolean value, true and false are converted to 1 and 0.
If it is a numeric value, it remains unchanged.
If it is a null value, it is converted to 0.
If it is undefined, NaN is returned.
If it is a string, the following rules are followed:

  • If there is only a number, it is converted to a decimal value.
  • If it is a floating point format, it is converted to the corresponding floating point value. The leading zero is also ignored.
  • If it is in hexadecimal format, it is converted to a decimal number.
  • If the string is empty, it is converted to 0.
  • In other cases, it is converted to NaN.

For more information, see the following example:

document.write(Number(true)); //1document.write(Number(false)); //0document.write(Number("789")); //789document.write(Number(null)); //0document.write(Number(undefined)); //NaNdocument.write(Number("02.0942")); //2.0942document.write(Number(0xa)); //10document.write(Number("")); //0document.write(Number("fdsa")); //NaN

The conversion rules of parseInt () are as follows:

  • If the first character is not a numeric character or symbol, parseInt () returns NaN.
  • If parseInt () is used to convert an empty string, NaN is returned.
  • If the first character is a numeric character, it will continue to parse the second character until a non-numeric character is encountered.

The following is an example:

Document. write (parseInt ("fds"); // NaNdocument. write (parseInt (""); // NaNdocument. write (parseInt ("11111_abc"); // 11111_document. write (parseInt ("-11111_abc"); //-11111_document. write (parseInt ("+ 11111_abc"); //-11111_document. write (parseInt ("0xa"); // 10document. write (parseInt ("0022.00009"); // 22document. write (parseInt ("070"); // ECMAScript 3 thinks it is 56 (octal), ECMAScript 5 thinks it is 70 (decimal)

In addition, ECMAScript 5 does not have the ability to parse the octal value. to eliminate this problem, you can provide the second parameter for this function: the base number used for conversion, as shown in the following code:

document.write(parseInt("070",10)); //70document.write(parseInt("070",8)); //56document.write(parseInt("070",16)); //112

In most cases, it is best to set the default value to 10 hexadecimal.

The conversion rules of parseFloat () are as follows:

  • Similar to parseInt (), the difference is that the first decimal point of the string is valid, and it is invalid to include the second decimal point from the second decimal point.
  • He cannot parse hexadecimal values !!!
  • It can only parse decimal values !!!
  • He does not use the second base to specify the hexadecimal usage.

The following is an example:

document.write(parseFloat("421")); //421document.write(parseFloat("0421.32.1")); //421.32document.write(parseFloat("0xaafd")); //0document.write(parseFloat("070")); //70document.write(parseFloat("070abc")); //70document.write(parseFloat("")); //NaNdocument.write(parseFloat("abc")); //NaN

String type

There are two methods to convert a value to a string. The first method is to use the toString () method that almost every value has. As follows:

document.write((533).toString(10)); //"533"document.write((0xa).toString(10)); //"10"document.write((0xa).toString(2)); //"1010"document.write((true).toString(10)); //"true"document.write((false).toString(10)); //"false"

In addition, null and undefined cannot be converted.

document.write((null).toString(10)); //document.write((undefined).toString(10)); //

If you do not know whether the value to be converted is null or undefined, you should use the transformation function String (). If it is null, "null" is returned. If it is undefined, "undefined" is returned ". As follows:

document.write(String(null)); //"null"document.write(String(undefined)); //"undefined"

In addition, we will introduce the Object type in detail in the next article.

The above is a brief introduction to JavaScript data types, and it is helpful for you to learn about JavaScript data types.

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.