As we know in Javascript there are two different datatypes - Primitive Datatype like string, number, boolean, undefined, null, Symbol(introduced in ES6) and Reference Datatype like object.
Primitive datatypes are immutable(cannot be changed) and values contain only a single thing (a string or a number etc) whereas Objects are mutable(can be changed).
let person = {name : "Alex", age: 27 };
console.log(person);
output:
let person = new Object();
person.name = "Alex";
person.age = 27;
console.log(person);
output:
Generally, property names are kept as string but it can be a string or a number.e.g.
Primitive datatypes are immutable(cannot be changed) and values contain only a single thing (a string or a number etc) whereas Objects are mutable(can be changed).
What is Object?
An object is an unordered list of data types, stored as name-value(or key-value) pairs. Each item in the list is called a property (and functions are called methods).e.g.let person = {name : "Alex", age: 27 };
console.log(person);
output:
let person = new Object();
person.name = "Alex";
person.age = 27;
console.log(person);
output:
Generally, property names are kept as string but it can be a string or a number.e.g.
let person = { name : "Alex", 34 : "age", "Year of graduation": 2014 };
NOTE : If the property name is a number or string with space, it has to be accessed with the bracket notation.
Is object a Reference datatype?
An object, is a reference data type. Variables(e.g. person) that are assigned a reference value(means object) are given a reference or a pointer to the location in memory. The variables don’t actually store the value.This is the reason why objects are called reference datatype.
Also because of reference type, when a object is changed, all its referenced objects gets changed and vice-versa.e.g.
let person = { name : "Alex" , age : 27 };
let man = person;
console.log(man);
Also because of reference type, when a object is changed, all its referenced objects gets changed and vice-versa.e.g.
let person = { name : "Alex" , age : 27 };
let man = person;
console.log(man);
// Now if we change the property of man,it will change person object and vice-versa
man.name = "Chris";
console.log(man);
console.log(person);
man.name = "Chris";
console.log(man);
console.log(person);
Similarly if we change person,it will reflect in man also.
NOTE : To avoid this we can use spread operator introduced in ES - 6.
let man = {...person};
Assign new property
let lynda = {...person,sex : "female" };
Reassign value to existing property
let marrie = {...lynda, name : "Marrie"};
let person = { name : "Alex" , age : 27 };
console.log(person.name); // Alex
person.name = "Alex Voucher";
If we try to access a property which doesn't exist for an object we get undefined value for it.
console.log(person.profession); // undefined
person.profession = "Engineering";
console.log(person);
The key or property name of object can be a string, number or string with spaces. In case of property name with single word can be accessed with dot-notation but property with number or string with space can be accessed with bracket-notation.
let person = { name : "Alex" , age : 27 , "year of birth" : 1990 , 3 : "graduation degree"};
console.log(person["name"]); // Alex
console.log(person["year of birth"]); // 1990
console.log(person[3]); // graduation degree
person["profession"] = "Engineering";
console.log(person);
Object as a property
let person = { name : "Alex" , age : 27 ,
address : {
city : "New Delhi",
country : "India"}
};
console.log(person.address.country); // India
Deleting a property
delete person.age; // true
console.log(person.age); // undefined
So in this article we have covered the basics of Javascript object. Hope this article was helpful. Subscribe and leave your comments to support.
Thank you!!
NOTE : To avoid this we can use spread operator introduced in ES - 6.
Creating object from another object
let person = { name : "Alex" , age : 27 };let man = {...person};
Assign new property
let lynda = {...person,sex : "female" };
Reassign value to existing property
let marrie = {...lynda, name : "Marrie"};
Accessing properties of object
The properties of an object can be accessed in two ways:- Dot Notation
- Bracket Notation
let person = { name : "Alex" , age : 27 };
console.log(person.name); // Alex
person.name = "Alex Voucher";
If we try to access a property which doesn't exist for an object we get undefined value for it.
console.log(person.profession); // undefined
person.profession = "Engineering";
console.log(person);
The key or property name of object can be a string, number or string with spaces. In case of property name with single word can be accessed with dot-notation but property with number or string with space can be accessed with bracket-notation.
let person = { name : "Alex" , age : 27 , "year of birth" : 1990 , 3 : "graduation degree"};
console.log(person["name"]); // Alex
console.log(person["year of birth"]); // 1990
console.log(person[3]); // graduation degree
person["profession"] = "Engineering";
console.log(person);
Object as a property
let person = { name : "Alex" , age : 27 ,
address : {
city : "New Delhi",
country : "India"}
};
console.log(person.address.country); // India
Deleting a property
delete person.age; // true
console.log(person.age); // undefined
So in this article we have covered the basics of Javascript object. Hope this article was helpful. Subscribe and leave your comments to support.
Thank you!!
Easy to understand.
ReplyDeleteI found it helpful. kindly write on hoisting and closure as well.