-1

In node.js

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

in Firefox's web console

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

let name2 = 12; 
console.log(typeof name2);
number 

Why is the difference between var and let in Firefox?

Why is no such difference in node.js?

halfer
  • 19,824
  • 17
  • 99
  • 186
Tim
  • 1
  • 141
  • 372
  • 590

2 Answers2

3

There's an inbuilt property on window called name:

console.log(name);

So your code will look for that, not the name you create.

Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
  • Thanks. Why is `typeof name` not number but string? – Tim May 11 '19 at 00:23
  • Because `window.name` is a string @Tim. – Jack Bashford May 11 '19 at 00:26
  • but `window.name` is just a reference, its type should depend on what it refers to . For comparison, in `node.js`, if I create and initialize `name` to be a string by `var name="hello"`, and then reassign to it a number, by `name=12`, then its type is number not string. – Tim May 11 '19 at 00:41
  • https://stackoverflow.com/questions/56086438/why-cant-i-change-the-type-of-name-in-firefox-web-console – Tim May 11 '19 at 01:13
-1

in my opinion from the output, node.js doesn't start the interpreter's work like the Firefox's web console - meaning in node.js similar to compiled languages it recognises it is a number. in the Firefox's web console it saves "12" as a string to the memory with var or use it only as a number with let without saving it to memory.