JavaScript中typeof知多少?


typeof運算符介 紹:
typeof 是一個一元運算,放在一個運算數之前,運算數可以是任意類型。
它返回值是一個字符串,該字符串說明運算數的類型。

你 知道下面typeof運算的結果嗎?

typeof(1);
typeof(NaN);
typeof(Number.MIN_VALUE);
typeof(Infinity);
typeof("123");
typeof(true);
typeof(window);
typeof(document);
typeof(null);
typeof(eval);
typeof(Date);
typeof(sss);
typeof(undefined);

看 看你會幾個?

如果看了以后,不是很明白的話,請看下面(明白的人就不用往下看了):
typeof是一個一元運算符,它返回的結果 始終是一個字符串,對不同的操作數,它返回不同的結果。
具體的規則如下:
一、對於數字類型的操作數而言, typeof 返回的值是 number。比如說:typeof(1),返回的值就是number。
上面是舉的常規數字,對於非常規的數字類型而言,其結果返回的也是number。比如typeof(NaN),NaN在
JavaScript中代表的是特殊非數字值,雖然它本身是一個數字類型。
在JavaScript中,特殊的數字類型還有幾種:
Infinity 表示無窮大特殊值
NaN            特殊的非數字值
Number.MAX_VALUE     可表示的最大數字
Number.MIN_VALUE     可表示的最小數字(與零最接近)
Number.NaN         特殊的非數字值
Number.POSITIVE_INFINITY 表示正無窮大的特殊值
Number.NEGATIVE_INFINITY  表 示負無窮大的特殊值

以上特殊類型,在用typeof進行運算進,其結果都將是number。

二、對於字符串類型, typeof 返回的值是 string。比如typeof("123")返回的值是string。 
三、對於布爾類型, typeof 返回的值是 boolean 。比如typeof(true)返回的值是boolean。
四、對於對象、數組、null 返回的值是 object 。比如typeof(window),typeof(document),typeof(null)返回的值都是object。
五、 對於函數類型,返回的值是 function。比如:typeof(eval),typeof(Date)返回的值都是function。
六、如 果運算數是沒有定義的(比如說不存在的變量、函數或者undefined),將返回undefined。比如:typeof(sss)、typeof(undefined)都返回undefined。

看完了六條規則,再回頭看一下,是不是很簡單了……

下面 我們將用程序代碼驗證一下:

<script>
document.write ( "typeof(1): "+ typeof(1)+ "<br>");
document.write ( "typeof(NaN): "+ typeof(NaN)+ "<br>");
document.write ( "typeof(Number.MIN_VALUE): "+ typeof(Number.MIN_VALUE)+ "<br>")
document.write ( "typeof(Infinity): "+ typeof(Infinity)+ "<br>")
document.write ( "typeof(\"123\"): "+ typeof( "123")+ "<br>")
document.write ( "typeof(true): "+ typeof( true)+ "<br>")
document.write ( "typeof(window): "+ typeof(window)+ "<br>")
document.write ( "typeof(document): "+ typeof(document)+ "<br>")
document.write ( "typeof(null): "+ typeof( null)+ "<br>")
document.write ( "typeof(eval): "+ typeof(eval)+ "<br>")
document.write ( "typeof(Date): "+ typeof(Date)+ "<br>")
document.write ( "typeof(sss): "+ typeof(sss)+ "<br>")
document.write ( "typeof(undefined): "+ typeof(undefined)+ "<br>")
</script>
 
 
 

typeof 運算符把類型信息當作字符串返回。

typeof 返回值有六種可能: "number," "string," "boolean," "object," "function," 和 "undefined."

null:空、無。表示不存在,當為對象的屬性賦值為null,表示刪除該屬性
undefined:未定義。當聲明變量卻沒有賦值時會顯示該值。可以為變量賦值為undefined
number:數值。最原始的數據類型,表達式計算的載體
string:字符串。最抽象的數據類型,信息傳播的載體
boolean:布爾值。最機械的數據類型,邏輯運算的載體

object:對象。面向對象的基礎

 

1.對於數字類型的操作數而言, typeof 返回的值是 number。

JavaScript中代表的是特殊非數字值,雖然它本身是一個數字類型。

在JavaScript中,特殊的數字類型還有幾種:

  • Infinity            表示無窮大特殊值
  • NaN            特殊的非數字值
  • Number.MAX_VALUE      可表示的最大數字
  • Number.MIN_VALUE      可表示的最小數字(與零最接近)
  • Number.NaN         特殊的非數字值
  • Number.POSITIVE_INFINITY  表示正無窮大的特殊值
  • Number.NEGATIVE_INFINITY  表示負無窮大的特殊值

以上特殊類型,在用typeof進行運算進,其結果都將是number。

 

2.對於字符串類型, typeof 返回的值是 string。

3.對於布爾類型, typeof 返回的值是 boolean。

4.對於對象、數組、null 返回的值是 object。

5.對於函數類型,返回的值是 function。

6.如果運算數是沒有定義的(比如說不存在的變量、函數或者undefined),將返回undefined。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM