1.Undefined type
The Undefined type has only one value, which is the special undefined. When a variable is declared using var but not initialized, the value of the variable is undefined, for example:
var message;
alert(message == undefined); //true
Copy after login
This example only declares the variable message, but it is not initialized. Comparing this variable with the undefined literal shows that they are equal. This example is equivalent to the following example: var message = undefined;
alert(message == undefined); //true
Copy after login
This example explicitly initializes the variable message with an undefined value. But we don't need to do this, because uninitialized values will get undefined by default.
2.Null type
The Null type is the second data type with only one value. This special value is null . From a logical point of view, a null value represents a null object pointer, and this is why "object" is returned when using the typeof operator to detect a null value, as shown in the following example:
var car = null;
alert(typeof car); // "object"
Copy after login
If you define a variable that is going to be used to hold an object in the future, it is best to initialize the variable to null rather than to another value. In this way, you can know whether the corresponding variable already holds a reference to an object by directly checking the null value, as shown in the following example:
if (car != null){
// 对car 对象执行某些操作
}
Copy after login
In fact, The undefined value is derived from the null value, so ECMA-262 specifies that their equality test returns true:
alert(null == undefined); //true
Copy after login
Here, the equality between null and undefined The operator (==) always returns true, but be aware that this operator converts its operands for comparison purposes (more on this later in this chapter). Although null and undefined have this relationship, their uses are completely different. As mentioned before, there is no need to explicitly set the value of a variable to undefined under any circumstances, but the same rule does not apply to null. In other words, as long as a variable that is intended to hold an object does not actually hold an object, you should explicitly let the variable hold a null value. Doing this not only
reflects the convention of null as a null object pointer, but also helps to further distinguish null and undefined.
3.Boolean type
This type has only two literal values: true and false. These two values are not the same thing as numeric values.
So true is not necessarily equal to 1, and false is not necessarily equal to 0. The following is an example of assigning a
Boolean type value to a variable:
var found = true;
var lost = false;
Copy after login
It should be noted that the literal value of the Boolean type is true and false are case-sensitive. That is, True and False (and other mixed case forms) are not Boolean values, just identifiers. Although there are only two literal values of the Boolean type, all types of values in ECMAScript have values equivalent to these two Boolean values. To convert a value to its corresponding Boolean value, you can call the conversion function Boolean(), as shown in the following example:
var message = "Hello world!";
var messageAsBoolean = Boolean(message);
BooleanExample01.htm
Copy after login
In this example, the string message is converted into a Boolean value, which is saved in the messageAsBoolean variable. The Boolean() function can be called on a value of any data type and will 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 gives various data types and their corresponding conversion rules.
type of data | Convert to the value of true
| Convert to the value of false
|
Boolean##true | false | |
StringAny non-empty string | ""(empty string) | |
NumberAny non-zero numeric value (including infinity) | 0 and NaN | |
Object UndefinedAny object | null | |
undefined | undefined |
4.Number类型
5.NaN
NaN,即非数值(Not a Number)是一个特殊的数值,这个数值用于表示一个本来要返回数值的操作数未返回数值的情况(这样就不会抛出错误了)。例如,在其他编程语言中,任何数值除以0 都会导致错误,从而停止代码执行。但在ECMAScript 中,任何数值除以0 会返回NaN①,因此不会影响其他代码的执行。NaN 本身有两个非同寻常的特点。首先,任何涉及NaN 的操作(例如NaN/10)都会返回NaN,这个特点在多步计算中有可能导致问题。其次,NaN
与任何值都不相等,包括NaN 本身。例如,下面的代码会返回false:
alert(NaN == NaN); //false
针对NaN 的这两个特点,ECMAScript 定义了isNaN()函数。这个函数接受一个参数,该参数可以是任何类型,而函数会帮我们确定这个参数是否“不是数值”。isNaN()在接收到一个值之后,会尝试将这个值转换为数值。某些不是数值的值会直接转换为数值,例如字符串"10"或Boolean 值。而任何不能被转换为数值的值都会导致这个函数返回true。请看下面的例子:
alert(isNaN(NaN)); //true
alert(isNaN(10)); //false(10 是一个数值)
alert(isNaN("10")); //false(可以被转换成数值10)
alert(isNaN("blue")); //true(不能转换成数值)
alert(isNaN(true)); //false(可以被转换成数值1)
Copy after login
6.数值转换
有3 个函数可以把非数值转换为数值:Number()、parseInt()和parseFloat()。第一个函数,即转型函数Number()可以用于任何数据类型,而另两个函数则专门用于把字符串转换成数值。这3 个函数对于同样的输入会有返回不同的结果
以上就是JavaScript中数据类型详解的内容,更多相关内容请关注PHP中文网(m.sbmmt.com)!