js中typeof的使用方法

來源:互聯網
上載者:User

標籤:

typeof在js中是一個一元操作符,可以判斷運算元的類型,其傳回值為字串,有number、string、object、boolean、function、undefined。使用方式可以是typeof 運算元或typeof(運算元),下面是幾個執行個體,

typeof NaN  傳回值:number  NaN特殊的非數字值

typeof Infinity  傳回值:number  Infinity無窮大特殊值

typeof 123  傳回值:number 

typeof ‘123‘  傳回值:string

typeof(true)  傳回值:boolean

typeof(Date)  傳回值:function

typeof(s)  傳回值:undefined  因為s作為一個變數未被定義,所以這裡的傳回值是undefined

typeof(undefined)  傳回值:undefined

typeof(document)  傳回值:object  document是DOM下的一個對象

typeof(null)  傳回值:object

 

typeof就是來判斷變數是什麼類型的,對傳回值做以下說明,

number 變數是數字類型

string  變數是字串類型

boolean 變數是布爾類型

object  變數是對象或者null

function  變數是函數

undefined  變數未定義

 

可以利用typeof來判斷給定的變數的類型,方便根據不同的類型做不同的處理。

有不正之處歡迎指出,謝謝!

js中typeof的使用方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.