Home > front end >  JavaScript data types
JavaScript data types

Time:09-21

Knowledge points list:
1. Literal, variables, identifiers
Some literal: you can't change value, for example: 1, 2, 3... , literal can be used directly, but we are generally not straight
By using literal,
Variables: variables can be used to save the literal and the value of a variable can be arbitrarily change variables is more convenient, we use so
In hair open by variables to save the literal, variable statement: through the var keyword statement
Identifier: in JS all can independently by our naming can be referred to as identifiers,
Variable names, for example, the function name attribute names belong to the identifier,
Naming an 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, identifier cannot use ES of keywords and reserved words
4 identifier is generally adopt hump writing (first letter lowercase, each word at the beginning of the letter, the rest of the lowercase, such as: small hump helloWorld big hump helloWorld)
Description: JS underlying preserve identifier when actual use is Unicode, so in theory, all of the utf-8 contains
Of the content can be used as identifier, special note: including the Chinese, Chinese can be used as identifier, but
Don't use so), for example: var variable=123;
2. 6 types of data: JavaScript
Basic data types: String String type Number value type Boolean Boolean type
Null Null type Undefined Undefined type,
Description:
String String types: String need to use quotation marks in the JS, use single or double quotes can be, but the most
Don't mix well, pay attention to the problem of nested quotes, as the escape character '\', the string we can use the
As the escape character '\', said when the use of some special symbols can use "" escape, {\ "table
", \ 'said, \ n newline, \ t TAB},
Number value type: all values are in JS Number types, including integers, floating point Numbers (decimal)
If you use digital exceeded the maximum Number, will return a Infinity said is no
Poor big, negative Infinity - Infinity said, use the typeof check Infinity also returns Number,
Boolean Boolean type: only two Boolean values, is mainly used to do logic, true true, false false said,
Null Null value type: Null (Null) only one type of value, is Null, the value Null specially used to represent an empty object,
Use the typeof check a null value, returns an object,
Undefined Undefined type: Undefined (Undefined) only one value, Undefined, declare a variable but not to the variable assignment, its value is 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
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 actually
Is to call the toString () method, but to null, and undefined
Don't call the toString () method,
It will be null directly must change to "null" (string)
It will direct must change to "undefined" undefined (string)
(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
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; For converting true must change to 1 false 0
- null - & gt; 0
- undefined - & gt; Digital NaN
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
(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
- 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

You can use operator typeof detection data type
Grammar: typeof variable

CodePudding user response:

Didn't pay attention, the content is a mess
  • Related