Array methods
- Array from() Method: It converts array-like or iterable objects into a new array with optional mapping function.
- Array keys() Method: It provides an iterator for array indices, allowing iteration over keys/indices in an array.
- Array find() Method: It locates and returns the first array element satisfying a provided condition.
- Array findIndex() Method: It retrieves the index of the first array element satisfying a given condition.
Example: In this example we are using the above mentioned methods one by one.
Javascript
const languages = [ "HTML" , "CSS" , "JavaScript" , "React.js" ]; // Using Array.from() to create a new array of lengths const lengths = Array.from(languages, lang => lang.length); console.log(lengths); // Outputs: [4, 3, 10, 7] // Using Array.keys() to iterate over array indices for (const index of languages.keys()) { console.log(`Index ${index}: ${languages[index]}`); } // Using Array.find() to find the first language // with length greater than 5 const longLanguage = languages.find(lang => lang.length > 5); console.log(longLanguage); // Outputs: "JavaScript" // Using Array.findIndex() to find the index of "React.js" const reactIndex = languages.findIndex( lang => lang === "React.js" ); console.log(reactIndex); // Outputs: 3 |
Output
[ 4, 3, 10, 8 ] Index 0: HTML Index 1: CSS Index 2: JavaScript Index 3: React.js JavaScript 3
JS 2015 or ECMAScript 6 (ES6)
JS 2015 (ES6) also known as ECMAScript 6 (ES6), ECMAScript 6 (ES6) is a significant update to JavaScript, introducing arrow functions, classes, template literals, let and const for variable declaration, enhanced object literals, destructuring, and more modern features for better code organization and readability.