Learn Simpli

Free Online Tutorial For Programmers, Contains a Solution For Question in Programming. Quizzes and Practice / Company / Test interview Questions.

What is an object in Javascript

Before creating an object in javascript, let’s understand what is an object in javascript and its behavior

What is an object in Javascript

  1. JavaScript has an object entity with a combination of properties.
  2. An object has the key-value pair.
  3. Every element has the name which is called key
  4. Objects help to group together different variable that belongs to the same person or entity.
  5. In object, the order doesn’t matter
  6. The object in javascript can be created using the keyword Object
  7. Every object in the javascript has properties associated with it.
  8. Properties of objects can be accessed with dot notation (.)
  9. You can only access the already assigned properties.
  10. If you try to access the properties those are not been assigned it will return undefined

Let’s consider below code snippet that creates an employee object with employee attributes

Create an employee object and assign properties:

//Employee object

var Employee = new Object();

Employee.name = 'Name';

Employee.designation = 'CEO';

Employee.email = 'name@gmail.com';

Access property of employee object:

var Employee = new Object();

Employee.name = 'Name';

Employee.designation = 'CEO';

Employee.email = 'name@gmail.com';

console.log(Employee.name);
//Output
//Name

You can only access the already assigned properties. If you try to access the properties those are not been assigned it will return undefined

var Employee = new Object();

Employee.name = 'Name';

Employee.designation = 'CEO';

Employee.email = 'name@gmail.com';

console.log(Employee.address);
//Output
//undefined

Points to be considered while creating an object and accessing:

  1. Object property name should be a valid string or
  2. Object property name should be anything that can be converted to a string
  3. Object property can not start with a number and if you want to use the number or string with space as property name then use the below format
var Employee = new Object();

Employee.name = 'Name';

Employee.designation = 'CEO';

Employee.email = 'name@gmail.com';

Employee['address of residence'] = 'The complete address';

console.log(Employee['address of residence']);
//Output
//The complete address

How many ways can you create an object in javascript?

We can create an object in javascript in the below ways

1) With the help of object initializers: You can start creating an object by using the literal notation.

var Employee = {

    name:'Name',
    
    email:'name@gmail.com'
}

console.log(Employee.name);
//Output
//Name

2) With the help of constructor function: You can create an object with the new keyword with constructor

function Person(name, age, sex) {
    
    this.name = name;
    
    this.age = age;
    
    this.sex = sex;
}

var Person1 = new Person('Rajani', 33, 'M');
console.log(Person1.name);
// output
// Rajani

3) Using Object.create(): You can also create an object by using the method Object.create(). Object.create() method accepts the proto object which has to be inherited. This method is very useful when you want to inherit methods from the other objects.

var employee = {
    netPayable:function(){
        return ' Employee salary details:\n Name: '+this.name+'\n Tax : '+this.tax+'\n Payable amount is : '+(this.salary - this.tax);
    }
}

// employee 1
var stark = Object.create(employee);
stark.name = 'Stark';
stark.tax = 200;
stark.salary = 20000;
var starkPayableSalary = stark.netPayable();
console.log(starkPayableSalary);

// employee 2
var mickel = Object.create(employee);
mickel.name = 'Mickel';
mickel.tax = 400;
mickel.salary = 30000;
var mickelPayableSalary = mickel.netPayable();
console.log(mickelPayableSalary);

// output
// Employee salary details:
// Name: Stark
// Tax : 200
// Payable amount is : 19800

// Employee salary details:
// Name: Mickel
// Tax : 400
// Payable amount is : 29600

 

Also, read What is apply in javascript?