Home > other >  JavaScript data types
JavaScript data types

Time:09-20

~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
Development tools and key technology: Adobe DreamweaverJavaScript
Author: WeiYongGui
Time to write: on April 20, 2020
~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
Knowledge points list:
1. Literal, variables, identifiers
Some literal: you can't change the value of the example: 1 3 5 6
Literal can be used directly, but we typically do not directly use literal
Variable quantity: variables can be used to save the literal, and the value of a variable can be arbitrarily change
Variables are more convenient, we use so in hair open by variables to save literal
Variable declaration: through the var keyword statement
Identifier: - in JS all can independently by our naming can be referred to as identifiers
- such as: variable name function name attribute names belong to identifier
- named 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. The identifier cannot use ES of keywords and reserved words
4. The identifier is generally USES the hump written
- first letters lowercase, 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 don't use so)
Var variable=12312; Do not recommend this to write
2. JavaScript six types of data:
Basic data types: String String type Number value type Boolean Boolean type
Null Null type Undefined Undefined type
Reference data type: Object type
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
- attention to the problem of quotes of nested
As escape character \
In a string we can use \ as the escape character,
When said the use of some special symbols can be used to escape
\\ "said" \ 'said' \ n newline \ t tabs
Number value type:
- all values are in JS Number types, including integers, floating point Numbers (decimal)
- expanding part: 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
- syntax: typeof variable
You can use a operator typeof to check a variable type
Boolean Boolean type: only two Boolean values, is mainly used to do logic
- true said really
False - false said
Null Null value 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
Undefined Undefined type: Undefined (Undefined) only one value, Undefined
Declare a variable - but not to the variable assignment, its value is undefined
3. The JavaScript data casts
Mainly to transform other data types to String String type Number value type Boolean Boolean type,
1) converts the data type of the other String String type
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. Other data types into a Number of numerical type
Method one:
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
Method 2:
- 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
- the parseInt () can be effective integer out content in a string, and then converted to Number
ParseFloat () with the parseInt () are similar, but different is that it can obtain the effective decimal
(3) other data types are converted to Boolean 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
  • Related