Length of a JavaScript object I have a JavaScript object. Is there a built-in or accepted best practice way to get the length of this object? const myObject = new Object(); myObject["firstname"] = "Gareth"; myObject["lastname"] = "Simpson"; myObject["age"] = 21; that's kinda true, but so many people are used to PHP's "associative array" that they might assume it means "ordered associative map", when JS objects are in fact unordered. In the above example, myObject.length is undefined, at least in a browser environment. That's why it isn't valid Variants of Object.{keys, values, entries}(obj).length have now been mentioned a total of 38 times in 16 answers plus in the comments of this question, and another 11 times in 7 deleted answers. I think that’s enough now. 44 Answers Updated answer Here's an update as of 2016 and widespread deployment of ES5 and beyond. For IE9+ and all other moder...
Archive
Labels
- Anchor
- ASP-Net
- background-color
- C-Sharp
- Caching
- cfml
- CSS
- Debugging
- delicious-api
- DOM
- events
- Firefox
- Fonts
- forms
- Fundamentals
- HTML
- http
- Internet-Explorer
- JavaScript
- JavaScript-kata
- javascript-objects
- jQuery
- keyboard
- mac-address
- Mathematics
- object
- onclick
- Popup
- postback
- Python Computer Programming
- Quotes
- Software Development
- user-interface
- Validation