JavaScript Advanced Programming Series-Basic data types

Source: Internet
Author: User

The basic data types in JavaScript include:

Undefined, Null, Boolean, number, String 5 data types

1. Undefined type (only one value Undefined, often used to represent uninitialized variables)

    • If a variable is not initialized, its initial value is undefined and the data type is undefined

var MyVar;

MyVar = = = undefined//return True

typeof MyVar = = = ' undefined '//return True

    • The parameter list for the function, whose value is undfined for the argument that is not passed in

function sum (A, b) {

if (b = = = undefined) b = 0;

return a + B;

}

sum (in); Return 3

SUM (1); Return 1, where the second parameter does not pass a value, its value is undefined,

2. Null type (only one value is null, often used to represent an empty object pointer)

    • Because it is used to represent an empty object, typeof null = = = ' object '
    • Because the undefined value is derived from a null value, NULL = = undefined

3. Boolean type (with two values: true, False)

    • Data type conversions using a Boolean ()

String:boolean (' str '); True

Boolean ("); False

Number:boolean (2); True

Boolean (0); False

Object:boolean ({}); True

Null:boolean (NULL); False

Undefined:boolean (Undefined); False

4. String type

    • Data type conversions

var myVar = 5;

String (5); Return ' 5 ' to convert any type to string

Myvar.tostring (); Return ' 5 ', undefined with null value without this method

Myvar.tostring (2); Return ' 101 ', returned in binary form

MyVar + = "; Return ' 5 ', automatic conversion with data

5. Number Type

    • Floating-point types are best not used in conditional tests due to calculated rounding errors
    • Integer 8 binary is not valid in strict mode
    • Max Number.MAX_VALUE, Min number.min_value
    • Positive Infinity number.positive_infinity, negative infinity number.negative_infinity, function isfinite (value) is used to determine whether a value is between positive infinity and negative infinity
    • NaN represents a value that is not a value, and the function isNaN (value) is used to determine whether a value is ' not numeric ' after a data type conversion
    • Data type conversion number (), parseint (), parsefloat ()

Number (): You can convert any type of data, and the following expression returns True

Number (undefined) = = = NaN;

Number (NULL) = = = 0;

Number (' 34 ') = = = 34;

Number (' 34df ') = = = NaN;

Number (') = = = 0;

Number (true) = = = 1;

Number (false) = = = 0;

Number ({}) = = = NaN;

parseint (): Primarily used to convert a string to an integral type, the second parameter can be specified as a binary

parseint (' 12df ') = = = 12

parseint (' 2.4dd ') = = = 2

parseint (") = = = NaN

parseint (' 10 ', 10) = = = 10

parseint (' 10 ', 2) = = = 2

Parsefloat (): Used to convert a string to floating-point data, the rule is similar to parseint, but there is no second parameter but can only be converted to 10 floating-point numbers

JavaScript Advanced Programming Series-Basic data types

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.