JavaScript Object Examples: Learn Objects Step by Step
Learn JavaScript objects through hands-on examples. You'll create objects, read and change their properties, dig into nested objects, and use methods with confidence.

Contents

In our JavaScript lessons, we had learned about data types. Now let’s start with JavaScript Object Examples and Lessons. JavaScript objects are a non-primitive data type that lets you store multiple pieces of data.
Note: If you have some knowledge of other programming languages, JavaScript objects are a little different from their use in other software languages. You don’t need to create a class to create a JavaScript object.
JavaScript Object Example.
// object
const student = {
ad: 'serhat',
soyad: 'alpaslan'
};
JavaScript Object Declaration (JS Object Declaration)
const nesne_adi= {
keyA: value1,
keyB: value2
}
Here we defined a JavaScript object and named it "nesne_adi". The values inside the object, that is, between the curly braces, are called key and value.
// object creation
const nesne_adi= {
ad: 'John',
soyad: 'alpaslan'
};
console.log(typeof nesne_adi); // you'll get object as output in the console
Let’s Examine JavaScript Object Properties
const nesne_adi= {
keyA: value1,
keyB: value2
}
Our object above is named "nesne_adi", and keyA : value1 and keyB : value2 are the properties of this object.
How Do We Access Object Properties?
- Access with dot notation:
objectName.key
const nesne_adi= {
keyA: value1,
keyB: value2
}
// We can access the value in the properties as follows.
console.log(nesne_adi.keyA); // The output will be value1.
2. Access with bracket notation:
objectName["propertyName"]
const nesne_adi= {
keyA: value1,
keyB: value2
} // We can access the value in the properties as follows.
console.log(nesne_adi[keyA]); // The output will be value1.
JavaScript Nested Objects and How Do We Access Them?
JavaScript objects can contain new objects, and their usage is very similar to the examples above.
// nested object
const nesne_adi= {
adi: 'SERHAT',
soyadi: 'ALPASLAN',
adres: {
sokak_adi: 'ABC SOKAK',
apartman_adi: 'MNO APARTMANI'
}
}
// the properties of the nesne_adi object can be accessed as follows
console.log(nesne_adi.adres); // {sokak_adi: 'ABC SOKAK', apartman_adi: 'MNO APARTMANI'}
// the properties of the adres object inside the nesne_adi object can be accessed as follows
console.log(nesne_adi.adres.sokak_adi); // ABC SOKAK
JavaScript Object Methods
JavaScript objects can also contain functions.
const kisi = {
ad: 'Sam',
soyad: 30,
// let's use a function as a value
merhaba : function() {
console.log('merhaba')
}
}
kisi.merhaba(); // merhaba
Here, we wrote a function for the merhaba key value. Therefore, to call this function inside the JavaScript object, we needed to use kisi.merhaba() instead of kisi.merhaba. In other words, when calling functions inside an object, we need to use the object name, followed by the key value, and parentheses.
- JavaScript Comparisons and Logical Operators
- JS Errors – try and catch and finally examples
- JavaScript do-while loop examples
- Easy nested for loop with JavaScript
- javascript break-continue usage
- Official website
Share it if it helped


