javascript-data types and data type conversions

Source: Internet
Author: User

In particular, the following is the Dahne technology web front-end lecturer Zhang Dong Zhang's original notes, without permission, can not be transferred to other commercial, only for learning.

1. What is javascript: languages that specialize in web page interaction

2. What variable: Store in memory * one * data storage space, then a name

Declaration: Create a variable, var variable name;

Assignment: Saves the data to the right of the equal sign to the variable to the left of the equals sign. Variable name = value;

Value: Using a variable name anywhere is equivalent to using the value in a variable directly

Special: 1. Shorthand: var variable name = value;--Recommendations

2. Declaration in advance: before formally executing the program, all variables declared by VAR are pre-read and centrally created at the top. Assigning values to stay in place * * *

Problem solving: First declare the state after the advance, and then judge the output

3. Error: declared only, but not assigned variable, can be used.

Undeclared variable, can be directly assigned--strongly not recommended

An undeclared variable participates in an operation or expression:

Error: Reference error

3. Data type: 2 major categories:

The first large type: the value of the original type is stored directly in the variable local data type

"5": number String Boolean undefined null

Number : 64 bits = 8 bytes, the occupied space is independent of the numerical size

Special: Rounding Error: The computer also has an endless number of calculations

FIX: Rounding->n.tofixed by a specified number of decimal digits (d)

string: A string of characters made up of a sentence

Save each character with a Unicode number in JS

Unicode: One number for each word in the world's major languages

View any character Unicode number: "word". charCodeAt (0)

Memory consumption: An English letter, number or symbol 1 bytes, Chinese characters accounted for 2 bytes

Special: Once the string creation cannot be changed, it can only be replaced as a whole.

This results in a result: a string is wasted each time the string + stitching

When a string of numbers is added "":

If the data may be used as an arithmetic calculation or a comparison, the unquoted-number type

For example: The number of age prices

Quote-string Type If data cannot be used as an arithmetic calculation or comparison

For example: QQ ID Phone number

Boolean: only two values: True/false

When to use: when used as a condition or conclusion

second Largest type: value of reference type cannot be saved directly to the data type of the variable local.

  Why :JS has data type conversion?

JS is a weakly typed language, with 3 features:* * *

1. When declaring a variable, do not specify the data type in advance

2. The same variable can save different types of data successively

3. JS will automatically convert the data type to use according to its own needs

Data type conversions: 2 major categories:

1. Implicit conversion: No programmer intervention required, JS auto-complete type conversion

Implicit conversions in arithmetic operations:

1. Default to digital, then operation

2. + operation, as long as one data is a string:

+ Changed to string concatenation, not string data, converted to string

Special: Default: BOOL Type: true-->1 false-->0

Convert to Nan if no number is converted

Nan participates in arithmetic operations, and the result is always Nan

2. Cast: the programmer actively invokes the type conversion of the function implementation 

  any string : 2 functions

var str=x.tostring () x cannot be null or undefined

var str=string (x) Magnum--equivalent to implicit type conversion

are generally called automatically and do not need to be called manually

Detection data type: typeof (X)--Return type name

Actually the source of string (x):

function String (x) {

if (x===null) {

return "NULL";

}else if (x===undefined) {

return "undefined";

}else{

return x.tostring ();

}

}

Complement: Var value=x.valueof (), only takes out the value inside X, does not do type conversion

Despise title: Distinguish string () toString () valueOf ()

Any number: 3 functions

Any number: var n=number (x)--equivalent to implicit conversion

Emphasis: Only the contents of a pure number can be converted in numbers

Special: Number (NULL)->0 number (undefined)->nan

Number ("")->0

When to use: automatically called when an implicit conversion to a number

"String-by-number":

var n=parseint(str)

From left to right, read the numeric characters in Str sequentially

Exit until the first non-numeric character is encountered

Automatically skips empty characters at the beginning

Do not recognize decimal points, omit fractional parts

var n=parsefloat (str)

Recognize the first decimal point

When to use: When a string type is used to go to a number

Number vs Parsexxx

for bool Type: Number can go, parsexxx can't go

String with end-of-band unit: number cannot be converted, parsexxx can go

arbitrary->bool:boolean (x)--Implicit conversion

Rule: Except 0,nan,null,undefined, "", to False,

The rest is converted to true.

javascript-data types and data type conversions

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.