Objects in JavaScript

An object is a collection of properties. Property is a relation of a name(key) or value. Objects in javascript can be created using the following methods:

1) By using an object literal:

With the help of object literal, you can create and define an object in one statement.

var computer = { OS: "Windows 10", RAM: "8GB", ROM: "1TB" };

You can also write above object like the following:

var computer = { 
   OS: "Windows 10",
   RAM: "8GB",
   ROM: "1TB"
};

2) By using keyword new:

You can create an object by the new keyword in javascript.

var computer = new Object();
computer.OS = "Windows 10";
computer.RAM = "8GB";
computer.ROM = "1TB";

**Note:

You can create objects in javascript by both methods from above. The result will be the same. But it is best practice to use the first method(using object literal).

Object Properties:

Properties contain key and value pairs. An object is a collection of these properties. You can add, change or delete properties.

Accessing properties:

You can access the values of keys present in properties like the following:

Syntax:

objectName.property

OR

objectName["property"]

Example:

var computer = {
  OS: "Windows 10",
  RAM: "8GB",
  ROM: "1TB"
};
console.log('My computer has operating system ' + computer.OS
             + ', RAM: ' + computer.RAM 
             + ' and ROM: ' + computer.ROM);
console.log('My computer has operating system ' + computer["OS"] 
             + ', RAM: ' + computer["RAM"]
             + ' and ROM: ' + computer["ROM"]);

Output:

My computer has operating system Windows 10, RAM: 8GB and ROM: 1TB
My computer has operating system Windows 10, RAM: 8GB and ROM: 1TB

Adding Properties:

You can add new properties to an existing object.

Example:

var company = {
   name : "MICROSOFT",
   ceo : "Satya Nadella"
};
company.website = "microsoft.com";
console.log(company);

Output:

{name: “MICROSOFT”, ceo: “Satya Nadella”, website: “microsoft.com”}

Object methods:

You can define a method as a value to a key in objects.

Example:

var company = {
  name: "MICROSOFT",
  website: function() {
      return 'microsoft.com';
  }
};

You can access the method of an object with the following syntax:

objectName.methodName();

Example:

var company = {
  name: "MICROSOFT",
  website: function() {
      return 'microsoft.com';
  }
};
console.log(company.website());

Output:

microsoft.com

Built-In Methods:

toLowerCase() method:

You can convert a value to lowercase using this method.

Example:

var company = {
    name : "MICROSOFT",
    ceo : "Satya Nadella"
};
console.log(company.name.toLowerCase());

Output:

microsoft

toUpperCase() method:

You can convert a value to uppercase using this method.

Example:

var company = {
    name : "MICROSOFT",
    ceo : "Satya Nadella"
};
console.log(company.ceo.toUpperCase());

Output:

SATYA NADELLA

Object.create() method:

The Object.create() method is used to create a new object, using an existing object as the prototype of the new object.

Example:

var company = {
    name : "MICROSOFT",
    ceo : "Satya Nadella"
};
const enterprise = Object.create(company);
console.log(enterprise.name);
console.log(enterprise.ceo);

Output:

MICROSOFT
Satya Nadella

Additional Links:

String methods and properties

Arrays in javascript

Functions in javascript

Leave a Reply

Your email address will not be published. Required fields are marked *