Javascript Objects & Maps

Yasin DALKILIÇ
3 min readFeb 17, 2023

--

hello everyone, in this article we will talk about javascript objects and maps.

JavaScript Objects and Maps

JavaScript is a versatile and popular programming language that is used for building a wide range of web applications. One of the most powerful features of JavaScript is its ability to work with objects and maps. In this article, we’ll explore the basics of JavaScript objects and maps, as well as some advanced concepts that will help you take your coding to the next level.

JavaScript Objects

In JavaScript, an object is a collection of key-value pairs. Each key is a string, and each value can be any JavaScript data type, including another object. Objects are defined using curly braces, like this:

let person = {
name: 'John',
age: 30,
address: {
street: '123 Main St',
city: 'Anytown',
state: 'CA'
}
};

JavaScript Objects and Maps: A Comprehensive Guide

JavaScript is a versatile and popular programming language that is used for building a wide range of web applications. One of the most powerful features of JavaScript is its ability to work with objects and maps. In this article, we’ll explore the basics of JavaScript objects and maps, as well as some advanced concepts that will help you take your coding to the next level.

JavaScript Objects

In JavaScript, an object is a collection of key-value pairs. Each key is a string, and each value can be any JavaScript data type, including another object. Objects are defined using curly braces, like this:

let person = {
name: 'John',
age: 30,
address: {
street: '123 Main St',
city: 'Anytown',
state: 'CA'
}
};

In this example, person is an object that has three key-value pairs: name, age, and address. The address key points to another object that has three key-value pairs of its own. To access the values of an object, you can use dot notation or square bracket notation:

console.log(person.name); // 'John'
console.log(person['age']); // 30
console.log(person.address.street); // '123 Main St'

You can also add, update, or delete key-value pairs in an object using dot or bracket notation:

person.email = 'john@example.com'; // add a new key-value pair
person.age = 31; // update an existing value
delete person.address; // remove a key-value pair

JavaScript Maps

A map is a data structure that allows you to store key-value pairs and quickly retrieve the value associated with a given key. In JavaScript, maps are represented by the Map class. Here's an example:

let map = new Map();
map.set('name', 'John');
map.set('age', 30);
map.set('address', {
street: '123 Main St',
city: 'Anytown',
state: 'CA'
});

In this example, map is a Map object that has three key-value pairs. To retrieve the value associated with a key, you can use the get method:

console.log(map.get('name')); // 'John'
console.log(map.get('age')); // 30
console.log(map.get('address').street); // '123 Main St'

You can also add, update, or delete key-value pairs in a map using the set, set, and delete methods:

map.set('email', 'john@example.com'); // add a new key-value pair
map.set('age', 31); // update an existing value
map.delete('address'); // remove a key-value pair

Maps are particularly useful when you need to associate complex or arbitrary data with a key. For example, you could use a map to store metadata about a file:

let fileMetadata = new Map();
fileMetadata.set('filename', 'document.pdf');
fileMetadata.set('size', 1024);
fileMetadata.set('created', new Date());
fileMetadata.set('tags', ['important', 'confidential']);

My Youtube Channel.

Good work everyone.

--

--

Yasin DALKILIÇ
Yasin DALKILIÇ

Written by Yasin DALKILIÇ

Hi, My name is Yasin I am a Software Developer, I love so much researching and development 😊 Here is my youtube channel @webciyasin

No responses yet