console.log(typeof) in browser and terminal (node)

Farshad :

when you run sample.js file with node in terminal

var name = 12;
console.log( typeof name )
//number

but it's different in browser console

var name = 12;
console.log( typeof name )
//string

why there's difference?!

Matt Ellen :

name is a property of window and a string.

When you set name in the console, it's actually setting the value of window.name which gets converted to a string, as stated in the documentation:

window.name will convert all values to their string representations by using the toString method.

This particularly happens because you are setting name like so:

var name = 12;

Using var name =, in a browser, without any other scoping is the same as writing window.name =.

However, if you were to use const or let to declare name, then name would not apply to the global scope, i.e. window, and it would be of the expected type:

const name = 4;
console.log(typeof name)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=31660&siteId=1