Kosal Ang
Mon Jan 08 2024
Arrays and objects in JavaScript is fundamental for managing and manipulating data. Here's an overview:
Arrays are ordered collections of values, allowing storage of multiple items under a single variable name. They are defined using square brackets []
.
1// Creating an empty array 2let myArray = [] 3 4// Initializing an array with values 5let numbers = [1, 2, 3, 4, 5] 6let mixedArray = [1, 'two', true, { key: 'value' }] 7
1// Accessing elements 2console.log(numbers[0]) // Output: 1 3console.log(mixedArray[2]) // Output: true 4 5// Modifying elements 6numbers[2] = 10 // Changing the third element to 10 7
JavaScript provides numerous built-in methods to work with arrays:
push()
: Adds elements to the end of an array.pop()
: Removes the last element from an array.shift()
: Removes the first element from an array.unshift()
: Adds elements to the beginning of an array.splice()
: Adds or removes elements from a specific position in an array.Objects are collections of key-value pairs, and they're defined using curly braces {}
.
1// Creating an empty object 2let myObj = {} 3 4// Initializing an object with properties 5let user = { 6 name: 'Kosal', 7 age: 30, 8 isStudent: false, 9} 10
1// Accessing properties 2console.log(user.name) // Output: Kosal 3console.log(user['age']) // Output: 30 4 5// Modifying properties 6user.age = 35 // Changing the age to 35 7user['isStudent'] = true // Changing isStudent to true 8
Objects have methods and operations to work with their properties:
Object.keys()
: Returns an array of an object's keys.Object.values()
: Returns an array of an object's values.Object.entries()
: Returns an array of an object's key-value pairs.You can have arrays that contain objects, creating more complex data structures.
1let users = [ 2 { name: 'Sakni', age: 25 }, 3 { name: 'Bora', age: 30 }, 4 { name: 'Kosal', age: 35 }, 5] 6 7console.log(users[1].name) // Output: Bora 8 9// Loop through the array and output the values. 10for (let user of users) { 11 console.log(`Name: ${user.name}, Age: ${user.age}`) 12} 13
1Bora 2Name: Sakni, Age: 25 3Name: Bora, Age: 30 4Name: Kosal, Age: 35 5
Understanding arrays and objects and how to manipulate them is crucial in JavaScript for storing and organizing data efficiently.
Creating a beautiful menu bar using HTML, CSS, and JavaScript involves creating the structure with HTML
JavaScript uses asynchronous programming. Promises and `async`/`await` are two powerful features that facilitate asynchronous operations in JavaScript
JavaScript's closures and scope are fundamental concepts that significantly impact how code behaves
If you're looking to build a responsive sidebar with icons and expandable menus, this HTML and CSS template can serve as a great starting point
ECMAScript 6, also known as ES6 or ECMAScript 2015, introduced several powerful features that significantly enhanced the capabilities of JavaScript
To get the Monday and Sunday of the week, use the setDate() method to set the day of the month of a date to the Monday and Sunday
Generate all permutations of string and filter unique string by using JavaScript
JavaScript is a versatile scripting language primarily used for client-side web development. It enables interactivity and dynamic content on web pages.
Dropdowns are a feature common to many websites. It's very useful, as they make it easy to show additional data only when it is needed.
TailwindCSS is a popular CSS framework that can help you create nice user interfaces quickly with pre-made CSS classes.