JavaScript Objects are the most important data type and form the building blocks for modern JavaScript. The “Object” class represents the JavaScript data types. Objects are quite different from JavaScript’s primitive data types (Number, String, Boolean, null, undefined, and symbol). It is used to store different types of key collections and complex entities. It can be created by using the Object() method or by using object initializer/literal syntax.
Syntax:
objectName.methodName()
Example: Using function definition as property value and accessing with additional details.
Javascript
// Object creation let student = { name: "Martin" , class: "12th" , section: "A" , studentDetails: function () { return this .name + " " + this .class + " " + this .section + " " ; } }; // Display object data console.log( "STUDENT " + student.studentDetails()); |
Output:
STUDENT Martin 12th A
The complete list of JavaScript Object is listed below:
JavaScript Object Properties: A JavaScript property is a member of an object that associates a key with a value.
- Instance Properties: An instance property is a property that has a new copy for every new instance of the class.
Instance Properties |
Description |
Example |
---|---|---|
constructor | Returns a reference to the object constructor function that has created the instance of an object. |
JavaScript Object Methods: JavaScript methods are actions that can be performed on objects.
- Static Methods: If the method is called using the Object class itself then it is called a static method of Object.
Static Methods |
Description |
Example |
---|---|---|
assign() | Copy the values and properties from one or more source objects to a target object. | |
create() | JavaScript does not have the traditional classes seen in other languages. | |
defineProperty() | Defines a new property directly on an object and returns the object. | |
defineProperties() | Defines object a new or modifies existing properties directly on an object and it returns the object. | |
entries() | Return an array consisting of enumerable property [key, value] pairs of the object. | |
freeze() | There is a method Object.freeze() which is used to freeze an object. | |
fromEntries() | Transform a list of key-value pairs into an object. | |
getOwnPropertyDescriptor() | Returns a property descriptor for the own property of a given object. | |
getOwnPropertyNames() | Returns all properties that are present in a given object. | |
getOwnPropertySymbols() | Returns an array of all symbol properties that are present in a given object. | |
getPrototypeOf() | Check the prototype of an object that the user has created. | |
hasOwn() | Checks if a given property exists or not | |
Object.is() | Determine whether two values are the same or not. | |
isExtensible() | Checks whether an object is extensible or not. | |
isFrozen( ) | Determine if an object is frozen or not. | |
isSealed( ) | Determine if an object is sealed or not. | |
keys() | Return an array whose elements are strings corresponding to the enumerable properties. | |
preventExtensions() | Additionally, it prevents the prototype of the item from being changed. | |
seal() | There is a method Object.seal() which is used to seal an object. | |
setPrototypeOf() | The internal [[Prototype]] property of a specified object to another object or null. | |
toLocaleString() | It returns a string representing the object. | |
values() | Return an array whose elements are the enumerable property values found on the object. |
- Instance Methods: If the method is called on an instance of a date object then it is called an instance method.
Instance Methods |
Description |
Example |
defineGetter() | Called when the specified property is looked up | |
hasOwnProperty() | Check whether the object has the specified property as its own property. | |
isPrototypeOf() | Checks if an object exists in another object’s prototype chain. | |
IsEnumerable() | Returns a Boolean indicating whether the specified property is enumerable and is the object’s own property. |