Intro
Objects are a collection of properties
let james = {
name: 'james';
age: 30;
email: 'james@jamesmail.com';
location: 'Britan';
}
so as you can see james here has multiple properties and that makes him an object.
we can also put strings equal things within objects
let james = {
name: 'james';
age: 30;
email: 'james@jamesmail.com';
location: 'Britan';
"Cup o'": 'joe';
}
Accessing values
to access all the values is not that hard its just
console.log(james)
and all of the values will show up
but lets say you only want to access one of the values and not all of them. well we use dot notaion for that
james.name
james.age
this will show just the age and just the name
Adding Properties to an object
By using dot notation we can add variables
let Guitars = {
Strings: 6;
in stock: 70;
}
guitars.type = "acoustic";
Bracket notation will also work
bracket notation is just dot notation but with different syntax
let Guitars = {
Strings: 6;
in stock: 70;
}
guitar["type"] = "acoustic";
in the same way you can update certain properties and change them with dot and bracket notation
let Guitars = {
Strings: 6;
in stock: 70;
}
guitars.strings= 7;
removing properties
using the keyword "Delete" you can delete certain properties
Property check
Checking for properties within objects is pretty easy with the command
prop in objectName
within this you can check if a object has a certain value
so lets check in james
age in james
this will show up as true because the variable age shows up in the object james
For properties in const we have a strange result
putting
const Guitars = {
Strings: 6;
in stock: 70;
}
guitars.strings= 7;
this will cause an error, trying to change one of the variables but
let Guitars = {
Strings: 6;
in stock: 70;
}
guitars.type= "acoustic";
we can add properties to it with no problem
Top comments (0)