Home > front end >  JavaScript data type
JavaScript data type

Time:09-22

Knowledge points list:
A: (1) literal: some of you can't change value, for example: 1 2 3;
(2) variables: variables can be used to save the literal, and the value of a variable can be arbitrarily change;
(3) the identifier: in JS all can independently by our naming can be referred to as identifiers,
2: Javascrcipt [u] [u] [u] [/u] [/u] [/u] six types:
Basic data types: String String type, Number value type, Boolean type, Boolean, Null Null value types, Undefined Undefined type;
Reference data type: Object type

You can use a operator typeof to check a variable type
Grammar: typeof variable

(1) the String String
- string need to use quotation marks in the JS, single or double quotes can be, it is best not to mix;

(2) the Number value type
- all values are in JS Number types, including integers, floating point Numbers (decimal), if you are using a digital exceeded the maximum Number, will return a
Infinity said plus Infinity - Infinity said negative Infinity
Use the typeof check Infinity also returns Number
(3) Boolean Boolean type
Only two Boolean values, is mainly used to do logic
- true said really
False - false said
(4) Null (Null) type
Null (Null) only one type of value, is Null
Null empty this value is used to represent a special object
Use the typeof check a null value, returns an object
(5) the Undefined Undefined type
Undefined (Undefined) only one value, Undefined
Declare a variable - but not to the variable assignment, its value is undefined

Three: JavaScript data casts

1: converts the data type of the other type String
Method one:
- call is to convert the data types of the toString () method
- this method will not affect the original variable, it will be the result of the transformation to return
- note: null, and undefined the two values without the toString () method, if this method is invoked program complains
Method 2:
- call the String () function, and will be passed as a parameter to function conversion of data
- use the String () function when doing casts for Number Boolean is actually call the toString () method, but for null, and undefined, it will be called the toString () method will be null it directly must change as the "null" (String) it will direct must change to "undefined" undefined (String)

2: convert other types to Number type
(1) use Number () function
String - & gt; Digital
1. If it is pure digital string can be directly converted into digital
2. If the content of the string contains non-numeric, converted to NaN
3. If the string is an empty string or a string, all Spaces are converted to 0
Boolean value - & gt; Digital
True must change to 1 false transformation of 0
Null - & gt; 0 undefined - & gt; Digital NaN
(2) the second way:
- this way specifically to deal with string type
- the parseInt () convert a string to an integer
- the parseFloat () convert a string to a floating point number
ParseFloat () with the parseInt () are similar, but different is parseFloat () can get effective decimal

3: the other types are converted to Boolean type
- use the Boolean () function
- digital - & gt; Boolean in addition to 0 and NaN is false, the rest are all true
- string - & gt; Boolean in addition to an empty string is false, the rest are all true
Null, and undefined will be converted to false object will be converted to true
  • Related