Complex Types
In computer science, an object is a value in memory which is possibly referenced by an identifier.
In JavaScript, almost everything is an Object.
Even functions
are, in fact, also objects with the additional capability of being callable.
Object
In JavaScript, Objects
are the only mutable values.
const person = {
name: 'literank',
age: 30,
isStudent: false,
address: {
street: '123 Main St',
city: 'Apple 🍏 Town',
},
hobbies: ['reading', 'coding', 'traveling']
};
// Accessing properties of the object
console.log(person.name); // Output: literank
console.log(person.address.city); // Output: Apple 🍏 Town
console.log(person.hobbies[1]); // Output: coding
// Modifying properties
person.hobbies.push('gardening');
// Adding a new property
person.jobTitle = 'Software Engineer';
// Deleting a property
delete person.isStudent;
// Iterating over object properties
for (const key in person) {
console.log(`${key}: ${person[key]}`);
}
Array
Arrays
are regular objects for which there is a particular relationship between integer-keyed properties and the length property.
// Define an array of fruits
const fruits = ['apple', 'banana', 'orange', 'grape', 'watermelon'];
// Accessing elements of the array
console.log(fruits[0]); // Output: apple
console.log(fruits[2]); // Output: orange
// Modifying elements
fruits[1] = 'kiwi';
fruits[3] = 'strawberry';
// Adding elements
fruits.push('mango');
fruits.unshift('pineapple');
// Removing elements
fruits.pop(); // Removes the last element (mango)
fruits.shift(); // Removes the first element (pineapple)
// Finding the index of an element
const indexOfOrange = fruits.indexOf('orange');
console.log(indexOfOrange); // Output: 2
// Slicing the array
const slicedFruits = fruits.slice(1, 4);
console.log(slicedFruits); // Output: ['kiwi', 'orange', 'strawberry']
// Iterating over array elements
for (let i = 0; i < fruits.length; i++) {
console.log(fruits[i]);
}
// Using forEach for iteration
fruits.forEach((fruit) => {
console.log(fruit);
});
Set
The Set
object lets you store unique values of any type, whether primitive values or object references.
// Creating a Set
const uniqueNumbers = new Set([1, 2, 3, 4, 5]);
// Adding elements to the Set
uniqueNumbers.add(6);
uniqueNumbers.add(7);
// Trying to add a duplicate element (won't affect the set)
uniqueNumbers.add(6);
// Removing an element from the Set
uniqueNumbers.delete(3);
// Checking if an element exists in the Set
const hasFour = uniqueNumbers.has(4);
console.log(hasFour); // Output: true
// Getting the size of the Set
const setSize = uniqueNumbers.size;
console.log(setSize); // Output: 6
// Iterating over Set elements using forEach
uniqueNumbers.forEach((number) => {
console.log(number);
});
Map
The Map
object holds key-value pairs and remembers the original insertion order of the keys.
// Creating a Map
const employeeMap = new Map();
// Adding key-value pairs to the Map
employeeMap.set('John', 20);
employeeMap.set('Alice', 25);
employeeMap.set('Bob', 30);
// Getting values by key
const johnAge = employeeMap.get('John');
console.log(johnAge); // Output: 20
// Checking if a key exists in the Map
const hasAlice = employeeMap.has('Alice');
console.log(hasAlice); // Output: true
// Deleting a key-value pair from the Map
employeeMap.delete('Bob');
// Checking the size of the Map
const mapSize = employeeMap.size;
console.log(mapSize); // Output: 2
Code Challenge
Write a function
countWords
that takes a string as input and returns a Map where keys are unique words in the string, and values are the frequency of each word.
Treat words case-insensitively.