简体   繁体   中英

typeof typeof x returns string and not object since type of null is object

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);

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM