-
Objects
- Dot notation (opens in a new tab)
- Bracket notation (opens in a new tab)
- Setting object members (opens in a new tab) In JavaScript, an object is a collection of properties and methods that are grouped together to represent a single entity. Objects can be thought of as a key-value store, where each key represents a property and each value represents its corresponding value. Here's an example of an object in JavaScript:
let person = { name: 'John Doe', age: 30, job: 'Developer', speak: function () { console.log('Hello, my name is ' + this.name); }, };
In the example above,
person
is an object with four properties:name
,age
,job
, andspeak
. Thespeak
property is a function, which is also known as a method. To access the properties of an object, you can use dot notation:console.log(person.name); // "John Doe" console.log(person.age); // 30
You can also access the properties of an object using bracket notation, which allows you to access properties using a string expression:
console.log(person['name']); // "John Doe" console.log(person['age']); // 30
In addition to properties, objects can also have methods, which are functions that are associated with an object. To call a method, you can use dot notation followed by parentheses:
person.speak(); // "Hello, my name is John Doe"
Objects are a fundamental concept in JavaScript and are used to represent real-world objects, such as a person, an event, or a car. Understanding how to create and manipulate objects is essential for any JavaScript programmer.
- Video: "Javascript Objects Explained | Javascript Objects Tutorial" https://www.youtube.com/watch?v=rLPwCAqyCAE (opens in a new tab)
- Article: "About Objects" https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object (opens in a new tab)