JS Data type Detailed

Source: Internet
Author: User
Tags chrome developer chrome developer tools

 There are six types of data in JavaScript, five of which are basic data types: Undefined, Null, Boolean, number, and string, and then there is a complex data class--object.

First, recognize the typeof operator

typeof can be used to detect the data type of a given variable.

    • "Undefined" This value is undefined
    • "Boolean" This value is a Boolean value
    • "String" The value is a string
    • "Number" The value is numeric
    • "Object" The value is an object or null
    • The value of "function" is

typeof General Usage:

Open Google Chrome Developer Tools tool, shortcut key (CTRL+SHIFT+J):

var str = "this string"; Console.log (typeof  (message));//"string" Console.log ( typeof );//number

Ii. Types of undefined

  There is only one value for this type, and that is undefined. A undefined is returned when the variable is declared in JavaScript with Var, but it is not initially evaluated.

var= = undefined); // true

If an undefined variable is output, an error is generated instead of undefined.

var str; // the undefined value is obtained by default after this variable declaration
var num;
Pretends to define NUMconsole.log (str); // undefinedconsole.log (num); // Error

Three, NULL type

The same null also has only one value of the data type, the null value represents an empty object pointer, and the typeof operator detects when we find a null value that returns "Object":

var NULL ; Console.log (typeof//"Object"

In general, undefined values derive from null values, and in ECMA-262 they are specified to return true for equality tests:

Console.log (null = = undefined);

Four, Boolean type

  The Boolean type has two literal values: true and false, but often these two values cannot be represented by 1 or zero. You can use the Boolean () method to convert the value:

var Boolean = "Hello world!" ; var thisboolean = Boolean (boolean);
Console.log (Thisboolean);//true

V. Type of number

  The number type uses the IEEEE754 format to represent integer and double numeric values (floating-point numbers), and here are some representations:

var inta = 20; // integer var octal = 070; // octal number var hex = 0xA; // hexadecimal var float = 3.141e5; // equals 314100 // range of Values var result = Number.MAX_VALUE + number.max_value;console.log (isfinite (Result));

  NaN (not a number)

Nan is a special value that represents a case where the operand that would have returned a numeric value does not return a numeric value, and JavaScript defines the isNaN () function.

IsNaN () can receive a parameter, which can be any type, and after receiving the parameter, it attempts to convert the parameter to a numeric value, which is denoted by true and false:

Console.log (IsNaN (NaN)); // trueconsole.log (IsNaN (0)); // false 0 is a numeric console.log (IsNaN ("0")); // false can be converted to a numeric Console.log (IsNaN (true)); // false can be converted to a value of 1console.log (IsNaN ("Blue")); // true cannot be converted to numeric value

Numeric conversions have three functions that convert non-numeric values to numeric values: Number (), parseint (), parsefloat (), where number () can be used for any data type, while the next two are used to convert a string to a numeric value.

Number () method

var thisnum = number ("Hello world"); // NaN var thisnum = number (""); // 0 var thisnum = number ("011"); //  One var thisnum = number ("false"); // 0 var thisnum = number ("This.window"); // NaN

parseint () method

Some examples:

varNUM1 = parseint ("1234blue");//1234varnum2 = parseint ("");//NaNvarnum3 = parseint ("0xA");//10 (hexadecimal number)varNUM4 = parseint (22.5);// AvarNUM5 = parseint ("070");//56 (octal number)varNUM6 = parseint ("70");//70 (decimal number)varNUM7 = parseint ("0xf");//15 (hexadecimal number)

There are differences between ECMASCRIPT3 and 5 when it comes to the transformation of the binary. Like what:

// ECMAScript 3 is considered 56 (octal), ECMAScript 5 is 70 (decimal) var num = parseint ("070");

In order to solve this problem parseint provides another parameter that can be specified for the corresponding binary:

var // 2 (by binary parsing) var // 8 (parsing by octal) var // 10 (resolution by decimal) var // 16 (parsing by hexadecimal)

Parsefloat () method

Some methods:

var // 1234 (integer) var // 0 (hex) var // 22.5 var // 22.34 var // 908.5(octal has no effect)  var // 31250000

Vi. type of string

  

General characteristics of Strings:

var str1 = "This was the letter"var str2 = "SIGMA:\U03A3"// contains the transfer character str = str1 + str2 ; Console.log (str.length); //  -

ToString () method

This method can convert 4 different values: numeric value, Boolean value, NULL, and undefined. Both numeric and Boolean conversions are obtained with the same result, and null and undefined do not have the ToString () method, so the associated literal is returned directly.

var str1 = ten; var true ; var NULL ; var Str4;console.log (String (str1)); // "Ten"Console.log (String (str2)); // "true"Console.log (String (STR3)); // "null"Console.log (String (STR4)); // "Undefined

Summarize:

    1. We know that. There are six kinds of JavaScript data types, five basic types, and one complex type.
    2. The type of the value can be detected using typeof ().
    3. Known as Nan, a non-numeric value (not a number) is a special value that can be used to indicate an operation numeric condition that is not returned.
    4. Learn to convert strings with number (), parseint (), parsefloat () three methods.
    5. The method of the string type, ToString (), converts the value to a string.

  There is not an object type to learn, I will add in the back, if there are errors or not rigorous place, please be sure to correct, thank you very much. If you like or have some inspiration, welcome to add a collection, together refueling learning ah.

JS Data type Detailed

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.