
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Finding the length of a JavaScript object
Suppose we have an object like this −
const obj = { name: "Ramesh", age: 34, occupation: "HR Manager", address: "Tilak Nagar, New Delhi", experience: 13 };
We are required to write a JavaScript function on Objects that computes their size (i.e., the number of properties in it).
Example
Following is the code −
const obj = { name: "Ramesh", age: 34, occupation: "HR Manager", address: "Tilak Nagar, New Delhi", experience: 13 }; Object.prototype.size = function(obj) { let size = 0, key; for (key in obj) { if (obj.hasOwnProperty(key)){ size++ }; }; return size; }; const size = Object.size(obj); console.log(size);
This will produce the following output on console −
5
- Related Articles
- Length of a JavaScript object?
- Finding the length of the diagonal of a cuboid using JavaScript
- Finding average word length of sentences - JavaScript
- Finding the length of second last word in a sentence in JavaScript
- Finding the length of longest vowel substring in a string using JavaScript
- Finding length of repeating decimal part in JavaScript
- Finding maximum length of common subarray in JavaScript
- Finding the smallest value in a JSON object in JavaScript
- How to get the length of an object in JavaScript?
- Finding even length numbers from an array in JavaScript
- Sorting JavaScript object by length of array properties.
- Finding the length of words in a given Pandas series
- Finding the character with longest consecutive repetitions in a string and its length using JavaScript
- Finding length, width and height of the sheet required to cover some area using JavaScript
- Finding the ASCII score of a string - JavaScript

Advertisements