Online Training for Asp.net MVC Web Designing Development, MS SQL, Digital Marketing

JavaScript object basics syntax

What is Object JavaScript?

Creating JavaScript object and using them in code is actually very organized way of writing JavaScript, it’s kind of clean code practice.

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

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

Creating JavaScript instance using new keyword
As an example here 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 basics syntax

JavaScript Tutorial

Group Training