Understanding JavaScript Objects FAST!
Everything You've Ever Wanted to Know About Javascript Objects
Table of contents
No headings in the article.
In JavaScript, an object is a collection of key-value pairs, similar to a dictionary in other programming languages.
Objects are used to store and organize data and are often used to represent real-world entities, such as a user or a product.
let person = {
firstName: "diki",
lastName: "dev",
age: 32,
occupation: "Web Developer"
};
You can access the values stored in an object using the dot notation or the bracket notation:
console.log(person.firstName); // Output: "diki"
console.log(person["lastName"]); // Output: "dev"
You can also add, update or delete key-value pair to an object using the assignment operator (=) and delete keyword.
JavaScript objects are also very powerful because they can be used as prototypes for other objects, allowing you to create complex and dynamic data structures.
person.address = "Dublin, Ireland";
console.log(person.address); // Output: "Dublin, Ireland"
person.address = "Johannesburg, South Africa";
console.log(person.address); // Output: "Johannesburg, South Africa"
delete person.age;
console.log(person.age); // Output: undefined
In JavaScript, an object is a collection of properties, where each property has a name and a value. Objects are a fundamental part of the language and are used to model real-world objects and concepts. They are similar to objects in other programming languages, such as dictionaries or hash tables.
Properties in JavaScript can be any type of value, including strings, numbers, booleans, functions, or even other objects. Properties can be added, removed, or modified after an object has been created, making objects a very versatile and powerful data structure.
JavaScript objects are created using object literals, which are denoted by curly braces {}. Properties can be added to an object literal by including a name-value pair, separated by a colon:
let myObject = {
property1: "value1",
property2: "value2",
property3: 3
};
Alternatively, objects can be created using the Object
constructor:
let myObject = new Object();
myObject.property1 = "value1";
myObject.property2 = "value2";
myObject.property3 = 3;
JavaScript also has a concept of "prototype inheritance", where objects can inherit properties from other objects. This is implemented using the prototype property, which all objects have by default. A prototype object can be set to any other object, and when properties are accessed on an object that do not exist, JavaScript will look to its prototype to see if the property exists there.
JavaScript objects also come with some built-in methods, such as Object.keys()
, Object.values()
, Object.entries()
and many more that can be used to manipulate and interact with objects.
Overall, objects in JavaScript are powerful and flexible data structures that are widely used in the language, and understanding how to work with them is a fundamental part of programming with JavaScript.
Let me know your thoughts in the comments below!
Bye, for now! ๐ Diki