javascript typeof操作符

來源:互聯網
上載者:User

標籤:

typeof操作符:

(1) 檢測給定變數的資料類型;

(2) 對一個值使用typeof操作符可能返回下列某個字串:

"undefined"——如果這個值未定義;

"boolean"——如果這個值是布爾值;

"string"——如果這個值是字串;

"number"——如果這個值是數值;

"object"——如果這個值是對象或null;

"function"——如果這個值是函數;

var person=new Object();

function sayHello(){console.log("hello");}

 

console.log(typeof(a)); //undefined

console.log(typeof(true)); //boolean

console.log(typeof("123")); //string

console.log(typeof(123)); //number

console.log(typeof(person)); //object

console.log(typeof(sayHello)); //function

(3) typeof是一個操作符而不是函數,因此圓括弧儘管可以使用,但不是必需的。

(4) typeof null會返回"object",因為特殊值null被認為是一個空的對象引用。

(5) 瀏覽器差異:Safari5及之前版本、Chrome7及之前版本在對Regex調用typeof操作符時會返回"function",而其他瀏覽器在這種情況下會返回"object"。

javascript 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.