DEV Community

avinash-repo
avinash-repo

Posted on

100 methods string and array es6

Certainly, here is a list of 100 terms related to ES6 array methods and string methods, along with brief explanations:

ES6 Array Methods:

  1. forEach: Iterates over array elements, invoking a callback for each.
  2. map: Creates a new array by applying a function to each element.
  3. filter: Returns a new array with elements that meet a specified condition.
  4. find: Returns the first array element that satisfies a testing function.
  5. findIndex: Returns the index of the first array element that satisfies a testing function.
  6. some: Checks if at least one element satisfies a condition.
  7. every: Checks if all elements satisfy a condition.
  8. reduce: Applies a function to reduce the array to a single value.
  9. includes: Checks if an array contains a specific element.
  10. indexOf: Returns the index of the first occurrence of a specified element.

Certainly, let's compare the usage of these array methods in both ES6 (ECMAScript 2015) and traditional JavaScript.

  1. forEach:
  • ES6:

     const array = [1, 2, 3];
     array.forEach(element => console.log(element));
    
  • Traditional:

     var array = [1, 2, 3];
     array.forEach(function(element) {
       console.log(element);
     });
    
  1. map:
  • ES6:

     const array = [1, 2, 3];
     const newArray = array.map(element => element * 2);
    
  • Traditional:

     var array = [1, 2, 3];
     var newArray = array.map(function(element) {
       return element * 2;
     });
    
  1. filter:
  • ES6:

     const array = [1, 2, 3, 4, 5];
     const filteredArray = array.filter(element => element > 2);
    
  • Traditional:

     var array = [1, 2, 3, 4, 5];
     var filteredArray = array.filter(function(element) {
       return element > 2;
     });
    
  1. find:
  • ES6:

     const array = [1, 2, 3, 4, 5];
     const foundElement = array.find(element => element > 2);
    
  • Traditional:

     var array = [1, 2, 3, 4, 5];
     var foundElement = array.find(function(element) {
       return element > 2;
     });
    
  1. findIndex:
  • ES6:

     const array = [1, 2, 3, 4, 5];
     const foundIndex = array.findIndex(element => element > 2);
    
  • Traditional:

     var array = [1, 2, 3, 4, 5];
     var foundIndex = array.findIndex(function(element) {
       return element > 2;
     });
    
  1. some:
  • ES6:

     const array = [1, 2, 3, 4, 5];
     const hasSome = array.some(element => element > 2);
    
  • Traditional:

     var array = [1, 2, 3, 4, 5];
     var hasSome = array.some(function(element) {
       return element > 2;
     });
    
  1. every:
  • ES6:

     const array = [1, 2, 3, 4, 5];
     const allMeetCondition = array.every(element => element > 0);
    
  • Traditional:

     var array = [1, 2, 3, 4, 5];
     var allMeetCondition = array.every(function(element) {
       return element > 0;
     });
    
  1. reduce:
  • ES6:

     const array = [1, 2, 3, 4, 5];
     const sum = array.reduce((accumulator, element) => accumulator + element, 0);
    
  • Traditional:

     var array = [1, 2, 3, 4, 5];
     var sum = array.reduce(function(accumulator, element) {
       return accumulator + element;
     }, 0);
    
  1. includes:
  • ES6:

     const array = [1, 2, 3];
     const hasElement = array.includes(2);
    
  • Traditional:

     var array = [1, 2, 3];
     var hasElement = array.indexOf(2) !== -1;
    
  1. indexOf:
- ES6 (Note: `indexOf` is the same in both ES6 and traditional):
Enter fullscreen mode Exit fullscreen mode
  ```javascript
  const array = [1, 2, 3];
  const index = array.indexOf(2);
  ```
Enter fullscreen mode Exit fullscreen mode
- Traditional:
Enter fullscreen mode Exit fullscreen mode
  ```javascript
  var array = [1, 2, 3];
  var index = array.indexOf(2);
  ```
Enter fullscreen mode Exit fullscreen mode

// forEach
// ES6:
const array = [1, 2, 3];
array.forEach(element => console.log(element));
// Output: 1
// 2
// 3

// Traditional:
var array = [1, 2, 3];
array.forEach(function(element) {
console.log(element);
});
// Output: 1
// 2
// 3

// map
// ES6:
const array = [1, 2, 3];
const newArray = array.map(element => element * 2);
// Output: newArray: [2, 4, 6]

// Traditional:
var array = [1, 2, 3];
var newArray = array.map(function(element) {
return element * 2;
});
// Output: newArray: [2, 4, 6]

// filter
// ES6:
const array = [1, 2, 3, 4, 5];
const filteredArray = array.filter(element => element > 2);
// Output: filteredArray: [3, 4, 5]

// Traditional:
var array = [1, 2, 3, 4, 5];
var filteredArray = array.filter(function(element) {
return element > 2;
});
// Output: filteredArray: [3, 4, 5]

// find
// ES6:
const array = [1, 2, 3, 4, 5];
const foundElement = array.find(element => element > 2);
// Output: foundElement: 3

// Traditional:
var array = [1, 2, 3, 4, 5];
var foundElement = array.find(function(element) {
return element > 2;
});
// Output: foundElement: 3

// findIndex
// ES6:
const array = [1, 2, 3, 4, 5];
const foundIndex = array.findIndex(element => element > 2);
// Output: foundIndex: 2

// Traditional:
var array = [1, 2, 3, 4, 5];
var foundIndex = array.findIndex(function(element) {
return element > 2;
});
// Output: foundIndex: 2

// some
// ES6:
const array = [1, 2, 3, 4, 5];
const hasSome = array.some(element => element > 2);
// Output: hasSome: true

// Traditional:
var array = [1, 2, 3, 4, 5];
var hasSome = array.some(function(element) {
return element > 2;
});
// Output: hasSome: true

// every
// ES6:
const array = [1, 2, 3, 4, 5];
const allMeetCondition = array.every(element => element > 0);
// Output: allMeetCondition: true

// Traditional:
var array = [1, 2, 3, 4, 5];
var allMeetCondition = array.every(function(element) {
return element > 0;
});
// Output: allMeetCondition: true

// reduce
// ES6:
const array = [1, 2, 3, 4, 5];
const sum = array.reduce((accumulator, element) => accumulator + element, 0);
// Output: sum: 15

// Traditional:
var array = [1, 2, 3, 4, 5];
var sum = array.reduce(function(accumulator, element) {
return accumulator + element;
}, 0);
// Output: sum: 15

// includes
// ES6:
const array = [1, 2, 3];
const hasElement = array.includes(2);
// Output: hasElement: true

// Traditional:
var array = [1, 2, 3];
var hasElement = array.indexOf(2) !== -1;
// Output: hasElement: true

// indexOf
// ES6 (Note: indexOf is the same in both ES6 and traditional):
const array = [1, 2, 3];
const index = array.indexOf(2);
// Output: index: 1

// Traditional:
var array = [1, 2, 3];
var index = array.indexOf(2);
// Output: index: 1

These examples demonstrate the usage of array methods in both ES6 and traditional JavaScript syntax. ES6 introduces arrow functions and enhanced syntax for better readability and conciseness.

  1. lastIndexOf: Returns the index of the last occurrence of a specified element.
  2. sort: Sorts array elements based on a compare function.
  3. reverse: Reverses the order of array elements.
  4. slice: Extracts a portion of an array into a new array.
  5. splice: Changes the contents of an array by removing or replacing existing elements.
  6. concat: Combines two or more arrays.
  7. isArray: Checks if an object is an array.
  8. from: Creates a new array from an iterable object.
  9. of: Creates a new array with variable arguments.
  10. entries: Returns an iterable containing key/value pairs for array entries.

Certainly, let's provide example code for each of the additional array methods you've listed in both ES6 and traditional JavaScript.

// lastIndexOf
// ES6:
const array = [1, 2, 3, 4, 2, 5];
const lastIndex = array.lastIndexOf(2);
// Output: lastIndex: 4

// Traditional:
var array = [1, 2, 3, 4, 2, 5];
var lastIndex = array.lastIndexOf(2);
// Output: lastIndex: 4

// sort
// ES6:
const array = [3, 1, 4, 1, 5, 9, 2, 6];
const sortedArray = array.sort((a, b) => a - b);
// Output: sortedArray: [1, 1, 2, 3, 4, 5, 6, 9]

// Traditional:
var array = [3, 1, 4, 1, 5, 9, 2, 6];
var sortedArray = array.sort(function(a, b) {
  return a - b;
});
// Output: sortedArray: [1, 1, 2, 3, 4, 5, 6, 9]

// reverse
// ES6:
const array = [1, 2, 3, 4, 5];
const reversedArray = array.reverse();
// Output: reversedArray: [5, 4, 3, 2, 1]

// Traditional:
var array = [1, 2, 3, 4, 5];
var reversedArray = array.reverse();
// Output: reversedArray: [5, 4, 3, 2, 1]

// slice
// ES6:
const array = [1, 2, 3, 4, 5];
const slicedArray = array.slice(1, 4);
// Output: slicedArray: [2, 3, 4]

// Traditional:
var array = [1, 2, 3, 4, 5];
var slicedArray = array.slice(1, 4);
// Output: slicedArray: [2, 3, 4]

// splice
// ES6:
const array = [1, 2, 3, 4, 5];
const splicedArray = array.splice(2, 2, 6, 7);
// Output: splicedArray: [3, 4]

// Traditional:
var array = [1, 2, 3, 4, 5];
var splicedArray = array.splice(2, 2, 6, 7);
// Output: splicedArray: [3, 4]

// concat
// ES6:
const array1 = [1, 2, 3];
const array2 = [4, 5, 6];
const concatenatedArray = array1.concat(array2);
// Output: concatenatedArray: [1, 2, 3, 4, 5, 6]

// Traditional:
var array1 = [1, 2, 3];
var array2 = [4, 5, 6];
var concatenatedArray = array1.concat(array2);
// Output: concatenatedArray: [1, 2, 3, 4, 5, 6]

// isArray
// ES6:
const isArrayES6 = Array.isArray([1, 2, 3]);
// Output: isArrayES6: true

// Traditional:
var isArrayTraditional = Array.isArray([1, 2, 3]);
// Output: isArrayTraditional: true

// from
// ES6:
const iterable = 'hello';
const arrayFromIterable = Array.from(iterable);
// Output: arrayFromIterable: ['h', 'e', 'l', 'l', 'o']

// Traditional:
var iterable = 'hello';
var arrayFromIterable = Array.from(iterable);
// Output: arrayFromIterable: ['h', 'e', 'l', 'l', 'o']

// of
// ES6:
const arrayOf = Array.of(1, 2, 3);
// Output: arrayOf: [1, 2, 3]

// Traditional (Note: No direct equivalent in traditional JavaScript, Array.of is an ES6 feature):
var arrayOfTraditional = [1, 2, 3];
// Output: arrayOfTraditional: [1, 2, 3]

// entries
// ES6:
const array = ['a', 'b', 'c'];
const entriesIterator = array.entries();
// Output: entriesIterator: Object [Array Iterator] {}

// Traditional (Note: No direct equivalent in traditional JavaScript, entries is an ES6 feature):
var array = ['a', 'b', 'c'];
var entriesIteratorTraditional = array.entries();
// Output: entriesIteratorTraditional: Object [Array Iterator] {}
Enter fullscreen mode Exit fullscreen mode

These examples demonstrate the usage of the additional array methods in both ES6 and traditional JavaScript syntax.

  1. keys: Returns an iterable containing keys for array entries.
  2. values: Returns an iterable containing values for array entries.
  3. copyWithin: Copies a sequence of array elements within the array.
  4. fill: Fills array elements with a static value.
  5. flat: Flattens nested arrays.
  6. flatMap: Maps each element using a function and flattens the result.
  7. Symbol.iterator: Symbol used to define an iterator method.
  8. Array.from: Creates a new array from an array-like or iterable object.
  9. Array.of: Creates a new array with the given elements.
  10. Array.prototype[@@unscopables]: Symbol used to exclude array methods from the 'with' statement.
  11. ArrayBuffer: A binary data buffer.
  12. TypedArray: An array-like view for handling binary data with specific data types.
  13. ArrayBuffer.prototype.slice: Creates a new ArrayBuffer with a portion of the original.
  14. ArrayBuffer.isView: Checks if an object is a TypedArray view.
  15. ArrayBuffer.prototype.byteLength: Returns the length (in bytes) of an ArrayBuffer.
  16. ArrayBuffer.prototype.byteOffset: Returns the byte offset of a TypedArray from the start of its ArrayBuffer.
  17. ArrayBuffer.prototype.constructor: Returns the function that created an object's prototype.
  18. ArrayBuffer.prototype.toString: Returns a string representation of an ArrayBuffer.
  19. ArrayBuffer.prototype.slice: Extracts a portion of an ArrayBuffer into a new ArrayBuffer.

ES6 String Methods:

  1. charAt: Returns the character at a specified index.
  2. charCodeAt: Returns the Unicode value of the character at a specified index.
  3. concat: Combines two or more strings.
  4. includes: Checks if a string contains another string.
  5. endsWith: Checks if a string ends with a specified value.
  6. startsWith: Checks if a string starts with a specified value.
  7. indexOf: Returns the index of the first occurrence of a specified value.
  8. lastIndexOf: Returns the index of the last occurrence of a specified value.
  9. match: Searches a string for a specified pattern and returns the matches.
  10. matchAll: Returns an iterator of all matches for a specified pattern.
  11. replace: Replaces a specified value or pattern with another value.
  12. search: Searches a string for a specified value or pattern.
  13. slice: Extracts a portion of a string and returns it as a new string.
  14. split: Splits a string into an array of substrings based on a specified delimiter.
  15. substring: Extracts characters between two specified indices.
  16. toLowerCase: Converts a string to lowercase.
  17. toUpperCase: Converts a string to uppercase.
  18. trim: Removes whitespace from both ends of a string.
  19. padStart: Pads a string with a specified character until it reaches a specified length.
  20. padEnd: Pads a string's end with a specified character until it reaches a specified length.
  21. repeat: Returns a new string by repeating the original string a specified number of times.
  22. normalize: Returns the Unicode Normalization Form of a string.
  23. String.raw: Returns a raw string representation of a template literal.
  24. String.fromCharCode: Returns a string created by using the specified sequence of Unicode values.
  25. String.fromCodePoint: Returns a string created by using the specified Unicode code points.
  26. String.prototype.codePointAt: Returns a non-negative integer representing the Unicode code point at a given position.
  27. String.prototype.includes: Checks if a string contains another string.
  28. String.prototype.endsWith: Checks if a string ends with a specified value.
  29. String.prototype.startsWith: Checks if a string starts with a specified value.
  30. String.prototype.repeat: Returns a new string repeated a specified number of times.
  31. String.prototype.trimStart: Removes whitespace from the beginning of a string.
  32. String.prototype.trimEnd: Removes whitespace from the end of a string.
  33. String.prototype.trimLeft: Removes whitespace from the beginning of a string.
  34. String.prototype.trimRight: Removes whitespace from the end of a string.
  35. String.prototype[@@iterator]: Returns a new Iterator object that iterates over the code points of a string.
  36. String.prototype.toLocaleLowerCase: Returns the calling string converted to lowercase, according to any locale-specific case mappings.
  37. String.prototype.toLocaleUpperCase: Returns the calling string converted to uppercase, according to any locale-specific case mappings.
  38. String.prototype.padStart: Pads the current string with a given string (repeated, if needed) so that the resulting string reaches a given length.
  39. String.prototype.padEnd: Pads the current string with a given string (repeated, if needed) so that the resulting string reaches a given length.
  40. String.prototype.slice: Extracts a section of a string and returns it as a new string.
  41. String.prototype.substring: Returns a subset of a string between one index and another.
  42. String.prototype.substr: Returns the characters in a string beginning at the specified location through the specified number of characters.
  43. String.prototype.link: Creates an HTML anchor element for the specified URL.
  44. String.prototype.fontcolor: Returns an HTML string with a element using the specified color.
  45. String.prototype.fontsize: Returns an HTML string with a element using the specified font size.
  46. String.prototype.anchor: Creates an HTML anchor element for the specified URL.
  47. **

String.prototype.small:** Returns an HTML string with a element.

  1. String.prototype.blink: Returns an HTML string with a element.
  2. String.prototype.bold: Returns an HTML string with a element.
  3. String.prototype.fixed: Returns an HTML string with a element.
  4. String.prototype.strike: Returns an HTML string with a element.
  5. String.prototype.sup: Returns an HTML string with a element.
  6. String.prototype.sub: Returns an HTML string with a element.
  7. String.prototype.big: Returns an HTML string with a element.
  8. String.prototype.italics: Returns an HTML string with an element.
  9. String.prototype.small: Returns an HTML string with a element.
  10. String.prototype.sup: Returns an HTML string with a element.
  11. String.prototype.sub: Returns an HTML string with a element.
  12. String.prototype.link: Creates an HTML anchor element for the specified URL.
  13. String.prototype.fontcolor: Returns an HTML string with a element using the specified color.
  14. String.prototype.fontsize: Returns an HTML string with a element using the specified font size.

Top comments (0)