簡體   English   中英

typeof typeof x 返回字符串而不是對象,因為 null 的類型是對象

[英]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);

typeof x返回x類型的字符串表示形式。 因此,很自然地, typeof typeof x是字符串。

來自MDN

typeof 運算符返回一個字符串,指示未計算的操作數的類型。

檢查這個簡單的例子,它會消除你的疑慮:

 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM