Data type
There are 5 simple data types (also known as basic data types) in javascript: Undefined, Null, Boolean, number, and string. There are also 1 kinds of complex data types--object,object are essentially composed of a set of unordered name-value pairs.
typeof operator
JavaScript is loosely typed, so there needs to be a means to detect the data type of a given variable--typeof is the operator responsible for the provider side information. Using the typeof operator on a value may return one of the following strings:
"Undefined"-if this value is not defined;
"Boolean"-if this value is a Boolean value;
"String"--if the value is a string;
"Number"-if the value is numeric;
"Object"-if the value is an object or null;
"Function"--if the value is a function;
Undefined type
The undefined type has only one value, that is, a special undefined. When you declare a variable with VAR but do not initialize it, the value of the variable is undefined, for example:
Copy Code code as follows:
VAR message;
Alert (message = = undefined)//true
Null type
The null type is the second data type with only one value, and this particular value is null. From a logical point of view, a null value represents an empty object pointer, which is why the "object" is returned when using the TypeOf operator to detect NULL, for example:
Copy Code code as follows:
var car = null;
Alert (typeof car); "Object"
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 another value. This allows you to know whether the corresponding variable has saved a reference to an object by simply detecting the null value, for example:
Copy Code code as follows:
if (car!= null)
{
Perform some action on the car object
}
In fact, the undefined value is derived from a null value, so ECMA-262 stipulates that their equality test returns TRUE.
Copy Code code as follows:
alert (undefined = null); True
Although null and undefined have such a relationship, their use is completely different. No matter under what circumstances 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 that is intended to save the object has not actually saved the object, you should explicitly let the variable save the null value. Doing so will not only embody null as the practice of null object pointers, but also help to further differentiate between null and undefined.
Boolean type
The type has only two literal values: true and False. These two values are not the same as numeric values, so true is not necessarily equal to 1, and false is not necessarily equal to 0.
Although there are only two Boolean literals, values of all types in JavaScript have values equivalent to these two Boolean values. To convert a value to its corresponding Boolean value, you can call the type conversion function Boolean (), for example:
Copy Code code as follows:
var message = "Hello World";
var Messageasboolean = Boolean (message);
In this example, the string message is converted to a Boolean value that is saved in the Messageasboolean variable. You can call a Boolean () function on a value of any data type, and always return a Boolean value. Whether the returned value is TRUE or false depends on the data type of the value to be converted and its actual value. The following table shows the transformation rules for various data types and their objects.
These transformation rules are important for understanding flow control statements, such as if statements, to automatically perform corresponding Boolean transformations, such as:
Copy Code code as follows:
var message = "Hello World";
if (message)
{
Alert ("Value is true");
}
When you run this example, a warning box is displayed because the string message is automatically converted to the corresponding Boolean value (TRUE). Because of this automatic Boolean transformation, it is important to know exactly what variables are used in flow control statements.
Number Type
This type is used to represent integers and floating-point values, and a special value, that is, Nan (non-numeric not a number). This value is used to indicate that an operand that would otherwise return a numeric value does not return a numeric value (this will not throw an error). For example, in other programming languages, any number divided by 0 results in an error, which stops code execution. In JavaScript, however, any number divided by 0 returns Nan, so it does not affect the execution of other code.
Nan itself has two unusual characteristics. First, any operation involving Nan (for example, NAN/10) returns Nan, a feature that can cause problems in multi-step computations. Second, Nan is not equal to any value, including the Nan itself. For example, the following code returns false.
Alert (nan = = Nan); False
JavaScript has a isNaN () function that takes a parameter that can make any type, and the function will help us determine whether the argument is "not numeric." isNaN () after receiving a value, attempts to convert the value to a number. Some values that are not numeric are converted directly to numeric values, such as the string "10" or a Boolean value. Any value that cannot be converted to a number will cause this function to return true. For example:
Copy Code code as follows:
Alert (isNaN (NaN)); True
Alert (isNaN (10)); False (10 is a numeric value)
Alert (isNaN ("10")); False (may be converted to numeric 10)
Alert (isNaN ("Blue")); True (cannot be converted to numeric values)
Alert (isNaN (true)); False (may be converted to numeric 1)
There are 3 functions that convert non-numeric values to numeric values: Number (), parseint (), and parsefloat (). The first function, the transition function number (), can be used with any data type, while the other two functions are specifically used to convert the string to a numeric value. These 3 functions return different results for the same input.
The conversion rules for the number () function are as follows:
If this is a Boolean value, True and false are replaced with 1 and 0, respectively.
If it's a numeric value, just a simple pass in and return
If it is a null value, return 0
If it is undefined, return to Nan
If it is a string, follow these rules:
0 if the string contains only numbers, convert it to a decimal value, that is, "1" becomes 1, "123" becomes 123, and "011" becomes 11 (leading 0 is ignored)
0 if the string contains a valid floating-point format, such as "1.1", it is converted to the corresponding floating-point number (similarly, leading 0 is ignored)
0 if the string contains a valid hexadecimal format, such as "0xf", converts it to a decimal integer value of the same size
0 if the string is empty, convert it to 0
0 if the string contains characters other than the above format, convert it to Nan
If it is an object, the valueof () method of the object is called, and the value returned by the previous rule transformation is converted. If the result of the conversion is Nan, the object's ToString () method is called, and then the string value returned by the previous rule transformation is followed.
Copy Code code as follows:
var num1 = number ("Hello world"); NaN
var num2 = number (""); 0
var num3 = number ("000011"); 11
var num4 = number (true); 1
Because the number () function is more complex and unreasonable when converting strings, the parseint () function is more commonly used when dealing with integers. The parseint () function, when converting a string, is more about whether it conforms to a numeric pattern. It ignores the space before the string until it finds the first non-whitespace character. If the first string is not a numeric character or a minus sign, parseint () returns Nan, that is, converting an empty string with parseint () returns a Nan. If the first character is a numeric character, Praseint () continues to parse the second character, knowing that all subsequent characters have been parsed or a non-numeric character has been encountered. For example, "1234blue" is converted to 1234, and "22.5" is converted to 22 because the decimal point is not a valid numeric character.
If the first character in the string is a numeric character, parseint () can also recognize the various integer formats (decimal, octal, hexadecimal). To better understand the conversion rules for the parseint () function, here are some examples
Copy Code code as follows:
var num1 = parseint ("1234blue"); 1234
var num2 = parseint (""); NaN
var num3 = parseint ("0xA"); 10 (hex)
var num4 = parseint ("22.5"); 22
var num5 = parseint ("070"); 56 (octal)
var num6 = parseint ("70"); 70
var num7 = parseint ("10", 2); 2 (in binary parsing)
var num8 = parseint ("10", 8); 8 (by octal)
var num9 = parseint ("10", 10); 10 (parsed by decimal)
var NUM10 = parseint ("10", 16); 16 (parsed by hexadecimal)
var num11 = parseint ("AF"); 56 (octal)
var num12 = parseint ("AF", 16); 175
Like the parseint () function, parsefloat () also parses each character starting at the first character (position 0). It is also resolved to the end of the string, or to the point where an invalid floating-point number character is encountered. That is, the first decimal point in the string is valid, and the second decimal is invalid, so the string that follows it is ignored. For example, "22.34.5" will be converted to 22.34.
The second difference between parsefloat () and parseint () is that it will always ignore the leading 0. Because the parsefloat () value resolves the decimal value, it does not specify the usage of the cardinality with the second argument.
Copy Code code as follows:
var num1 = parsefloat ("1234blue"); 1234
var num2 = parsefloat ("0xA"); 0
var num3 = parsefloat ("22.5"); 22.5
var num4 = parsefloat ("22.34.5"); 22.34
var num5 = parsefloat ("0908.5"); 908.5
String Type
The string type is used to represent a sequence of characters consisting of 0 or more 16-bit Unicode characters, that is, a string. Strings can be represented by single quotes (') or double quotes (").
Copy Code code as follows:
var str1 = "Hello";
var str2 = ' Hello ';
The length of any string can be obtained by accessing its long property
Copy Code code as follows:
alert (str1.length); Output 5
There are two ways to convert a value to a string. The first is the ToString () method that uses almost every value.
Copy Code code as follows:
var age = 11;
var ageasstring = age.tostring (); String "11"
var found = true;
var foundasstring = found.tostring (); String "true"
Values, Boolean values, objects, and string values all have the ToString () method. However, null and undefined values do not have this method.
In most cases, calling the ToString () method does not have to pass parameters. However, when you call the ToString () method of a value, you can pass a parameter: the cardinality of the output value.
Copy Code code 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
This example shows that by specifying the cardinality, the ToString () method changes the value of the output. The value 10, depending on the cardinality, can be converted to a different numeric format at the time of the output.
You can also use the transformation function string () When you do not know if the value to convert is null or undefined, and this function can convert any type of value to a string. The String () function follows the following conversion rules:
If the value has the ToString () method, call the method (without parameters) and return the corresponding result
Returns "NULL" if the value is null
Returns "Undefined" if the value is undefined
Copy Code code as follows:
var value1 = 10;
var value2 = true;
var value3 = null;
var value4;
Alert (String (value1)); "10"
Alert (String (value2)); "True"
Alert (String (VALUE3)); "NULL"
Alert (String (VALUE4)); "Undefined"
Object Type
An object is actually a collection of data and functionality. An object can be created by executing the new operator followed by the name of the object type to be created. You can create a custom object by creating an instance of type object and adding properties and/or methods to it.
Copy Code code as follows:
Each instance of object has the following properties and methods:
constructor--saves the function used to create the current object
hasOwnProperty (PropertyName)--for checking whether a given property exists in the current object instance (not in the prototype of the instance). Where the property 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 enumerate with for-in statements
ToString ()--Returns the string representation of an object
valueof ()--Returns a string, numeric, or Boolean representation of an object. Usually the same as the return value of the ToString () method.