Javasscript has 5 simple data types (also a basic data type):
Undefined, Null, Boolean, number, String
1 of complex data types:
Object
The data type of all values in JavaScript is one of the above 6 types. So we need a way to detect what type of data the given variable is, and the typeof operator can help us solve the problem.
var msg= "blog Park";
Alert (typeof msg); "String"
Alert (typeof 110); "Number"
Using the typeof operator, returns one of the following strings:
"Undefined"--this value is undefined
"Boolean"--Boolean value
"String"--string
"Number"--numeric
"Object"--object or null
"Function"--functions
See here we have some doubts about "object"--object or NULL, why typeof Null returns object, and here we need to remember that NULL is a special type, he represents a reference to an empty object, so it returns object.
Gorgeous Split Line--------------------------------------------------------------------------------------------------------- -------------------------------------------------------
1.Undefined type
When we declare a variable without initializing it, the value of the variable is undefined:
var msg;
alert (msg==undefined); True
The example declares the variable msg and does not initialize it, so it pops up the undefined.
So we can also use undefined to initialize variables when declaring variables: Var msg=undefined,
But uninitialized values get undefined by default, which is not necessary.
var msg; Declare a variable and get undefined by default
Variable name is not declared
var name
Alert (msg); "Undefined"
alert (name); Generate an error
The above code name variable was not declared, and an error was generated when passed to the alert function.
Then we use typeof to check our variables:
var msg; Declare a variable and get undefined by default
Variable name is not declared
var name
Alert (typeof msg); "Undefined"
Alert (typeof name); "Undefined"
The result is an uninitialized and undeclared variable execution typeof returns undefined, which is logically reasonable. Because although these two variables are technically different from the point of view.
Even uninitialized variables are automatically given undefined values, but the habit of explicitly initializing variables (that is, assigning values to variables while declaring a variable) is developed. If this can be done, then when the typeof operator returns undefined
, we know whether the detected variable is or is not declared, not yet initialized.
2.Null type
Previously said NULL is a special type that represents a reference to an empty object.
But what is the relationship between undefined and null? Look at the following code:
1
alert (null==undefined); True
In effect, undefined values derive from null values, so ECMA-262 their equality tests to return Teue.
In spite of this relationship, their use is completely different. I have also said that declaring variables that are not necessary to display the initialization to undefined, will be obtained by default. This is not true for NULL, which means declaring a variable to hold the object as long as
Variable is not saved, you should let the variable explicitly save the null value. This can not only embody null as the attribute of null object pointer, but also distinguish between null and undefined.
3.boolean type
The Boolean type has only two values: True and False. These two values are not the same as numeric values, so true is not necessarily equal to 1,false and not necessarily equal to 0.
Boolean literal values True and false are case-sensitive. Like true and False are not Boolean values, but identifiers.
Although Boolean values are only two, other types of values have values equivalent to those of the two Boolean values. To convert a value to its corresponding Boolean value, you need to call the Boolean () function.
var msg= "blog Park";
Alert (a Boolean (msg)); True
In the above code, the string msg is converted to a Boolean value. You can call a Boolean () function on a value of any data type, and always return a Boolean value. Whether this value is true or false depends on the data type and the specific value of the converted value.
Look at the following data types and the corresponding conversion rules:
Value with value converted to true for data type conversion to False
Boolean true False
string is not empty "" Empty string
Number Non 0 value 0 and NaN
Objeact any object Null
Undefined------Not applicable Undefined
These conversion rules help us to understand that the control flow statement if automatically performs the corresponding Boolean transformation:
var msg= "blog Park";
if (msg)
{
Alert ("variable msg is converted to true");
}
When you run the above code, a warning box appears. The string msg is automatically converted to the corresponding Boolean value. Knowing this principle, we can use variables in the control statement if.
4.number type
3.1 integers, the most common is the decimal integer. Of course, there are eight in, hexadecimal, here do not explain.
It should be noted that all numeric values in eight and hexadecimal will eventually be converted to decimal values when doing arithmetic calculations.
3.2 Floating-point value, that is, the number must contain a decimal point, followed by a digit.
var num1=1.1;
var num2=1.2;
var num3=.3; Valid, but not recommended
Because the amount of memory required to hold a floating-point number is twice times the value of an integer, ECMAScript automatically converts the corresponding floating-point number to an integer number. If the decimal point is not followed by a value (2.) or followed by a 0 (2.0),
This value is saved as an integer value:
var num1=2.;
alert (NUM1); 2, no numeric resolution to 2 after the decimal point
var num2=2.0;
alert (num2); 2, itself is an integer resolution of 2
For those large or small values, the e notation (scientific notation) can be used to represent floating-point values.
The format of the scientific notation: integer/floating-point number + e/e + 10 exponent in power
3.125 e 7 = 1
var Num1=3.125e7; 31250000
3.3 Value range, because of the memory limit, and can not save all the values.
Minimum value to represent: Number.min_value, this value is 5e-324.
Maximum value to be represented: Number.MAX_VALUE, this value is 1.7976931348623157e+308.
If one of your calculated values is not within the JavaScript minimum and maximum range, your value is automatically converted to a special infinity value.
If this value is a negative number, it is converted to-infinity (negative infinity), and if it is a positive number, it is turned into Infinity (positive infinity).
3.4 NaN: Non-numeric. There are two features:
Any operation involving Nan will return a Nan (for example, NAN/5).
Second, Nan is not equal to any value, including itself.
alert (Nan==nan); False
For these two features of Nan, JavaScript defines the isNaN () function. This function receives a parameter that can be of any type, and the function will help us determine whether the parameter is "not numeric".
isNaN () When a value is received, it attempts to convert the value to a numeric value, and some values that are not numeric are directly converted to values (the string "10" or Boolean), and any that cannot be converted returns true
Alert (isNaN (NaN)); True
Alert (isNaN (10)); False 10 is a numeric value
Alert (isNaN ("10")); False "10" can be converted to a value of 10
Alert (isNaN ("blog Park")); True cannot be converted to numeric values
Alert (isNaN (false)); False can be converted to 0
5.String type
A string is a basic type of data for JavaScript.
The length property of a String object declares the number of characters in the string.
The string class defines a number of methods for manipulating strings, such as extracting characters or substrings from strings, or retrieving characters or substrings.
6.object type
A collection of data and functions that an object can create by using the new operator.
var obj=new Object ();
You can customize the properties or methods of an object:
var obj=new Object ();
Obj.name= "John";
Obj.say=function () {
Alert (this.name)
}
You can also write this:
var obj={
Name: "John",
Say:function () {
Alert (this.name)
}
}
The result is the same.
In JavaScript, object is the basis of all objects, so all objects have properties and methods of object.