Hits: 5
(JavaScript Tutorials for Beginners)
In this end-to-end example, you will learn – JavaScript tutorials for Beginners – JavaScript Objects.
JavaScript Objects
In this tutorial, you will learn about JavaScript objects with the help of examples.
In the JavaScript data types tutorial, you learned about 7 different primitive data types. And here, you are going to learn about the eighth data-type(JavaScript object).
JavaScript object is a non-primitive data-type that allows you to store multiple collections of data.
Note: If you are familiar with other programming languages, JavaScript objects are a bit different. You do not need to create classes in order to create objects.
Here is an example of a JavaScript object.
// object
let student = {
firstName: 'ram',
class: 10
};
Here, student
is an object that stores values such as strings and numbers.
JavaScript Declare Objects
The syntax to declare an object is:
let object_name = {
key1: value1,
key2: value2
}
Here, an object object_name
is defined. Each member of an object is a key: value pair separated by commas and enclosed in curly braces {}
.
For example,
// object creation
let person = {
name: 'John',
age: 20
};
console.log(typeof person); // object
You can also define an object in a single line.
let person = { name: 'John', age: 20 };
In the above example, name
and age
are keys, and John
and 20
are values respectively.
There are other ways to declare an object in JavaScript. To learn more, visit Different ways to declare JavaScript objects.
JavaScript Object Properties
In JavaScript, “key: value” pairs are called properties. For example,
let person = {
name: 'John',
age: 20
};
Here, name: 'John'
and age: 20
are properties.

Accessing Object Properties
You can access the value of a property by using its key.
1. Using dot notation
Here’s the syntax of the dot notation.
objectName.key
For example,
let person = {
name: 'John',
age: 20,
};
// accessing property
console.log(person.name); // John
2. Using bracket notation
Here is the syntax of the bracket notation.
objectName["propertyName"]
For example,
let person = {
name: 'John',
age: 20,
};
// accessing property
console.log(person["name"]); // John
JavaScript Nested Objects
An object can also contain another object. For example,
// nested object
let student = {
name: 'John',
age: 20,
marks: {
science: 70,
math: 75
}
}
// accessing property of student object
console.log(student.marks); // {science: 70, math: 75}
// accessing property of marks object
console.log(student.marks.science); // 70
In the above example, an object student
contains an object value in the marks
property.
JavaScript Object Methods
In JavaScript, an object can also contain a function. For example,
let 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.
A JavaScript method is a property containing a function declaration. In the next tutorial, you will learn about JavaScript Methods in detail.
Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.
Learn by Coding: v-Tutorials on Applied Machine Learning and Data Science for Beginners
Latest end-to-end Learn by Coding Projects (Jupyter Notebooks) in Python and R:
All Notebooks in One Bundle: Data Science Recipes and Examples in Python & R.
End-to-End Python Machine Learning Recipes & Examples.
End-to-End R Machine Learning Recipes & Examples.
Applied Statistics with R for Beginners and Business Professionals
Data Science and Machine Learning Projects in Python: Tabular Data Analytics
Data Science and Machine Learning Projects in R: Tabular Data Analytics
Python Machine Learning & Data Science Recipes: Learn by Coding
R Machine Learning & Data Science Recipes: Learn by Coding
Comparing Different Machine Learning Algorithms in Python for Classification (FREE)
There are 2000+ End-to-End Python & R Notebooks are available to build Professional Portfolio as a Data Scientist and/or Machine Learning Specialist. All Notebooks are only $29.95. We would like to request you to have a look at the website for FREE the end-to-end notebooks, and then decide whether you would like to purchase or not.