The standard structure to store elements accessible by arbitrary keys
(mapping) in Python is called a dict (dictionary) and in JavaScript
— object.
| Python | Javascript |
|---|---|
| Creation of dictionary / object # | |
10
Exception: AttributeError 'dict' object has no attribute 'a' |
10
10
some value
other value
other value |
| Modification of a value in dictionary / object # | |
'hello' |
hello
hello Two ways of access (square brackets and dot-notation) gives the same element. |
| Non-string keys # | |
{5: 'hello', '5': 'world'}
|
{ '5': 'world' }
world Keys of elements in JavaScript object has to be string. Any other key will be coerced to string. |
| Check if element with given key exists # | |
True |
true |
| Access to non-existent key # | |
Exception: KeyError 'c'
None
'Nothing' |
undefined
undefined |
You can access elements of an object either with square brackets or with dot-notation.