A JavaScript object is a fundamental data structure used to store collections of key-value pairs, where keys are strings (or Symbols) and values can be any data type, including functions known as methods. These objects act as containers that group related properties (data) and actions together, allowing developers to represent real-world entities like users, cars, or courses in a structured way.
Creation and Structure
Objects are most commonly created using object literal syntax with curly braces {}, though they can also be instantiated via the Object() constructor, Object.create(), or constructor functions.
Properties store data (e.g.,
name: "John"), while methods are functions that perform actions (e.g.,greet: function() {...}).Objects are mutable, meaning you can add, change, or delete properties after creation.
They support nested structures, allowing objects to contain other objects as values.
Access and Manipulation
You can interact with object data using dot notation (e.g., object.property) for simple keys or bracket notation (e.g., object["key"]) for dynamic names, spaces, or special characters.
To iterate over an object, use loops like
for...in, or utility methods such asObject.keys(),Object.values(), andObject.entries().Nearly all objects inherit properties and methods from
Object.prototype, though null-prototype objects (created viaObject.create(null)) can be used to avoid inherited properties and prevent prototype pollution.
How do you create a JavaScript object?
How do you create an object in JavaScript?
What does 'this' mean in JavaScript objects?
Videos
You can use Object.getOwnPropertyNames() to get all properties that belong to an object, whether enumerable or not. For example:
console.log(Object.getOwnPropertyNames(Math));
//-> ["E", "LN10", "LN2", "LOG2E", "LOG10E", "PI", ...etc ]
You can then use filter() to obtain only the methods:
console.log(Object.getOwnPropertyNames(Math).filter(function (p) {
return typeof Math[p] === 'function';
}));
//-> ["random", "abs", "acos", "asin", "atan", "ceil", "cos", "exp", ...etc ]
In ES3 browsers (IE 8 and lower), the properties of built-in objects aren't enumerable. Objects like window and document aren't built-in, they're defined by the browser and most likely enumerable by design.
From ECMA-262 Edition 3:
Global Object
There is a unique global object (15.1), which is created before control enters any execution context. Initially the global object has the following properties:• Built-in objects such as Math, String, Date, parseInt, etc. These have attributes { DontEnum }.
• Additional host defined properties. This may include a property whose value is the global object itself; for example, in the HTML document object model the window property of the global object is the global object itself.As control enters execution contexts, and as ECMAScript code is executed, additional properties may be added to the global object and the initial properties may be changed.
I should point out that this means those objects aren't enumerable properties of the Global object. If you look through the rest of the specification document, you will see most of the built-in properties and methods of these objects have the { DontEnum } attribute set on them.
Update: a fellow SO user, CMS, brought an IE bug regarding { DontEnum } to my attention.
Instead of checking the DontEnum attribute, [Microsoft] JScript will skip over any property in any object where there is a same-named property in the object's prototype chain that has the attribute DontEnum.
In short, beware when naming your object properties. If there is a built-in prototype property or method with the same name then IE will skip over it when using a for...in loop.
It's not possible with ES3 as the properties have an internal DontEnum attribute which prevents us from enumerating these properties. ES5, on the other hand, provides property descriptors for controlling the enumeration capabilities of properties so user-defined and native properties can use the same interface and enjoy the same capabilities, which includes being able to see non-enumerable properties programmatically.
The getOwnPropertyNames function can be used to enumerate over all properties of the passed in object, including those that are non-enumerable. Then a simple typeof check can be employed to filter out non-functions. Unfortunately, Chrome is the only browser that it works on currently.
function getAllMethods(object) {
return Object.getOwnPropertyNames(object).filter(function(property) {
return typeof object[property] == 'function';
});
}
console.log(getAllMethods(Math));
logs ["cos", "pow", "log", "tan", "sqrt", "ceil", "asin", "abs", "max", "exp", "atan2", "random", "round", "floor", "acos", "atan", "min", "sin"] in no particular order.
I'm a beginner to javascript and I am currently learning objects. The examples that have been provided are mostly about games. I understand that objects are used to hold related data but how can this be functional in web development? I am still learning objects and this might make sense later on but I am just curious if someone can explain it.