📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
✅ Some premium posts are free to read — no account needed. Follow me on Medium to stay updated and support my writing.
🎓 Top 10 Udemy Courses (Huge Discount): Explore My Udemy Courses — Learn through real-time, project-based development.
▶️ Subscribe to My YouTube Channel (172K+ subscribers): Java Guides on YouTube
To understand and best learning experience, I highly recommended that you open a console (which, in Chrome and Firefox, can be done by pressing Ctrl+Shift+I), navigate to the "console" tab, copy-and-paste each JavaScript code example, and run it by pressing the Enter/Return key.
1. JavaScript Object Properties
Accessing JavaScript Properties
objectName.property
objectName["property"]
objectName[expression]
var user = {
firstName : 'Ramesh',
lastName : 'Fadatare',
emailId : 'ramesh@gmail.com',
age : 29
}
let firstName = user.firstName; // objectName.property
console.log(firstName);
var lastName = user['lastName']; // objectName["property"]
console.log(lastName);
let x = "emailId";
var emailId = user[x]; // objectName[expression]
console.log(emailId);
Adding New Properties
var user = {
firstName : 'Ramesh',
lastName : 'Fadatare',
emailId : 'ramesh@gmail.com',
age : 29
}
user.fullName = user.firstName + " " + user.lastName;
console.log(user.fullName);
Ramesh Fadatare
Deleting Properties
var user = {
firstName : 'Ramesh',
lastName : 'Fadatare',
emailId : 'ramesh@gmail.com',
age : 29
}
delete user.age;
console.log(user);
{firstName: "Ramesh", lastName: "Fadatare", emailId: "ramesh@gmail.com"}
Enumerate the properties of an object
- for...in loops - This method traverses all enumerable properties of an object and its prototype chain
- Object.keys(o) - This method returns an array with all the own (not in the prototype chain) enumerable properties' names ("keys") of an object o.
- Object.getOwnPropertyNames(o) - This method returns an array containing all own properties' names (enumerable or not) of an object o.
var txt = "";
var person = {fname:"Ramesh", lname:"Fadatare", age:29};
var x;
for (x in person) {
txt += person[x] + " ";
}
"Ramesh Fadatare 29 "
const object1 = {
a: 'somestring',
b: 42,
c: false
};
console.log(Object.keys(object1));
["a", "b", "c"]
const object1 = {
a: 1,
b: 2,
c: 3
};
console.log(Object.getOwnPropertyNames(object1));
["a", "b", "c"]
2. Creating new objects
2.1. Using object literals or object initializers
// using Object Literals
var user = {
firstName : 'Ramesh',
lastName : 'Fadatare',
emailId : 'ramesh@gmail.com',
age : 29,
getFullName : function (){
return user.firstName + " " + user.lastName;
}
}
console.log(JSON.stringify(user));
console.log(user.getFullName());
// access object properties
console.log('firstName :', user.firstName);
console.log('lastName :', user.lastName);
console.log('emailId :', user.emailId);
console.log('age :', user.age);
{"firstName":"Ramesh","lastName":"Fadatare","emailId":"ramesh@gmail.com","age":29}
Ramesh Fadatare
firstName : Ramesh
lastName : Fadatare
emailId : ramesh@gmail.com
age : 29
2.2. Using a constructor function
- Define the object type by writing a constructor function. There is a strong convention, with good reason, to use a capital initial letter.
- Create an instance of the object with a new keyword.
// using constructor function
function User(firstName, lastName, emailId, age){
this.firstName = firstName;
this.lastName = lastName;
this.emailId = emailId;
this.age = age;
}
var user1 = new User('Ramesh', 'Fadatare', 'ramesh24@gmail.com', 29);
var user2 = new User('John', 'Cena', 'john@gmail.com', 45);
var user3 = new User('Tony', 'Stark', 'tony@gmail.com', 52);
console.log(user1);
console.log(user2);
console.log(user3);
User {firstName: "Ramesh", lastName: "Fadatare", emailId: "ramesh24@gmail.com", age: 29}
User {firstName: "John", lastName: "Cena", emailId: "john@gmail.com", age: 45}
User {firstName: "Tony", lastName: "Stark", emailId: "tony@gmail.com", age: 52}
2.3. Using the Object.create() method
// using Object.create Method
var Employee = {
firstName : 'Ramesh',
lastName : 'Fadatare',
emailId : 'ramesh@gmail.com',
age : 29,
getFullName : function (){
return user.firstName + " " + user.lastName;
}
}
var employee1 = Object.create(Employee);
// access object properties
console.log('firstName :', employee1.firstName);
console.log('lastName :', employee1.lastName);
console.log('emailId :', employee1.emailId);
console.log('age :', employee1.age);
firstName : Ramesh
lastName : Fadatare
emailId : ramesh@gmail.com
age : 29
// Animal properties and method encapsulation
var Animal = {
type: 'Invertebrates', // Default value of properties
displayType: function() { // Method which will display type of Animal
console.log(this.type);
}
};
// Create new animal type called animal1
var animal1 = Object.create(Animal);
animal1.displayType(); // Output:Invertebrates
// Create new animal type called Fishes
var fish = Object.create(Animal);
fish.type = 'Fishes';
fish.displayType(); // Output:Fishes
2.4. Using ES 6 Class
class Employee {
constructor(firstName, lastName, emailId, age){
this.firstName = firstName;
this.lastName = lastName;
this.emailId = emailId;
this.age = age;
}
getFullName(){
return this.firstName + " " + this.lastName;
}
getFirstName(){
return this.firstName;
}
}
const employee = new Employee('Ramesh', 'Fadatare', 'ramesh@gmail.com', 29);
console.log(employee.getFirstName());
console.log(employee.getFullName());
class Rectangle {
constructor(height, width) {
this.height = height;
this.width = width;
}
// Getter
get area() {
return this.calcArea();
}
// Method
calcArea() {
return this.height * this.width;
}
}
const square = new Rectangle(10, 10);
console.log(square.area); // 100
3.JavaScript Object Methods
var user = {
firstName : 'Ramesh',
lastName : 'Fadatare',
emailId : 'ramesh@gmail.com',
age : 29,
getFullName : function (){
return this.firstName + " " + this.lastName;
}
}
console.log(JSON.stringify(user));
console.log(user.getFullName());
{"firstName":"Ramesh","lastName":"Fadatare","emailId":"ramesh@gmail.com","age":29}
Ramesh Fadatare
Accessing Object Methods
objectName.methodName()
var user = {
firstName : 'Ramesh',
lastName : 'Fadatare',
emailId : 'ramesh@gmail.com',
age : 29,
getFullName : function (){
return user.firstName + " " + user.lastName;
}
}
console.log(user.getFullName());
Ramesh Fadatare
Using Built-In Methods
var message = "Hello world!";
var x = message.toUpperCase();
HELLO WORLD!
Adding a Method to an Object
var user = {
firstName : 'Ramesh',
lastName : 'Fadatare',
emailId : 'ramesh@gmail.com',
age : 29,
getFullName : function (){
return user.firstName + " " + user.lastName;
}
}
user.getFirstName = function(){
return this.firstName;
}
console.log(user.getFirstName());
Ramesh
4. JavaScript Accessors (Getters and Setters)
4.1 JavaScript Getter (The get Keyword)
var person = {
firstName: "John",
lastName : "Doe",
language : "en",
get lang() {
return this.language;
}
};
console.log(person.lang);
en
JavaScript Setter (The set Keyword)
var person = {
firstName: "John",
lastName : "Doe",
language : "",
set lang(lang) {
this.language = lang;
}
};
// Set an object property using a setter:
person.lang = "en";
console.log(person.language);
en
Comments
Post a Comment
Leave Comment