Loading Please Wait...

Logo Lynxsia IT Solutions

JavaScript Object Introduction

JS Object Introduction

In JavaScript, objects are king. If you understand objects, you understand JavaScript.

JavaScript Objects

In JavaScript, almost "everything" is an object.

  • Booleans can be objects (if defined with the new keyword)
  • Numbers can be objects (if defined with the new keyword)
  • Strings can be objects (if defined with the new keyword)
  • Dates are always objects
  • Maths are always objects
  • Regular expressions are always objects
  • Arrays are always objects
  • Functions are always objects
  • Objects are always objects

Objects are variables too. But objects can contain many values.

The values are written as name:value pairs (name and value separated by a colon). It is a common practice to declare objects with the const keyword.

					 
        
          const user = { name:"John Doe", email:"john@gmail.com" };
        
      
Creating a JavaScript Object

With JavaScript, you can define and create your own objects. There are different ways to create new objects.

  • Create a single object, using an object literal.
  • Create a single object, with the keyword new.
  • Define an object constructor, and then create objects of the constructed type.
  • Create an object using Object.create().
Using an Object Literal

This is the easiest way to create a JavaScript Object.

Using an object literal, you both define and create an object in one statement.

An object literal is a list of name:value pairs (like age:50) inside curly braces {}.

					 
        
          const user = { name:"John Doe", email:"john@gmail.com" };

          // OR
          const user = {
            name:"John Doe",
            email:"john@gmail.com"
          };
        
      
Using the JavaScript Keyword new

You can create an object using new Object().

					 
        
          const user = new Object();
          user.name = "John Doe";
          user.email = "john@gmail.com";
        
      

There is no need to use new Object(). For readability, simplicity and execution speed, use the object literal method.

					 
        
          const user = {};
          user.name = "John Doe";
          user.email = "john@gmail.com";
        
      
JavaScript Objects are Mutable

Objects are mutable. They are addressed by reference, not by value.

					 
        
          const x = user;  // Will not create a copy of user.
        
      

The object x is not a copy of user. It is user. Both x and user are the same object.

Any changes to x will also change user, because x and user are the same object.

					 
        
          const user = { name:"John Doe", email:"john@gmail.com" };
          const x = user;
          x.email = "john@gmail.com";      // Will change both x.email and user.email
        
      

How you feel about this blog:

Share this blog on:

Report Us

If you find any error in the turtorials, or want to share your suggestion/feedback, feel free to send us email at: info@lynxsia.com

Contact Us
Ads
Logo
Lynxsia IT Solutions

We are concern with various development process like website design & development, E-commerce development, Software development, Application development, SMS & Bulk SMS Provider, PWA Development, and many more..

Kotwali Road, Chhiptehri, Banda, 210001, UP, India

Copyright © 2022, Lynxsia IT Solutions, All rights reserved