Free Web Development Online Tutorials, Learn to Code
Javascript object attributes, javascript object to json, javascript object add property

Create Javascript Object, Object Keys

What is Object in JavaScript?

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.

<script>
var student = new Object();
</script>

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.

Learn JavaScript Objects with Examples

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

<script>
var student = new Object();
student.Name = "Abantika";
student.addess = "Kormongla, Bangalore";
student.subject = "Javascript";
document.write(student.Name + " of " + student.addess );
</script>
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

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

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

<script>
_stu = {name: "Deepika", address: "Bangalore", subject: "JavaScript"}
document.write(_stu.name + " " + _stu.address + " " + _stu.subject);
</script> 
    

Javascript Object Keys Example

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

Object.keys(obj);

We just need to pass an object in keys method

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

Object.keys(student);

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

    Object.keys(student).length

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) {
    this.name = name || ""; //Public attribute default value is null
    this.city = 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);


Comment
Name
Email
Website
Subscribe