Join Class

Create Object in JavaScript

Like any other object oriented programming language we can create object in JavaScript programming too, though JavaScript don’t support all four pillars of OOPs, but still we can do many things with JavaScript object.

In this tutorial you will learn how to create JavaScript object, JavaScript object constructor, JavaScript object to array, object properties etc

What is JavaScript Object?

Creating JavaScript Object and using them in code is actually very organized way of writing JavaScript, it’s kind of clean code practice. you can create JavaScript object to string, and convert a javascript object to json.

Here we learn how many ways we can create a JavaScript object and use them with function or any other way.

var student = new Object();

Objects in JavaScript are just collections of key-value pairs. The values can have any number of methods and properties, and may contain different JavaScript data types, like numbers, strings, date, Boolean etc.

JavaScript Objects Property Example

We can create object in JavaScript in three different ways.

  • Creating instance of Object using new keyword
  • Creating by an object constructor (using new keyword)
  • Creating by object literal
Add property to javascript object

Here in example I am creating a student object with few properties, then writing them on webpage

var student = new Object();
student.Name = "Abantika";
student.addess = "Kormongla, Bangalore";
student.subject = "Javascript";
document.write(student.Name + " of " + student.addess );

JavaScript Object constructor Example

Here we see how we can set property values at the time of creating a new instance of an student object

function getStudent(name, addess, subject) { = name;
this.addess = addess;
this.subject = subject;
var _student = new getStudent("Ajit", "EM Pass, Kolkata", "JSON");
document.write( + " of " + _student.addess );
JavaScript object literal examples

Object literal is like storing values with Key-value pair , this will be very useful when we learn JSON

_stu = {name: "Deepika", address: "Bangalore", subject: "JavaScript"}
document.write( + " " + _stu.address + " " + _stu.subject);
Javascript Object Keys Example

Object.keys() returns enumerable properties, here is the syntax for Object.keys()


We just need to pass an object in keys method

let student = {
    Name : "Abantika",
    addess : "Kormongla, Bangalore",
    subject : "Javascript"

If you want to get the length of an Object in JavaScript, for example we want to get the length of above student object

Stringify a JavaScript Object

We can convert a JavaScript Object to JSON object by using Stringify method

 studentObject = {
    Name : "Abantika",
    addess : "Kormongla, Bangalore",
    subject : "Javascript"
Use JavaScript function JSON.stringify() to convert to a string.
var myJSONobj = JSON.stringify(studentObject);
JavaScript Object Example

Here is another example of creating JavaScript object using function keyword

var Employee = function (name, city, salary) { = name || ""; //Public attribute default value is null = city || "";  
    this.salary = salary || 1000; 
// Create Employee class object
var emp1 = new Employee("Aratri","Kolkata",3000);
// Create Employee class object
var emp2 = new Employee("Bristi","Bangalore",3500);

Above way of creating JavaScript object may be more interesting, because while creating a new instance of the object, you may feel like passing parameter values to constructor like we do in any other object oriented programming.

JavaScript programming tutorials to learn JavaScript coding step by step, develop website using JavaScript and html.
Advanced JavaScript
JavaScript object Example
javascript Interview Questions Answers
JavaScript Examples | JavaScript Online Course