JS has a total of 6 data types defined for us, namely:undefined,null,Boolean(Boolean),number (numeric),string (string),object. (Additional data types are defined in ES6). Where the first 5 is a simple data type, object is a complex data type
Before we understand each type of data, we say variables , which are containers for storing information in any language. For example: in mathematics x = 5; that x is the container of the number 5, and x is the variable. Use the var keyword in js to define a variable. For example: var x = 5;
In this case, we are well aware of what data types are. In mathematics, numbers are also divided into integers, decimals, negative numbers ... And so on several types, each type is suitable for what kind of arithmetic, all have the explicit stipulation. So in the JS language, this defines the 6 data types, as well as the usage rules and usage scenarios for each type. Next we'll look at each.
Before we talk about each type we introduce a JS operator:typeof (the data type used to detect variables).
var A;
typeof (a); Undefined
var B = 123;
typeof B; Number
The above code can see that typeof () is optional.
Undefined
Only one value is undefined;
Let's start by defining a undefined variable:
var a = undefined;
We know that undefined is undefined, so what does this variable mean? In fact, undefined is not used to define a variable, but to automatically assign a value to undefined when the variable is not initialized. For example:var a; At this time a is automatically given the value of undefined.
Then why did you do it? We think about math when we write an X, can we say that x is an integer or a decimal or a negative number? Definitely not! Similarly, if there is no undefined this data type, we define a variable var A; Can we say that this variable is a number? String? Or an object? You sure you can't? Now with undefined, we can say that this is a undefined type. This is the meaning of the undefined data type.
NULL
There is only one value that is null;
var b = null;
At a logical angle, NULL represents a null pointer object. What is the purpose of the null? We can use typeof nullto find that the return is not null, but object, Oh, you're a liar, don't you say typeof can detect data types? In fact, this is exactly what it means to use NULL. If we define a variable, we prepare to save the object in the future. Then we'd better initialize this variable to null.
Another thing to mention here is that undefined is derived from a null value. So
undefined = = NULL; True
Boolean
There are two values, respectively: true and false;
var C = true;
var d = false;
Although the Boolean type has only two values, all data types in JS can be converted to and from a Boolean value. The conversion rules are as follows:
Data type True False
String non-empty strings empty string
Number is not a 0 numeric value (including infinity) 0/nan
Object NULL for any objects
Undefined not suitable for undefined
These translation rules are important for learning the later Process Control statements.
Number
This type represents a number:
var f = 1234566;
var g = 1.2356;
Another number type has a special value:NaN(non-numeric not a number)
This value is used to indicate that an operand that would have returned a numeric value does not return a numeric value (so that no error is thrown). For example, in other programming languages, dividing any number by 0 will result in an error, thereby stopping code execution. In JavaScript, however, any number divided by 0 returns Nan, so it does not affect the execution of other code.
String
Use this type to represent a string:
var s = "abcdef";
var t = "Beijing";
The string can be represented by a single quotation mark (') or double quotation mark ("). as long as the variable is represented by single or double quotation marks, the variable is a string . Look at the following examples:
var a = "true"; typeof A; "String"
var b = "5689"; typeof B; "String"
Object
An object is a set of data and features .
var o =new Object ();
O.name= "Beijing"; Properties of the Object
O.run = function () {}; Methods of the Object
When you create an object, JS automatically adds the following default properties and methods to the object instance.
constructor--holds the function that is used to create the current object.
hasOwnProperty (PropertyName)--Used to check for the existence of a given property in the current object instance, rather than in the prototype of the instance. where the attribute name (PropertyName) as a parameter must be specified as a string (for example: O.hasownproperty ("name")).
isPrototypeOf (object)--Used to check if an incoming object is a prototype of another object.
propertyIsEnumerable (PropertyName)--Used to check whether a given property can be enumerated using the For-in statement.
ToString ()--Returns the string representation of the object.
ValueOf ()--Returns the string, numeric, or Boolean representation of the object. Usually the same as the return value of the ToString () method.
JavaScript data types