![](/img/trans.png)
[英]typeof (Array, null) returns object and typeof(null, Array) returns function
[英]typeof typeof x returns string and not object since type of null is object
我是js
新手,正在嘗試學習js
,你們能告訴我為什么typeof typeof x
返回string
,在下面提供代碼片段,如果我理解這個簡單的概念,它將對我有更多幫助:
var x=null;
console.log(typeof typeof x);
檢查這個簡單的例子,它會消除你的疑慮:
var a = null; console.log(typeof a, typeof a === 'object') var b = function (){}; console.log(typeof b, typeof b === 'function') var c = ""; console.log(typeof c, typeof c === 'string')
原因: typeof
返回一個字符串,屬於您提供的值的類型,當您檢查typeof
返回的值時,它將是字符串形式,例如:
'object', 'function', 'string' etc.
您正在檢查typeof "object"
,這就是它返回string
的原因。
typeof 運算符來查找 JavaScript 變量的數據類型 // 這從 JavaScript 開始就存在 typeof null === 'object';
var x=null;
var x=(typeof x);
it returns "object";
var y=typeof "object";
it returns string
so
console.log(typeof typeof x);
show string
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.