Arrays

var a1 = [1,2,3]; 
var a2 = Array.of(1, 2, 3);
var a3 = new Array(); // empty array, same as []
a3.push("lalala"); // add item to end of array
var item = a2[1]; // access to item by index
var a4 = [ [1,2], ["a","b"]];
var a5 = [ 1,,3]; // skiped value 

// inline iterating over array
["a", 2 , true].forEach(function(item, ind){
   console.log(item, " has position ", ind);  
});

// iterating over array
var a6 = ["a", 2 , true];
for(var i = 0; i < a6.length; i++){
   console.log(a6[i], " has position ", i);  
}

Arrays in javascript implemented by class Array. Item of array can be any type. Items of array can be different types. Skiped value means undefined value.

Index of element in array is zero based.

Array

Some methods handle part of array defined by range [from; end). A from is first index and by default is 0. An end last index and by default is length of array. Negative values will be treated as array.length + val.

The cb parameter in methods such as forEach defines a callback function that is applied to each element in the array. It has the following parameters:

  • val - value of element
  • ind - index of element in array, it is optional parameter
  • arr - array itself, it is optional parameter

Callback can return value, for example true/false when it used as test function.

The argThis parameter contains value that used as this value inside a callback function.

methoddescription
concat( [a1 [, a2 [..., an]]]) Creates new array from current array and append elements from the a1...an arrays to the end. If ax is not array, it will be append as element.
copyWithin( to [, from [, end]]) Copies part of an array to another location [from;end) in the same array without changing a length. This is a mutable method.
every(cb [,argThis] ) Tests whether all elements in the array pass the test implemented by the callback function. Returns true if all elements pass test or array is empty.
some(cb [,argThis] ) Returns true if at least one element passes test.
fill( v [, from [,end]]) Fill [from;end) or all array by pointed value.
filter( cb [, argThis]) Creates a new array with all elements that pass the test implemented by callback function.
find( cb [, argThis]) Returns the value of the first element that passed the test.
findIndex( cb [, argThis]) Returns the index of the first element that passed the test.
flat( [ depth])
forEach( cb [, argThis]) Executes a callback function for each element.
indexOf(val[, from]) Returns the index of first element with given value or -1 if it is not present.
join([sep]) Creates a new string from the elements of array separated by given separator.
lastIndexOf(val[, from]) Returns the index of last element with given value or -1 if it is not present.
reverse() Reverses an array. In new array the first element becomes the last, and the last becomes the first.
push(el1[, ...[, elN]]) Adds elements at the end of array.
pop() Removes a last element.
shift() Removes the first element from an array and returns it.
splice(pos[, cnt[, item1[, item2[, ...]]]]) Removes all or a specified number of elements from position pos . Adds the specified elements item_x if necessary.
unshift(el1[, ...[, elN]]) Adds elements to the beginning of the array.
toString() Creates a new string from the elements of array separated by comma.
values() Returns a new Array Iterator object.
var arr = [1, 2, 3, 4, 5];
var it = arr.values();

for (let el of it) {
  console.log(el);
}  

remove items

You can remove item by its index by using delete operator:

var a = [1, 2, 3, 4, 5, 6];
delete a[2]; // result a = [1, 2, 4, 5, 6]

You can remove items by value using iteration:

var a = [1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6];
for( var i = 0; i < a.length; i++) { 
    if ( a[i] === 2) { 
        delete a[i--]; 
    }
}
// result a = [1, 3, 4, 5, 6, 1, 3, 4, 5, 6]

You can remove the last elements using the length property or the pop() method:

var a = [1, 2, 3, 4, 5, 6];
a.length = 4; // result a = [1, 2, 3, 4]
var a = [1, 2, 3, 4, 5, 6]; ar.pop(); // result a = [1, 2, 3, 4, 5]

You can remove first item using the shift() method:

var a = [1, 2, 3, 4, 5, 6];
a.shift(); // result a = [2, 3, 4, 5, 6]

You can remove subarray using the splice() method:

var a = [1, 2, 3, 4, 5, 6];
a.splice(2,3); // result a = [1, 2, 6]

You can remove items by conditions using filtering:

var a = [1, 2, 3, 4, 5, 6];
a = a.filter((e) => e%2==0); // result a = [2, 4, 6]; 

ArrayBuffer

The ArrayBuffer object is used to store raw binary data. Size of array has a fixed length. It is not used directly for writing and reading data, for this is used typed arrays or DataView object which represents the buffer in a specific format.
It can be useful for working with image data, file data or stream data.

Typed arrays

A typed array allows to get an array-like view of an underlying binary data buffer (ArrayBuffer).

  • Int8Array - array of signed bytes, possible values of element in range [-128 ; 127]
  • Uint8Array - array of unsigned bytes, possible values of element in range [0 ; 255]
  • Int16Array - array of signed short integers, every element has size 2 bytes, possible values in range [-32768 ; 32767]
  • Uint16Array - array of unsigned short integers, every element has size 2 bytes, possible values in range [0 ; 65535]
  • Int32Array - array of signed integers, every element has size 4 bytes, possible values in range [-2147483648 ; 2147483647]
  • Uint32Array - array of signed integers, every element has size 4 bytes, possible values in range [0 ; 4294967295]
  • Float32Array - array of floating point numbers with size 4 bytes, possible values in range [1.2x10-38 ; 3.4x1038]
  • Float64Array - array of double floating point numbers with size 8 bytes, possible values in range [5.0x10-324 ; 1.8x10308]

Not all methods of Array are supported, because ArrayBuffer is fixed length array.