Prototype Methods – Accessors

These accessorfunctions do not modify the original array.Combining & SplittingCombining & Splitting Combining & Splitting :

Array.prototype.concat()

Returns a new array comprised of this array joined with other array(s) and/or value(s).

Array.prototype.slice()

Extracts a section of an array and returns a shallow copy. (Note the difference with splice()).


RESETRUNFULL
<!DOCTYPE html><html><body><script>


   var a = [1,2,3,4,5];
   console.log(a.concat(a)); // [1,2,3,4,5,1,2,3,4,5]
   console.log(a.slice(2,4)); // [3, 4]

</script></body><html>

Searching: Searching: Searching:

Array.prototype.includes()

Determines whether an array contains a certain element, returning true or false as appropriate.

Array.prototype.indexOf()

Returns the first (least) index of an element within the array equal to the specified value, or -1 if none is found.

Array.prototype.lastIndexOf()

Returns the last (greatest) index of an element within the array equal to the specified value, or -1 if none is found.

This illustrates the motivation behind the introduction of .includes() in newer JavaScript.
RESETRUNFULL
<!DOCTYPE html><html><body><script>

console.log([NaN].indexOf(NaN)); // -1 failsconsole.log([NaN].includes(NaN)); // true succeeds

</script></body><html>

Conversion to String: Conversion to String: Conversion to String:

Array.prototype.join()

Joins all elements of an array into a string.

Array.prototype.toString()

Returns a string representing the array and its elements. Overrides the Object.prototype.toString() method.

Array.prototype.toLocaleString()

Returns a localized string representing the array and its elements. Overrides the Object.prototype.toLocaleString() method.


RESETRUNFULL
<!DOCTYPE html><html><body><script>


   var a = [3];
   a=a.concat(4,[5,6]);
   console.log(a.join());          // 3,4,5,6
   console.log(a.join('-'));       // 3-4-5-6
   console.log("{"+a+"}");     // {3,4,5,6}
  toString() called
         console.log(a.slice(1)+"");  // 4,5,6
  toString() called
      console.log(a.slice(2,4).toString());                                            //5,6, ‘a’ still not changed
      console.log(a.includes([5,6]));                 // false
   console.log(a.includes([5,6],1));                                                 //false,search from index 1
   console.log(a.indexOf(4));                       // 1
   console.log(a.indexOf(4,2));     // -1, search from index 2
   console.log([1,[2,3]].lastIndexOf([2,3])); // -1
    console.log(typeof [1,[2,3]][1]);              // object
   console.log(typeof []);                            // object
      a = [1337, new Date(), 'foo'];
   console.log(a.toLocaleString('ja-JP',
                                { style: 'currency', currency: 'JPY' })); 

</script></body><html>