JavaScript - Objects
Javascript objects are a fundamental building block in the language and are used to model real-world objects in a program. They are a collection of properties, each of which has a name (key) and a value. The properties can be of any data type, including numbers, strings, arrays, functions, or even other objects.
Creating an object in Javascript is done by defining a set of properties within curly braces {}. For example:
let person = {
name: "John Doe",
age: 32,
job: "Software Engineer",
skills: ["JavaScript", "Node.js", "React"]
};
You can access and modify the properties of an object using dot notation or square bracket notation. For example:
console.log(person.name); // John Doe
person.age = 33;
console.log(person["age"]); // 33
In addition to its properties, an object can also have methods, which are functions that belong to the object. Methods can be used to perform actions related to the object. For example:
let person = {
name: "John Doe",
age: 32,
job: "Software Engineer",
skills: ["JavaScript", "Node.js", "React"],
introduce: function() {
console.log(`Hi, my name is ${this.name} and I am a ${this.job}.`);
}
};
person.introduce();
// Hi, my name is John Doe and I am a Software Engineer.
Javascript objects are dynamic and can be modified at runtime. You can add, delete, or modify properties and methods of an object. You can also create new objects based on existing objects using the Object.create() method.
In summary, Javascript objects provide a powerful and flexible way to model and manage data in your applications. They allow you to organize and structure your data in a way that makes it easy to understand and manipulate.