Javascript gotcha ! (if you come from Python)

All keys are strings (unlike in Python)

Even though this is a basic, got me really hard times debugging my code after writing a lot in python, where this statement is not true. So if you have background in Python, make sure to be aware of following !!!

When iterating keys of dictionary / object in this manner

Regardless of what type key you think should be, it’s going to be string. Consider following

Outputs

Moreover, even thought you used key of type Number, you can still address it with String. Like this:

In case you would expect this to ouput “Undefined” twice, you are wrong, because the output is

To put in contrast with python dictionary, in python things works differently. In python, the type of the used key is also used to has the value. So you can’ to followin in JavaScript, but can in Python

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *