typeof typeof x returns string and not object sinc

2020-04-23 03:57发布

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

标签: javascript
3条回答
▲ chillily
2楼-- · 2020-04-23 04:35

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
查看更多
Anthone
3楼-- · 2020-04-23 04:39

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.

查看更多
The star\"
4楼-- · 2020-04-23 04:45

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.

查看更多
登录 后发表回答