Today I Learned

hashrocket A Hashrocket project

Javascript keys are only strings!

You can use squares to access or set a value in an object, but that key is just getting converted to a string. And you can't loop through the keys and expect the keys to be objects... they're strings!

> a = {1: "x"}
{ '1': 'x' }
> a[/x/] = "y"
'y'
> a[1]
'x'
> a[/x/]
'y'
> a['1']
'x'
> a['/x/']
'y'
> for (var i in a) { console.log(i === 1); console.log(i === /x/) }
false
false
false
false
See More #javascript TILs
Looking for help? At Hashrocket, our JavaScript experts launch scalable, performant apps on the Web, Android and iOS. Contact us and find out how we can help you.