I would like to know if the following javascript is ‘valid’ or not.
var object = {
'name' : 'test',
'age' : 56,
'age' : 25
}
As you can see I deliberately repeated one of the attributes. (age)
For what it matters
A quick test on chrome seems to prove that the javascript is valid and object.age is equal to 25.
p.s.
I am asking this because we have written some javascript code generator and I want to know if that is valid js or not.
Technically it is valid, but not recommended.
According to RFC4627 (emphasis mine):
Granted this says should, so yes you can do it…but it may result in unpredictable behavior depending on who’s parsing it down the road (typically the last property wins because of how most forward-reading parsers behave).
Also note that this RFC applies to JSON not object literals (JSON is a stricter subset of that), but the conventions apply to both.