Use the Object.keys()
method, passing the object you want to inspect, to get an array of all the (own) enumerable properties of the object.
Then calculate the length of that array by checking the length
property:
const car = {
color: 'Blue',
brand: 'Ford',
model: 'Fiesta'
}
Object.keys(car).length
I said enumerable properties. This means their internal enumerable flag is set to true, which is the default. Check MDN for more info on this subject.
Download my free JavaScript Beginner's Handbook and check out my JavaScript Course!
More js tutorials:
- Discover JavaScript Timers
- The String toUpperCase() method
- Introduction to ES Modules
- event.stopPropagation vs event.preventDefault() vs. return false in DOM events
- The Lexical Structure of JavaScript
- JavaScript labeled statements
- The String codePointAt() method
- How to iterate over object properties in JavaScript
- How to check if a JavaScript value is an array?