Learn JavaScript

JavaScript Objects

Understanding JavaScript Objects Using Real World Example

Real world objects includes things like” your mobile phone, television, and washing machine etc. Each and every object has two important characteristic first one is the state/properties and the next one is their behavior/methods.

Example: A television has properties like its color and weight, and methods like on and off.

Note: Different television may have same properties, but the value of properties vary from television to television. Similarly different television may have same methods, but the performance of methods may be time dependent.

JavaScript Objects

In the universe of softwares, objects are modelled after the real world objects. That means such objects also possess these two characteristic, states and behavior. In a programming language objects maintain its state and characteristics in terms of variables and methods respectively.

Like C++ and Java, JavaScript is also an object oriented programming language. A programming language is called object oriented, if it provides the capabilities like: Aggregation, Encapsulation, Inheritance, and Polymorphism to the programmers.

We already discussed about the variables in earlier tutorial. A variable is nothing but a place holder, that is used to hold a value. Just like a variable, an object is also used as a placeholder, but it has capability of holding more than one value.

JavaScript Objects Properties

Properties of an object can be any of the available data types, or any of the abstract data type like an another object. As we have already mentioned, the properties of an object are nothing but the variables, these variables are used internally in the methods of an object.

Standard Syntax of JavaScript Objects

object_name.object_property = property_value;

Example

// Object Creation
var myTv = new object();
myTv.company = 'Samsung';
myTv.model = 'S-3826';
myTv.year = 2021;

Note: The object written above can also be written using an Object Initializer (the values of an object are written as a pair of name and value like: name:value (colon is used as a separator), each value is separated by comma separator).

Example

// Writting objects using Object Initializer
var user = {name: "Johney", age: 18, tutorial: "JavaScript", cd: 7};

Example: Program to understand the JavaScript Objects.

<html>
<body>
<h2>Program to understand the JavaScript Objects</h2>
<script>
var myTv = {name: "Samsung", year: 2020, model: "S-36762", warranty_in_years: 7};
var detail = "";
var x
for(x in myTv)
{
detail += myTv[x] + "<br>";
}
document.write(detail);
</script>
</body>
</html>

Output

Program to understand the JavaScript Objects

Samsung
2020
S-36762
7


Audience

This JavaScript tutorial is designed to help the beginners as well as the professional developers. For better understanding the language of the tutorial is kept simple and easy to understand.

Prerequisites

This tutorial demands to have the basic knowledge of the programming languages such as c or c++, HTML, loops for the better understanding of the JavaScript programming language. The other basic requirement to learn JavaScript through this tutorial is the basic knowledge of computer and information technology so you can easily acquire a better understanding JavaScript programming.

Community Helping

In case you find any kind of error like typing error, spelling mistakes, incorrect grammar, incorrect information or any other mistakes. Please feel free to write to us at: akyadav@etutororg.com.