Javascript Objects Intro
Javascript Objects Intro
Javascript Objects Intro
In this tutorial, you will learn about JavaScript objects with the help of
examples.
// object
const student = {
firstName: 'ram',
class: 10
};
const object_name = {
key1: value1,
key2: value2
}
For example,
// object creation
const person = {
name: 'John',
age: 20
};
console.log(typeof person); // object
In the above example, name and age are keys, and John and 20 are
values respectively.
let person = {
name: 'John',
age: 20
};
objectName.key
For example,
const person = {
name: 'John',
age: 20,
};
// accessing property
console.log(person.name); // John
objectName["propertyName"]
For example,
const person = {
name: 'John',
age: 20,
};
// accessing property
console.log(person["name"]); // John
// nested object
const student = {
name: 'John',
age: 20,
marks: {
science: 70,
math: 75
}
}
const person = {
name: 'Sam',
age: 30,
// using function as a value
greet: function() { console.log('hello') }
}
person.greet(); // hello
Here, a function is used as a value for the greet key. That's why we
need to use person.greet() instead of person.greet to call the function
inside the object.