Because javascript is loose, it needs a way to detect the data typeof variables, and Typeof is an operator of this function. Detecting a variable of type of may return one of the following strings:
Undefined variables are undefined. Boolean variables are Boolean values, String variables are strings, Number variables are numeric values, Object variables are objects, or Null function variables are functions. From a technical point of view, a function is an object, not a data type. However, functions have some special properties, so it is necessary to distinguish functions from other objects by typeof.
An undefined type has only one value, which is a special undefined type. When var is used to declare a variable but it is not initialized, the value of this variable is undefined, for example:
However, variables with undefined values are different from those that have not been defined, such as:
However, using typeof on variables that are undeclared or declared uninitialized will return undefined, for example:
Empty types also have only one value, which is Null. Logically, null value represents null pointer, so detecting null value of typeof will return "object", such as:
So if you want to define a variable to store objects, you'd better initialize the variable to null. In fact, undefined values are inherited from null values, so judging whether they are equal will return true:
alert(null = = undefined); //true
Although there is such a relationship between null and undefined, their purposes are completely different, because it is not necessary to set the value display of variables to undefined at any time. However, when defining an object variable with unsaved objects, the variable should be set to null, which can not only reflect null as a pointer to an empty object, but also distinguish null from undefined objects.
Boolean types have two literal values: true and false, but all types of values can be converted into Boolean values by calling the Boolean () function. The following table lists the conversion rules corresponding to various data types:
Data type is converted into true value and converted into false value BooleantruefalseString Any non-empty string ""Empty string Number Any non-zero value 0 and NaNObject Any object nullUndefined/undefinedNumber types are divided into integer and floating point number, and integers can be expressed in decimal, octal or hexadecimal, such as:
However, octal literals are invalid in strict mode, and all values will eventually be converted into decimal values when performing arithmetic calculations. Floating-point values must contain decimal points, for example:
The highest precision of floating-point numerical value is 17 decimal place, but it is far less accurate than integer in arithmetic calculation, for example:
NaN, that is, non-numerical, is a special numerical value. NaN has two characteristics: the result of any sum operation of NaN will return NaN, and NaN is not equal to any value, including NaN. Use the isNaN () function to determine whether a value is NaN. When receiving a parameter, isnan () will try to convert this value into a numerical value, and any value that cannot be converted into a numerical value will return true, such as:
IsNaN () can also convert objects. When an object calls isNaN (), it will first call the object's valueOf () method, and then determine whether the return value of this method can be converted into a numerical value. If not, it will use this return value to call the toString () method again, and then test the return value.
There are three ways to convert non-numeric values into numeric values: Number (), parseInt () and parseFloat (). Number () can convert values of any data type, while parseInt () and parseFloat () can only convert strings.
The Number () function has the following conversion rules:
1. If it is a Boolean value, true is converted to 1, and false is converted to 0;
2. If it is numeric, it is the same as the passed-in value;
var num = Number( 1); // 1
3. If it is empty, it will be converted to 0;
Var num = Number (empty); //0
4. If it is not defined, it will be converted to NaN;;
Var num = Number (undefined); //Nan
5. If it is a string value, it should be divided into various situations. If it is an empty string, it is converted to 0; If the string is a pure number, it will be converted into the corresponding numerical value; If the string is a number and contains ".",it will be converted to the corresponding floating-point value (if the string starts with 0, it will be ignored); If the string is in a valid hexadecimal format, it will be converted to the corresponding decimal value; If the string contains characters outside the above format, it is converted to NaN;; If the string is an object, the valueOf () method of the object will be called first, and then it will be judged whether the return value of this method can be converted into a numerical value. If the result is NaN, the toString () method will be called and the return value will be tested.
Because Number () is complicated when converting strings, parseInt () and parseFloat () are often used when converting strings. When converting a string, these two functions check whether the string conforms to the numeric pattern, start parsing from the first non-empty character, and return NaN (including empty string) if the first character is not numeric or negative. If the first character is a string, continue to parse the following characters until all characters are parsed or non-numeric characters are encountered.
ParseInt () can recognize various integer formats (decimal, octal and hexadecimal). If a string starts with "0x" followed by a numeric character, it will be parsed as hexadecimal, and if it starts with "0" followed by a numeric character, it will be parsed as octal (ECMAScript5 does not recognize octal, so it will ignore the preceding 0 and parse it as decimal).
To solve the compatibility problem, parseInt () provides a second parameter, which numeric format to parse.
ParseFloat () only recognizes the first decimal point, and the following decimal points are invalid. At the same time, parseeflow () only recognizes decimal values, so there is no second parameter, and other format values will be interpreted as 0.
A value of type String consists of a sequence of several Unicode characters, which can be expressed by single quotation marks ("") or double quotation marks (""), but the left and right quotation marks must match.
There are two ways to explicitly convert a value to a String: toString () and string (). Numeric values, Boolean values, objects and Strings all have methods of toString () and string (), while undefined and null only have methods of string (), and the parameters of toString () are in the converted binary format.