# Object (https://wesbos.com/javascript/08-data-types/objects)
```javascript
// how Object define
// implicitily = {} load faster
// explicitily = new Object() work same but load slower then implicitily
//The syntax to declare an object is:
const object_name = {
key1: value1,
key2: value2
}
The values of an object can be any type (string, function, boolean, other objects, arrays etc).
const car = {}
// define property
// 1. initial
const car = {
name: 'BMW'
}
// 2. after initial
const car = {
name: 'BMW'
}
//Accessing Properties Method
//1-dot notation
//2-square brackets
//
//
// dot notation
car.color = "blue";
// square brackets notation
car['color'] = "blue";
console.log(wes.age);
console.log(wes["age"]);
// define method
// 1. initial
const car = {
run: function() { /* code */ }
}
// 2. after initial
// dot notation
car.stop = function() { /* code */ }
// square brackets notation
car['stop'] = function() { /* code */ }
// get property
// dot notation
console.log(car.color)
// square brackets notation
console.log(car['color'])
// get method
// dot notation
car.stop()
// square brackets notation
car['stop']()
const bike = {
"name-asd": "Honda",
nameAd: "Honda 2"
}
agar kisi property ko '', _,number ya space ke sath dete hai tab usko string ke ander dete hai like
"name-asd" or usko []ke ander dete hai dot ke sath nahi.
bike["name-asd"];
const car = {
name: 'BMW',
color: 'RED'
}
console.log(car)
delete car.name;
console.log(car)
const vehical = {
...car,
run: function() {}
}
console.log(vehical);
agar car ko spread oprator ke sath dete hai or car se kuch property delete karte hai tab wo vehical se delete nahi karega.
delete car.color;
console.log(vehical);
For example let's say you added one more property to the persons object
const person ={
name: "wes",
age,
"cool-dude": true
});
Similarly you can also have spaces and numbers in your properties like so but in under string ''.
const person = new Object({
name:'wes',
age,
'cool-dude': true,
'really cool dude': false,
'777': true,
};
//nested objects
//For example, you can create a property called clothing and assign it a sub object like so
const person ={
name: "wes",
age,
"cool-dude": true,
"really cool": false,
"777": true,
dog: "snickers",
clothing: {
shirts: 10,
pants: 2
}
};
//JavaScript Object Properties
In JavaScript, "key: value" pairs are called properties. For example,
let person = {
name: 'John',
age: 20
};
const age = 100;
const person ={
name: "wes",
age
};
That is the exact same thing as doing age: age.
//You can add new properties to an object even after it has been created, using the dot notation.
const person ={
name: "wes",
age:100,
};
For example if we wanted to add a job property to the person object, you could simply add this line of code 👇
person.job = 'Web Developer';
Similarly if you were trying to overwrite a property, you could do that as well.
For example, is you add this line person.age = 50; and then refresh the HTML page and look at the console, you will see that age is now set to 50 even though when creating the object, we set it to the variable age which was 100.
```
1-Objects are used for collections of data or collections of functionality.
2- the order doesn't matter in an object.
example:--
const person = {
first: "wes",
last: "bos",
age: 100,
};
but output come
age: 100,
first: "wes",
last: "bos",
Objects are used for when the order of the properties does not matter.
3- To access the properties, there is a couple of different ways we can do it.
a) dot notation
if you ever want to make an object so it cannot be changed, you could do that with Object.freeze().
The word in programming that we use to describe something that cannot be changed is immutable. Mutation is changing a value.
example:-- wesFroze.age = 100