Reputation:
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);
Upvotes: 3
Views: 6953
Reputation: 1419
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.
If you want it to return "object" you need to change it to just one 'typeof'
var x=null;
console.log(typeof x);
Upvotes: 8
Reputation: 999
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
Upvotes: 0
Reputation: 104369
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
.
Upvotes: 1