Objects in JavaScript
JavaScript / Tips

What are Objects in JavaScript?

Objects in JavaScript

Hi, today I am going to teach you about Objects in JavaScript.

In JavaScript all the values can be an object except Primitive Values. Booleans, Numbers, Strings, Dates, Maths, Regular expressions, Arrays, Functions can be an object.

Note: Booleans, Numbers and Strings can be objects after defining with the new keyword.

Objects can be a variable too and contain many values which can be written as name and value seperated by a colon.

Create a Javascript Object

You can define and create objects in javascript using Object Literal, the new keyword or define an object constructor. So let’s try to understand all these methods one by one.

Object Literal: This is the easiest way to create objects in javascript. You can define and create an object in a single statement using Object Literal method. Here is an example for you.

Example:

var users = {firstName:"Dheeraj", lastName:"Verma", age:"27"};

The Keyword new: We can also create an object by using the new keyword. Check the example below for better understanding.

Example:

var users = new object ();
users.firstName="Dheeraj";
users.lastName="Verma";
users.age=27;

Constructor Function: We can use constructor function when we need to create multiple objects with same data.

Example:

function user(name, designation) {
  this.fullName = name;
  this.designation = designation;
}
var userdetails = new user("Dheeraj Verma", "Full Stack Web Developer");
console.log(userDetails.fullName + ", " + userDetails.designation);

Thanks for visiting.

Author

I am a Full Stack Web Developer. I have more than 8 years of experience in the web design and development industry.

Leave a Reply

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