Creating and Accessing Objects
In this blog post, we'll dive into various aspects of JavaScript objects and arrays, exploring how to create, access, modify, and combine them. Whether you're a beginner or looking to refresh your knowledge, this guide will provide valuable insights and practical examples.
Creating an Object
In JavaScript, you can create an object using object literal notation. Here’s an example:
javascriptconst obj1 = {
name: "rahul",
surname: "mishra",
age: 23,
branch: "cse",
};
Accessing Object Properties
You can access properties of an object using dot notation or bracket notation:
javascriptconsole.log(obj1.name); // Output: rahul
console.log(obj1.surname); // Output: mishra
Modifying Object Properties
Modifying properties is straightforward:
javascriptobj1.name = "shubham";
console.log(obj1.name); // Output: shubham
Freezing an Object
You can freeze an object using Object.freeze()
, which prevents any modifications:
javascriptObject.freeze(obj1);
obj1.name = "amam"; // This will not change the name
console.log(obj1.name); // Output: shubham
Important Note on Accessing Properties
You can also access properties using bracket notation. This is particularly useful when the property name is stored in a variable:
javascriptconsole.log(obj1["name"]); // Output: shubham
Using Symbols in Objects
Symbols are unique identifiers that can be used as keys in objects. Here's how you can create and use a symbol:
javascriptconst mysmb = Symbol("yo boi");
obj1[mysmb] = "this is the value of the key that's name is mysmb";
console.log(obj1[mysmb]); // Output: this is the value of the key that's name is mysmb
Functions in Objects
You can also add functions as properties of an object:
javascriptobj1.greetings = function () {
console.log("namaste ji");
};
console.log(obj1.greetings()); // Output: namaste ji
Working with Arrays
Creating Arrays
Arrays can be created using array literals:
javascriptconst arr1 = ["rahul", "shubham", "aman"];
const arr2 = ["anjali", "palak", "vanshika", "baby"];
Modifying Arrays with push()
You can add elements to an array using the push()
method:
javascriptconsole.log(arr1.push(arr2)); // Adds arr2 as a new element and returns the new length of arr1.
console.log(arr1); // Output: ['rahul', 'shubham', 'aman', ['anjali', 'palak', 'vanshika', 'baby']]
Combining Arrays with concat()
The concat()
method combines two or more arrays without modifying the original arrays:
javascriptconst arr3 = ["car", "bike", "tank"];
const arr4 = ["aeroplane", "submarine", "helicopter"];
console.log(arr3.concat(arr4)); // Output: ['car', 'bike', 'tank', 'aeroplane', 'submarine', 'helicopter']
Best Practices for Combining Arrays
Using the Spread Operator
The spread operator (...
) is a modern way to combine arrays efficiently:
javascriptconst phones1 = ["iphone", "samsung", "apple"];
const phones2 = ["redmi", "micromax", "vivo"];
const phones3 = ["lava", "red cherry001"];
const allPhones = [...phones1, ...phones2, ...phones3];
console.log(allPhones); // Output: ['iphone', 'samsung', 'apple', 'redmi', 'micromax', 'vivo', 'lava', 'red cherry001']
Checking if a Value is an Array
You can use Array.isArray()
to check if a value is an array:
javascriptconst tellme = Array.isArray("rahul");
console.log(tellme); // Output: false (because it's a string)
Creating Arrays from Strings with Array.from()
The Array.from()
method allows you to create an array from an iterable or array-like structure:
javascriptconst createArr = Array.from("hello ji namaste");
console.log(createArr); // Output: ['h', 'e', 'l', 'l', 'o', ' ', 'j', 'i', ' ', 'n', 'a', 'm', 'a', 's', 't', 'e']
Using Array.of()
The Array.of()
method creates a new array instance from a variable number of arguments:
javascriptconst allArr = Array.of(arr1, arr2, arr3, arr4, phones1, phones2, phones3);
console.log(allArr);
Conclusion
In this blog post, we've covered essential concepts related to JavaScript objects and arrays. From creating and accessing objects to utilizing symbols and functions within them, as well as exploring various methods for working with arrays, these foundational skills will enhance your JavaScript programming capabilities.
Subscribe to my newsletter
Read articles from rahul mishra directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
rahul mishra
rahul mishra
I am learning tech skills and sharing my journey here to make others life a little error free