JavaScript variables and data types
Js do client language
According to Js grammar, to operate the relevant elements in the page, and sometimes operate some of the features inside the browser
Js consists of three parts:
ECMAScript
(ES
): Describes the syntax of the language and basic objectDOM
(Document Object Model
): Document Object Model, the contents of the page description of the processing methods and interfaces through a number ofJs
properties and methods for manipulating the pageDOM
elementBOM
(Browser Object Model
): Browser object model, describing browser method and interactive interface, for operating the browser.
Js variables in Variable
JS data divided into two categories: First, the basic data types: Undefined
, , Null
, Boolean
, Number
, String
, and Symbol
second, the data type is: Object
, Object
essentially by a set of unordered's name pairs.
Variable: a variable amount, in a programming language, is actually a variable name, used to store things and represent different values.
// ES3
var a = 123;
// ES6
let b = 100;
b = 200 //报错
const c = 1000;
// 创建函数也相当于在创建变量
function fn() {}
// 创建类也相当于创建变量
class A{}
// ES6的模块岛屿也可以创建变量
import a from './a.js'
// Symbol创建唯一值
let n = Symbol(100)
let m = Symbol(100)
to sum up:
- With
var
variable declaration, the declaration may be repeated after the override earlier statement - Use
let
has scoped variables declared in the statement can not be repeated under the same scope variables, but you can modify the value of a variable, for examplelet a = 1;
, can not be declared againa
,let a = 2;
you will get an error, suggesting:Uncaught SyntaxError: Identifier 'a' has already been declared
- With
const
variable declaration is constant, not modify the value of the variable, but asObject
complex data types such, stored in memory is a pointer to the object, that is to say, withconst
still modify the value of the target object declaration but not declare the object again
The data type JS
- Basic data types
Digital
Number
(digital and conventionalNaN
)- String
String
- Boolean value
Boolean
- Null pointer
Null
- Undefined
Undefined
Unique value (unique value)
Symbol
- Reference data type
Object
object type{}
Ordinary objects[]
An array of objectsRegExp
Regular objectsFunction
Function Data TypesMath
Mathematical function objectDate
Date Object
Data type scenarios:
When we pass Ajax
before the request to get the data, first in the local initialization data, typically will give a default data, then the requested data to the data into a data structure which we defined, for example:
var data = {
count: 0,
isActive: true,
goods: [],
model: null,
message: ''
}
Verification data type typeof
operator
typeof
Operator is used to detect a given type of data variables. typeof
Data types can be detected operators are:
undefined
boolean
string
number
object
function
var message = 'some string'
console.log(typeof(message) // "string"
var arr = [1,2,3,4]
console.log(typeof(arr) // "object"
console.log(typeof(null) // "object"
In the example above, the call typeof null
will return "object"
, because of the special value null
is considered to be an empty object reference. By typeof
detecting whether a variable is an array is not accurate, it will return"object"