1

Maybe this question will look silly to you. But I have a quick one.

I'm learning JavaScript these days:

And trying to understand following code.

var name = true; 
console.log(typeof(name));

why the output is string ?

I'm expecting output will be boolean type. Why its returning as a string ?

Satpal
  • 132,252
  • 13
  • 159
  • 168
Tushar Niras
  • 3,654
  • 2
  • 22
  • 24

1 Answers1

0

typeof operator will give us the datatype of the variable in a string format. for example:

var a = false;
console.log(typeof a)
//Result: "boolean"

var b = {};
console.log(typeof b)
//Result: "object"


var name = true;
console.log(typeof name)
//Result: "string"

Every window having predefined variable called "name" and the datatype of this variable is "string". Open new window and in console just give name, it will give empty sting

Rajesh
  • 24,354
  • 5
  • 48
  • 79
Aravinder
  • 503
  • 4
  • 8
  • 1
    This does not answer the question of why passing what looks like a Boolean outputs 'string' – Christopher Ronning Dec 30 '16 at 06:47
  • that's right I guess... as @JaromandaX said it's special variable may be ? and better not to use it.. :/ – Tushar Niras Dec 30 '16 at 06:57
  • @TusharNiras—not a special variable or reserved word, it's a special property of the [*window* object](https://developer.mozilla.org/en/docs/Web/API/Window), which is synonymous with the global object in browsers. Other types of host don't necessarily have such restrictions, it's not part of ECMAScript. – RobG Dec 30 '16 at 07:07