Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does console.log(new Map<String, Number>([])) log true, true in JavaScript?

Tags:

javascript

What exactly is causing this TypeScript-like expression to log true, true in JavaScript?

console.log(new Map<String, Number>([]))
like image 441
pbuzz007 Avatar asked Sep 21 '25 07:09

pbuzz007


1 Answers

If you format it differently, you can see that this might look like TS, but it's just JavaScript. Coincidentally, the angle brackets and the comma match perfectly with the syntax for the two arguments of console.log, where each is a comparison, less than and greater than. What is being compared is:

new Map < String // true
Number > [] // true

Written out in a way that makes this more obvious:

console.log(new Map < String, Number > ([]))

If you meant to write this in a TS file where it might get interpreted (as you may have intended) as a type generic, then you are probably looking for different types: string and number, rather than the String and Number constructors that you used in the example.

like image 169
Ben Steward Avatar answered Sep 22 '25 21:09

Ben Steward