admin管理员组文章数量:1293984
I am new to js
, trying to learn js
, can you guys tell me why typeof typeof x
returns string
, providing code snippet below, if i understand this simple concept it will help me more:
var x=null;
console.log(typeof typeof x);
I am new to js
, trying to learn js
, can you guys tell me why typeof typeof x
returns string
, providing code snippet below, if i understand this simple concept it will help me more:
var x=null;
console.log(typeof typeof x);
Share
Improve this question
edited Apr 17, 2018 at 1:40
Cœur
38.7k26 gold badges204 silver badges277 bronze badges
asked Apr 2, 2017 at 16:59
user7805277user7805277
0
3 Answers
Reset to default 8typeof x
returns a string representation of the type of x
. So, naturally, typeof typeof x
is string.
From MDN:
The typeof operator returns a string indicating the type of the unevaluated operand.
If you want it to return "object" you need to change it to just one 'typeof'
var x=null;
console.log(typeof x);
Check this simple example, it will clear your doubt:
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')
Reason: typeof
returns a string, of the type of the value you provided, When you check the value returned by typeof
, it will be in string form, like:
'object', 'function', 'string' etc.
And you are checking the typeof "object"
, that's why it returned string
.
typeof operator to find the data type of a JavaScript variable // This stands since the beginning of 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
本文标签: javascripttypeof typeof x returns string and not object since type of null is objectStack Overflow
版权声明:本文标题:javascript - typeof typeof x returns string and not object since type of null is object - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741592055a2387185.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论