JavaScript Objects

Last Updated Jul 29, 2015, 07:00:14 PM





JavaScript Objects

Objects in Javascript provide a flexible way to keep track of data by associating a name with a particular value. They're used all the time in JavaScript programming using a particular object format called JSON; they're used to transmit data between websites, databases, and web pages

Basic Object Example
Try It Now

The example would first

  • define the website as an object
  • define the website as a blank object
  • define the website.test function
  • call the website.test function with variable "SnoopCode"

The result:

  • An alert message with 'Welcome to SnoopCode' will appear.
  • website.url will be defined as string
  • An alert message with 'SnoopCode' will appear.


What is an Object

Objects in Javascript provide a flexible way to keep track of data by associating a name with a particular value. They're used all the time in JavaScript programming using a specific object format called JSON; they're used to transmit data between web sites, databases, and web pages

Consider this simple object:




Think of an object as a list that contains items, and each item (a property or a method) in the list is stored by a name-value pair. The property names in the example above are firstName and favoriteGame. And the values are “Ricky” and “Cricket.”





As a JavaScript developer, you will most often use the object data type, mostly for storing data and for creating your custom methods and functions.



Creating JavaScript Objects

These are the two common ways to create objects:

The most common and, indeed, the easiest way to create objects is with the object literal described here:

1. Object Literals



2. Object Constructor

The second most common way to create objects is with Object constructor. A constructor is a function used for initializing new objects, and you use the new keyword to call the constructor.





Example
Try It Now

Accessing the Properties of an Object


Example 2
Try It Now

Example 3
Try It Now

Example 4

Try It Now

Example 5
Try It Now

JavaScript objects provide a structured way to store data. An object gives you one location to store a lot of information and it lets you organize that information as easy to access key value pairs. In fact, because objects provide such an excellent way to store and access data, they've become the model for one of the most popular data exchange formats on the web, JSON. JSON stands for JavaScript Object Notation.


JavaScript Objects are Mutable

Objects are mutable: Which means objects can be changed .They are addressed by reference, not by value.

If a is an object, the following statement will not create a copy of b:




The object a is not a copy of b. It is b. Both a and b points to the same object. Any changes to b will also change a because a and b are the same object.

Any changes to a copy of an object will also replace the original.

Example
Try It Now

JavaScript Native Objects

JavaScript has several built-in or native objects. These objects are accessible anywhere in your program and will work the same way in any browser running in any operating system.

Here is the list of all important JavaScript Native Objects −

JavaScript Number Object

JavaScript Boolean Object

JavaScript String Object

Sources and Credits

The source of the content has been referred and updated with Mozilla Developer Network and W3C Organization

Last Updated Jul 29, 2015, 07:00:14 PM



Browser compatibility

Feature
Basic support Yes Yes Yes Yes Yes Yes Yes