Learn Simpli

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

Object in Javascript

Introduction
  1. JavaScript has an object entity with a combination of properties
  2. An object has the key-value pair
  3. Every element has a 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
//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
Accessing property
  1. You can only access the already assigned properties
  2. 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
Key points
  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
Ways create an object
  1. With the help of object initializers: You can start creating an object by using the literal notation
  2. With the help of the constructor function: You can create an object with the new keyword with the constructor
  3. Using Object.create()
// With the help of object initializers
var Employee = {
    name: 'Name',
    email: 'name@gmail.com'
}
console.log(Employee.name);
//Output
//Name

//With the help of constructor function
// 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

// Using Object.create()
// 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