1. Classification
Basic data types: undefined, null, string, Boolean, number
Complex data type: object
The object's attribute is defined in the form of an unordered name and value pair (name : value).
2. Detailed explanation
1. undefined : The undefined type has only one value: undefined. When a variable is declared using var but not initialized, the value of this variable is undefined.
Variables containing undefined values are different from variables that have not been defined. The following example can illustrate:
The code copy is as follows:
var demo1;//Declared but not initialized
alert(demo1);//undefined
alert(demo2);//Report an error, demo2 is not defined
2. null : There is only one value for the null type: null. From a logical point of view, the null value represents an empty object pointer.
If the defined variable is ready to be used to save the object in the future, it is best to initialize the variable to null instead of other values. In this way, just by directly detecting the null value, you can know whether the corresponding variable has saved an object's reference, for example:
The code copy is as follows:
if(car != null)
{
//Perform some operations on the car object
}
In fact, undefined values are derived from null values, so ECMA-262 specifies that their equality tests should return true.
alert(undefined == null); //true
Although null and undefined have such a relationship, their uses are completely different. In any case, it is not necessary to explicitly set the value of a variable to undefined, but the same rule does not apply to null. In other words, as long as the variable intended to save the object has not yet saved the object, the variable should be explicitly allowed to save the null value. Doing so not only reflects the convention of null as a pointer for null objects, but also helps to further distinguish between null and undefined.
3. Boolean : Boolean type has two values: true and false. These two values are not the same as numeric values, so true does not necessarily equal 1, and false does not necessarily equal 0.
It should be noted that the literal values of the Boolean type are case sensitive, that is, neither True nor False (and other forms of case mixing) are Boolean values, but are just identifiers.
Although there are only two literal values for Boolean types, all types of values in JavaScript have values equivalent to those of these two Boolean values. To convert a value to its corresponding Boolean value, you can call the type conversion function Boolean(), for example:
The code copy is as follows:
var message = 'Hello World';
var messageAsBoolean = Boolean(message);
In this example, the string message is converted to a Boolean value, which is stored in the messageAsBoolean variable. The Boolean() function can be called on values of any data type and a Boolean value will always be returned. As for whether the returned value is true or false, it depends on the data type to convert the value and its actual value. The following table shows the conversion rules for various data types and their objects.
These conversion rules are very important for understanding the automatic execution of corresponding Boolean transformations in flow control statements (such as if statements), for example:
The code copy is as follows:
var message = 'Hello World';
if(message)//equivalent to if(Boolean(message)==true)
{
alert("Value is true");//Value is true
}
Because of this automatic Boolean transformation, it is crucial to know exactly what variables are used in the flow control statement.
4. Number: integer and floating point
4.1 Integer: When performing calculations, all octal and hexadecimal numbers will be converted into decimal
4.2 Floating point: The highest accuracy of a floating point value is 17 bits, so its accuracy is far less than that of an integer when calculating arithmetic. For example: The result of 0.1+0.2 is not 0.3, but 0.3000000000000000000004. For example:
The code copy is as follows:
a=0.2;
b=0.1;
if(a+b==0.3){
alert("hello");
}
else{
alert("hi");
}
The result will pop up "hi", so don't test a specific floating point value.
4.3 NaN: Non-numeric Not a Number, this value is used to represent a case where an operand that originally wanted to return a value did not return a value (this will not throw an error).
NaN itself has two extraordinary characteristics. First, any operation involving NaN (e.g. NaN/10) will return NaN, a feature that may cause problems in multi-step calculations. Secondly, NaN is not equal to any value, including NaN itself. For example:
The code copy is as follows:
alert(NaN == NaN); //false
There is an isNaN() function in JavaScript. This function accepts a parameter, which can be of any type, and the function will help us determine whether this parameter is "not a numerical value". After receiving a value, isNaN() attempts to convert this value to a numeric value. Some values that are not numeric values are converted directly into numeric values, such as the string "10" or Boolean values. Any value that cannot be converted to a numeric value will cause this function to return true. For example:
The code copy is as follows:
alert(isNaN(NaN)); //true
alert(isNaN(10)); //false(10 is a numerical value)
alert(isNaN("10")); //false(may be converted to numerical value 10)
alert(isNaN("blue")); //true(cannot be converted to numerical value)
alert(isNaN("bule123")); //ture(cannot be converted to numerical value)
alert(isNaN(true)); //false(may be converted to a value of 1)
There are 3 functions that can convert non-numeric values into numeric values: Number(), parseInt() and parseFloat(). The first function, namely the transformation function Number(), can be used for any data type, while the other two functions are specifically used to convert strings into numeric values. These 3 functions will return different results for the same input.
The conversion rules of the Number() function are as follows:
● If it is a Boolean value, true and false will be replaced by 1 and 0 respectively.
● If it is a numeric value, it is just a simple pass and return.
● If it is a null value, return 0
● If undefined, return NaN
● If it is a string, follow the following rules:
○ If the string contains only numbers, convert it to a decimal value, that is, "1" will become 1, "123" will become 123, and "011" will become 11 (the leading 0 is ignored)
○ If the string contains a valid floating point format, such as "1.1", it is converted to the corresponding floating point number (also, leading 0 will be ignored)
○ If the string contains a valid hexadecimal format, such as "0xf", it is converted to a decimal integer value of the same size
○ If the string is empty, convert it to 0
○ If the string contains characters other than the above format, convert them to NaN
● If it is an object, call the object's valueOf() method, and then convert the returned value according to the previous rules. If the result of the conversion is NaN, the toString() method of the object is called, and then the returned string value is converted in turn according to the previous rules.
The code copy is as follows:
var num1 = Number("Hello World"); //NaN
var num2 = Number(""); //0
var num3 = Number("000011"); //11
var num4 = Number(true); //1
Since the Number() function is more complicated and unreasonable when converting strings, the parseInt() function is more commonly used when processing integers, and the parseFloat() function is often used when processing floating point numbers. For details, please refer to: http://www.cnblogs.com/yxField/p/4167954.html
5. String
The String type is used to represent a sequence of characters composed of zero or more 16-bit Unicode characters, i.e. a string. A string can be represented by single quotes (') or double quotes (").
The code copy is as follows:
var str1 = "Hello";
var str2 = 'Hello';
The length of any string can be obtained by accessing its length property.
The code copy is as follows:
alert(str1.length); //Output 5
There are two ways to convert a value to a string. The first is to use the toString() method that almost every value has.
The code copy is as follows:
var age = 11;
var ageAsString = age.toString(); //String "11"
var found = true;
var foundAsString = found.toString(); //String "true"
Numeric, Boolean, object and string values all have toString() methods. But null and undefined values do not have this method.
In most cases, calling the toString() method does not have to pass parameters. However, when calling the toString() method of the numeric value, you can pass a parameter: the cardinality of the output value.
The code copy is as follows:
var num = 10;
alert(num.toString()); //"10"
alert(num.toString(2)); //"1010"
alert(num.toString(8)); //"12"
alert(num.toString(10)); //"10"
alert(num.toString(16)); //"a"
From this example, we can see that by specifying the cardinality, the toString() method will change the output value. The value 10 can be converted to different numerical formats when outputting according to the different cardinality.
Without knowing whether the value to be converted is null or undefined, you can also use the transformation function String(), which can convert any type of value into a string. The String() function follows the following conversion rules:
● If the value has a toString() method, the method is called (no parameters) and the corresponding result is returned.
● If the value is null, return "null"
● If the value is undefined, return "undefined"
6. object
Objects are actually a collection of data and functions. Objects can be created by executing the new operator followed by the name of the object type to be created. By creating an instance of type Object and adding properties and (or) methods to it, you can create a custom object.
var o = new Object();
Any properties and methods of the object type also exist in more specific objects. Each instance of the object has the following properties and methods:
● constructor (constructor) - saves the function used to create the current object
● hasOwnProperty(propertyName) - used to check whether the given property exists in the current object instance (not in the prototype of the instance). Where, the property name (propertyName) as the parameter must be specified in a string form (for example: o.hasOwnProperty("name"))
● isPrototypeOf(object) - used to check whether the 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 object's string, numeric or boolean representation. Usually the return value of the toString() method is the same.
3. Small test
The code copy is as follows:
typeof(NaN)
typeof(Infinity)
typeof(null)
typeof(undefined)
Many interviews will ask the above questions~~
The above is an introduction to these 6 JavaScript data types. Have you understood it clearly? I hope you can improve after reading this article.