(Progress persistence is disabled until Phase 9)
Array Methods
Manipulate array data.
Learning Objectives
- push, pop, map, filter
Array Methods
Welcome to this lesson on Array Methods. JavaScript is the programming language of the web, and array methods is a fundamental piece of that ecosystem.
Explanation
When your browser loads a web page, the HTML creates the structure, the CSS creates the style, and JavaScript creates the interactivity. JavaScript can update both HTML and CSS in real-time.
Code Examples
Here is a simple snippet demonstrating this concept:
const numbers = [1, 2, 3];
const doubled = numbers.map(n => n * 2);
const evens = numbers.filter(n => n % 2 === 0);
How to loop through an array in JavaScript
A very common question when learning JavaScript is how to loop through an array. Let’s look at a few JavaScript array methods examples:
JavaScript Array Map Example
The map method lets you transform every element in an array and returns a new array:
const prices = [10, 20, 30];
const withTax = prices.map(price => price * 1.2);
console.log(withTax); // [12, 24, 36]
Explanation of Code
In the example above, we define the logic required to implement the concept, and then execute it using built-in JavaScript methods.
Common Mistake: Watch out for syntax errors! Missing a bracket or a semicolon can sometimes break your entire script.
Try It Yourself
Put this into practice! The BrowserCode runtime allows you to execute JavaScript securely in your browser.
Key Takeaways
- JavaScript makes pages interactive.
- Always check your browser console for errors.