Home > Back-end >  JavaScript data types
JavaScript data types

Time:12-02

Knowledge points list:
1. JavaScript literals, variables, identifiers
Some literal: you can't change value (typically do not directly use) such as: 1 2 3 5 6
Variables: variables can be used to save the literal and the value of a variable can be arbitrarily change
Variable declaration: through the var keyword statement
Code: var num=12;//var variable=literal;
Identifier:
- all can be independent in JS naming can be referred to as identifiers
- such as: variable name function name attribute names belong to identifier
- named identifier is the need to comply with the following rules:
1. The identifier can contain letters, Numbers, _ (underscore), $
2. The identifier cannot begin with digital
3. The identifier cannot use ES of keywords and reserved words
4. The identifier is generally USES the hump written
- first letters lowercase, or at the beginning of each word uppercase letters lowercase
the restFor example: a small hump helloWorld big hump helloWorld
JS underlying identifier for storage when actual use is Unicode
So in theory, all utf-8 contains content can be used as identifiers
(note: including the Chinese, Chinese can be used as identifier, but is not recommended)
You can use operator typeof detection data type
Grammar: typeof variable
2. 6 types of data: JavaScript
Basic data types (6) :
String String type
- need to use quotation marks in the JS string
- use single or double quotes can be, but it is best not to mix (note the quotes of nested)
Code: var STR="hi";
Number value type
- all values are in JS Number types, including integers, floating point Numbers (decimal)
- development: if you use digital exceeded the maximum Number, will return a
Infinity said plus Infinity
Negative Infinity - Infinity said
Use the typeof check Infinity also returns Number
Number. MAX_VALUE said the maximum value 1.7976931348623157 e+308
Number. MIN_VALUE numerical value of the minimum 5 e - 324
Code: var STR=11.20;
The console. The log (typeof STR);
Var Max=Number. MAX_VALUE;
The console. The log (Max);
Boolean Boolean type
Only two Boolean values, mainly for logic
- true said really
False - false said
Code: var bl=true;
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
Undefined (Undefined) only one value, Undefined
Declare a variable - but not to the variable assignment, its value is undefined
Code: var a;
The console. The log (a);//undefined undefined
Reference data type: Object type
3. The casts of JavaScript data
Mainly to transform other data types to String Number, Boolean,
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
Code: converts numeric types to string type
Var a=120;//number
Var b=a.t oString ();
The console. The log (typeof a);//test data types for the number number
console.log(typeof b);//test data type of the String data type
Converts Boolean type to string type
Var a=false;
Var b=a.t oString ();
The console. The log (typeof a);
console.log(typeof b);
Method 2:
- for null, and undefined invocation String () function, and will be passed as a parameter to function conversion of data
- use the String () function when making casts
Code: converts a null value type
Var a=null;
Var=String b (a);//call the String () function
The console. The log (typeof a);//number
console.log(typeof b);//the string
Summary: comparing two methods, null, and undefined in mind for a particular need to call the String () function, and for Number Boolean is actually call the toString () method,
(2) to convert other data types for the Number type
Use the Number () function
- string - & gt; Digital
1. If it is pure digital string can be directly converted into digital
Code:
Var num="112";
The console. The log (typeof num);//the string
B=Number (num);
The console. The log (typeof b);//number

2. If the content of the string contains non-numeric, converted to NaN
Code:
Var num="hu324";
B=Number (num);
The console. The log (b);//NaN

3. If the string is an empty string or a string, all Spaces are converted to 0
Code:
Var num="";
B=Number (num);
The console. The log (b);//0
4. Other types:
5. - Boolean value - & gt; For converting true must change to 1 false 0
- undefined - & gt; Digital NaN
- null - & gt; 0
The second way:
- this way specifically to deal with string type
- the parseInt () convert a string to an integer
Code:
Var num="12 px";
Num=parseInt (num);
The console. The log (num);//12
Summary: parseInt () can be a string in the effective content of integer out
And then converted to Number

- the parseFloat () convert a string to a floating point number
Code:
Var num="12.88 px";
Num=parseInt (num);
The console. The log (num);//12.88
Summary: parseFloat () with the parseInt () are similar, but different is that it can obtain the effective decimal
Code: var bl=false;
Bl=parseInt (bl);
The console. The log (bl);//NaN
Summary: the call is not a string type parseFloat () with the parseInt () the conversion of the value of the NaN
(3) other data 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
Code: var a=12;
A=Boolean (a);
The console. The log (a);//true
Var a=NaN;
A=Boolean (a);
The console. The log (a);//false
Summary: the decimal also to 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 is converted to a Boolean type to true
  • Related