Today I spent some time exploring three important JavaScript concepts: array map, while loop, and arrow functions. Here’s a quick summary of what I learned.
Array Map
The map() method creates a new array by applying a function to every element in an existing array. It’s super useful when you want to transform data without modifying the original array.
Example:
const numbers = [1, 2, 3, 4];
const doubled = numbers.map(num => num * 2);
console.log(doubled); // [2, 4, 6, 8]
In this example, map() takes each number and multiplies it by 2, returning a new array with the doubled values.
While Loop
A while loop repeatedly executes a block of code as long as a specified condition is true. It’s handy when you don’t know beforehand how many times the loop should run.
Example:
let count = 0;
while (count < 5) {
console.log(count);
count++;
}
This will print numbers from 0 to 4. The loop keeps running until count is no longer less than 5.
Arrow Functions
Arrow functions are a concise way to write functions in JavaScript. They use the => syntax and often make your code cleaner and easier to read.
Example:
const greet = name => `Hello, ${name}!`;
console.log(greet('Alice')); // Hello, Alice!
Arrow functions also have different behavior with this, which can be useful in some cases.
Top comments (0)