javascript typeof()的用法與運算子用法

來源:互聯網
上載者:User

typeof 運算子
返回一個用來表示運算式的資料類型的字串。

typeof[()expression[]] ;

expression 參數是需要尋找類型資訊的任意運算式。

說明
typeof 運算子把類型資訊當作字串返回。typeof 傳回值有六種可能: "number," "string," "boolean," "object," "function," 和 "undefined."

typeof 文法中的圓括弧是可選項

 

<html>
<head>
<title>Debug Example</title>
<script type="text/網頁特效">

function aFunction(iNum1) {
    if (typeof iNum1 == "number" ){
       alert("number");
    }

    if ( typeof iNum1 == "string") {
        alert("string");
    }
}
aFunction("a");
aFunction(1);
</script>
</head>
<body>
</body>
</html>


 在js裡用到數組,比如 多個名字相同的input, 若是動態產生的, 提交時就需要判斷其是否是數組.if(document.mylist.length != "undefined" ) {} 這個用法有誤.

正確的是 if( typeof(document.mylist.length) != "undefined" ) {}

或 if( !isNaN(document.mylist.length) ) {}

typeof的運算數未定義,返回的就是 "undefined".

運算數為數字 typeof(x) = "number"

字串 typeof(x) = "string"

布爾值 typeof(x) = "boolean"

對象,數組和null typeof(x) = "object"

函數 typeof(x) = "function


在js裡用到數組,比如 多個名字相同(的)input, 若是動態產生(的), 提交時就需要判斷其是否是數組. if(document.mylist.length != "undefined" ) {} 這個用法有誤.
正確(的)是 if( typeof(document.mylist.length) != "undefined" ) {}
或 if( !isNaN(document.mylist.length) ) {}
typeof(的)運算數未定義,返回(的)就是 "undefined".
運算數為數字 typeof(x) = "number"
字串 typeof(x) = "string"
布爾值 typeof(x) = "boolean"
對象,數組和null typeof(x) = "object"
函數 typeof(x) = "function"
typeof 運算子返回一個用來表示運算式(的)資料類型(的)字串。
可能(的)字串有:"number"、"string"、"boolean"、"object"、"function" 和 "undefined"。
如:

alert(typeof (123));//typeof(123)返回"number"
alert(typeof ("123"));//typeof("123")返回"string"
<html>
<head>
<title>Debug Example</title>
<script type="text/javascript">

function aFunction(iNum1) {
    if (typeof iNum1 == "number" ){
       alert("number");
    }

    if ( typeof iNum1 == "string") {
        alert("string");
    }
}
aFunction("a");
aFunction(1);
</script>
</head>
<body>
</body>
</html>


typeof 運算子
返回一個用來表示運算式(的)資料類型(的)字串。
typeof[()expression[]] ;
expression 參數是需要尋找類型資訊(的)任意運算式。

typeof 運算子把類型資訊當作字串返回。typeof 傳回值有六種可能: "number," "string," "boolean," "object," "function," 和 "undefined."

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);

執行個體

<html>
<head>
<title>www.111cn.net </title>
<script language="JavaScript">
<!--
var x = "Hello", y;
alert("Variable x value is " + typeof(x));
alert("Variable y value is " + typeof(y));
alert("Variable z value is " + typeof(z));
//  -->
</script>
</head>
<body>

</body>
</html>


看看你會幾個?

如果看了以後,不是很明白的話,請看下面(明白的人就不用往下看了):
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。

執行個體

<html>
    <head>
      <title>Using typeof to determine the type of variables</title>
    <script language="JavaScript1.1">
    <!--
    var bMyVar = true;

    var nMyVar = 35;

    var sMyVar = "This is a string";

    var uMyVar;

    -->
    </script>
    </head>
    <body>
    <script language="JavaScript1.1">
    <!--
    document.writeln("bMyVar = " + typeof(bMyVar));

    document.writeln("<br>nMyVar = " + typeof(nMyVar));

    document.writeln("<br>sMyVar = " + typeof(sMyVar));

    document.writeln("<br>uMyVar = " + typeof(uMyVar));

    -->
    </script>
    </body>
    </html>

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.