Cara menggunakan javascript array with key

The

fruits[5] = "mango";
console.log(fruits[5]); // 'mango'
console.log(Object.keys(fruits)); // ['0', '1', '2', '5']
console.log(fruits.length); // 6
9 object, as with arrays in other programming languages, enables storing a collection of multiple items under a single variable name, and has members for .

In JavaScript, arrays aren't primitives but are instead

fruits[5] = "mango";
console.log(fruits[5]); // 'mango'
console.log(Object.keys(fruits)); // ['0', '1', '2', '5']
console.log(fruits.length); // 6
9 objects with the following core characteristics:

  • JavaScript arrays are resizable and can contain a mix of different data types. (When those characteristics are undesirable, use typed arrays instead.)
  • JavaScript arrays are not associative arrays and so, array elements cannot be accessed using arbitrary strings as indexes, but must be accessed using nonnegative integers (or their respective string form) as indexes.
  • JavaScript arrays are zero-indexed: the first element of an array is at index
    fruits.length = 10;
    console.log(fruits); // ['banana', 'apple', 'peach', empty x 2, 'mango', empty x 4]
    console.log(Object.keys(fruits)); // ['0', '1', '2', '5']
    console.log(fruits.length); // 10
    console.log(fruits[8]); // undefined
    
    1, the second is at index
    fruits.length = 10;
    console.log(fruits); // ['banana', 'apple', 'peach', empty x 2, 'mango', empty x 4]
    console.log(Object.keys(fruits)); // ['0', '1', '2', '5']
    console.log(fruits.length); // 10
    console.log(fruits[8]); // undefined
    
    2, and so on — and the last element is at the value of the array's
    fruits.length = 10;
    console.log(fruits); // ['banana', 'apple', 'peach', empty x 2, 'mango', empty x 4]
    console.log(Object.keys(fruits)); // ['0', '1', '2', '5']
    console.log(fruits.length); // 10
    console.log(fruits[8]); // undefined
    
    3 property minus
    fruits.length = 10;
    console.log(fruits); // ['banana', 'apple', 'peach', empty x 2, 'mango', empty x 4]
    console.log(Object.keys(fruits)); // ['0', '1', '2', '5']
    console.log(fruits.length); // 10
    console.log(fruits[8]); // undefined
    
    2.
  • JavaScript create shallow copies. (All standard built-in copy operations with any JavaScript objects create shallow copies, rather than deep copies).

fruits[5] = "mango";
console.log(fruits[5]); // 'mango'
console.log(Object.keys(fruits)); // ['0', '1', '2', '5']
console.log(fruits.length); // 6
9 objects cannot use arbitrary strings as element indexes (as in an associative array) but must use nonnegative integers (or their respective string form). Setting or accessing via non-integers will not set or retrieve an element from the array list itself, but will set or access a variable associated with that array's . The array's object properties and list of array elements are separate, and the array's cannot be applied to these named properties.

Array elements are object properties in the same way that

fruits.length = 10;
console.log(fruits); // ['banana', 'apple', 'peach', empty x 2, 'mango', empty x 4]
console.log(Object.keys(fruits)); // ['0', '1', '2', '5']
console.log(fruits.length); // 10
console.log(fruits[8]); // undefined
6 is a property (to be specific, however,
fruits.length = 10;
console.log(fruits); // ['banana', 'apple', 'peach', empty x 2, 'mango', empty x 4]
console.log(Object.keys(fruits)); // ['0', '1', '2', '5']
console.log(fruits.length); // 10
console.log(fruits[8]); // undefined
7 is a method). Nevertheless, trying to access an element of an array as follows throws a syntax error because the property name is not valid:

console.log(arr.0); // a syntax error

JavaScript syntax requires properties beginning with a digit to be accessed using instead of dot notation. It's also possible to quote the array indices (e.g.,

fruits.length = 10;
console.log(fruits); // ['banana', 'apple', 'peach', empty x 2, 'mango', empty x 4]
console.log(Object.keys(fruits)); // ['0', '1', '2', '5']
console.log(fruits.length); // 10
console.log(fruits[8]); // undefined
8 instead of
fruits.length = 10;
console.log(fruits); // ['banana', 'apple', 'peach', empty x 2, 'mango', empty x 4]
console.log(Object.keys(fruits)); // ['0', '1', '2', '5']
console.log(fruits.length); // 10
console.log(fruits[8]); // undefined
9), although usually not necessary.

The

fruits.length = 2;
console.log(Object.keys(fruits)); // ['0', '1']
console.log(fruits.length); // 2
0 in
fruits.length = 10;
console.log(fruits); // ['banana', 'apple', 'peach', empty x 2, 'mango', empty x 4]
console.log(Object.keys(fruits)); // ['0', '1', '2', '5']
console.log(fruits.length); // 10
console.log(fruits[8]); // undefined
9 is coerced into a string by the JavaScript engine through an implicit
fruits.length = 10;
console.log(fruits); // ['banana', 'apple', 'peach', empty x 2, 'mango', empty x 4]
console.log(Object.keys(fruits)); // ['0', '1', '2', '5']
console.log(fruits.length); // 10
console.log(fruits[8]); // undefined
6 conversion. As a result,
fruits.length = 2;
console.log(Object.keys(fruits)); // ['0', '1']
console.log(fruits.length); // 2
3 and
fruits.length = 2;
console.log(Object.keys(fruits)); // ['0', '1']
console.log(fruits.length); // 2
4 would refer to two different slots on the
fruits.length = 2;
console.log(Object.keys(fruits)); // ['0', '1']
console.log(fruits.length); // 2
5 object, and the following example could be
fruits.length = 2;
console.log(Object.keys(fruits)); // ['0', '1']
console.log(fruits.length); // 2
6:

console.log(years["2"] !== years["02"]);

Only

fruits.length = 10;
console.log(fruits); // ['banana', 'apple', 'peach', empty x 2, 'mango', empty x 4]
console.log(Object.keys(fruits)); // ['0', '1', '2', '5']
console.log(fruits.length); // 10
console.log(fruits[8]); // undefined
8 is an actual array index.
fruits.length = 2;
console.log(Object.keys(fruits)); // ['0', '1']
console.log(fruits.length); // 2
8 is an arbitrary string property that will not be visited in array iteration.

A JavaScript array's

fruits.length = 10;
console.log(fruits); // ['banana', 'apple', 'peach', empty x 2, 'mango', empty x 4]
console.log(Object.keys(fruits)); // ['0', '1', '2', '5']
console.log(fruits.length); // 10
console.log(fruits[8]); // undefined
3 property and numerical properties are connected.

Several of the built-in array methods (e.g.,

method(callbackFn, thisArg)
0,
method(callbackFn, thisArg)
1,
method(callbackFn, thisArg)
2, etc.) take into account the value of an array's
fruits.length = 10;
console.log(fruits); // ['banana', 'apple', 'peach', empty x 2, 'mango', empty x 4]
console.log(Object.keys(fruits)); // ['0', '1', '2', '5']
console.log(fruits.length); // 10
console.log(fruits[8]); // undefined
3 property when they're called.

Other methods (e.g.,

method(callbackFn, thisArg)
4,
method(callbackFn, thisArg)
5, etc.) also result in updates to an array's
fruits.length = 10;
console.log(fruits); // ['banana', 'apple', 'peach', empty x 2, 'mango', empty x 4]
console.log(Object.keys(fruits)); // ['0', '1', '2', '5']
console.log(fruits.length); // 10
console.log(fruits[8]); // undefined
3 property.

const fruits = [];
fruits.push("banana", "apple", "peach");
console.log(fruits.length); // 3

When setting a property on a JavaScript array when the property is a valid array index and that index is outside the current bounds of the array, the engine will update the array's

fruits.length = 10;
console.log(fruits); // ['banana', 'apple', 'peach', empty x 2, 'mango', empty x 4]
console.log(Object.keys(fruits)); // ['0', '1', '2', '5']
console.log(fruits.length); // 10
console.log(fruits[8]); // undefined
3 property accordingly:

fruits[5] = "mango";
console.log(fruits[5]); // 'mango'
console.log(Object.keys(fruits)); // ['0', '1', '2', '5']
console.log(fruits.length); // 6

Increasing the

fruits.length = 10;
console.log(fruits); // ['banana', 'apple', 'peach', empty x 2, 'mango', empty x 4]
console.log(Object.keys(fruits)); // ['0', '1', '2', '5']
console.log(fruits.length); // 10
console.log(fruits[8]); // undefined
3.

fruits.length = 10;
console.log(fruits); // ['banana', 'apple', 'peach', empty x 2, 'mango', empty x 4]
console.log(Object.keys(fruits)); // ['0', '1', '2', '5']
console.log(fruits.length); // 10
console.log(fruits[8]); // undefined

Decreasing the

fruits.length = 10;
console.log(fruits); // ['banana', 'apple', 'peach', empty x 2, 'mango', empty x 4]
console.log(Object.keys(fruits)); // ['0', '1', '2', '5']
console.log(fruits.length); // 10
console.log(fruits[8]); // undefined
3 property does, however, delete elements.

fruits.length = 2;
console.log(Object.keys(fruits)); // ['0', '1']
console.log(fruits.length); // 2

This is explained further on the

const arrayLike = {
  0: "a",
  1: "b",
  length: 2,
};
console.log(Array.prototype.join.call(arrayLike, "+")); // 'a+b'
0 page.

Empty slots in behave inconsistently between array methods. Generally, the older methods will skip empty slots, while newer ones treat them as

const arrayLike = {
  0: "a",
  1: "b",
  length: 2,
};
console.log(Array.prototype.join.call(arrayLike, "+")); // 'a+b'
1.

Among methods that iterate through multiple elements, the following do an

const arrayLike = {
  0: "a",
  1: "b",
  length: 2,
};
console.log(Array.prototype.join.call(arrayLike, "+")); // 'a+b'
2 check before accessing the index and do not conflate empty slots with
const arrayLike = {
  0: "a",
  1: "b",
  length: 2,
};
console.log(Array.prototype.join.call(arrayLike, "+")); // 'a+b'
1:

  • const arrayLike = {
      0: "a",
      1: "b",
      length: 2,
    };
    console.log(Array.prototype.join.call(arrayLike, "+")); // 'a+b'
    
    4
  • const arrayLike = {
      0: "a",
      1: "b",
      length: 2,
    };
    console.log(Array.prototype.join.call(arrayLike, "+")); // 'a+b'
    
    5
  • const arrayLike = {
      0: "a",
      1: "b",
      length: 2,
    };
    console.log(Array.prototype.join.call(arrayLike, "+")); // 'a+b'
    
    6
  • const arrayLike = {
      0: "a",
      1: "b",
      length: 2,
    };
    console.log(Array.prototype.join.call(arrayLike, "+")); // 'a+b'
    
    7
  • const arrayLike = {
      0: "a",
      1: "b",
      length: 2,
    };
    console.log(Array.prototype.join.call(arrayLike, "+")); // 'a+b'
    
    8
  • const arrayLike = {
      0: "a",
      1: "b",
      length: 2,
    };
    console.log(Array.prototype.join.call(arrayLike, "+")); // 'a+b'
    
    9
  • Array.prototype.flat.call({}); // []
    
    0
  • method(callbackFn, thisArg)
    
    2
  • Array.prototype.flat.call({}); // []
    
    2
  • Array.prototype.flat.call({}); // []
    
    3
  • Array.prototype.flat.call({}); // []
    
    4
  • Array.prototype.flat.call({}); // []
    
    5
  • Array.prototype.flat.call({}); // []
    
    6
  • method(callbackFn, thisArg)
    
    1
  • Array.prototype.flat.call({}); // []
    
    8
  • Array.prototype.flat.call({}); // []
    
    9
  • method(callbackFn, thisArg)
    
    5

For exactly how they treat empty slots, see the page for each method.

These methods treat empty slots as if they are

const arrayLike = {
  0: "a",
  1: "b",
  length: 2,
};
console.log(Array.prototype.join.call(arrayLike, "+")); // 'a+b'
1:

  • const a = { length: 0.7 };
    Array.prototype.push.call(a);
    console.log(a.length); // 0
    
    2
  • const a = { length: 0.7 };
    Array.prototype.push.call(a);
    console.log(a.length); // 0
    
    3
  • const a = { length: 0.7 };
    Array.prototype.push.call(a);
    console.log(a.length); // 0
    
    4
  • const a = { length: 0.7 };
    Array.prototype.push.call(a);
    console.log(a.length); // 0
    
    5
  • const a = { length: 0.7 };
    Array.prototype.push.call(a);
    console.log(a.length); // 0
    
    6
  • const a = { length: 0.7 };
    Array.prototype.push.call(a);
    console.log(a.length); // 0
    
    7
  • const a = { length: 0.7 };
    Array.prototype.push.call(a);
    console.log(a.length); // 0
    
    8 Experimental
  • const a = { length: 0.7 };
    Array.prototype.push.call(a);
    console.log(a.length); // 0
    
    9 Experimental
  • console.log(years["2"] !== years["02"]);
    
    00
  • method(callbackFn, thisArg)
    
    0
  • console.log(years["2"] !== years["02"]);
    
    02
  • console.log(years["2"] !== years["02"]);
    
    03
  • console.log(years["2"] !== years["02"]);
    
    04

Some methods do not mutate the existing array that the method was called on, but instead return a new array. They do so by first accessing

console.log(years["2"] !== years["02"]);
05 to determine the constructor to use for the new array. The newly constructed array is then populated with elements. The copy always happens shallowly — the method never copies anything beyond the initially created array. Elements of the original array(s) are copied into the new array as follows:

  • Objects: the object reference is copied into the new array. Both the original and new array refer to the same object. That is, if a referenced object is modified, the changes are visible to both the new and original arrays.
  • Primitive types such as strings, numbers and booleans (not
    console.log(years["2"] !== years["02"]);
    
    06,
    console.log(years["2"] !== years["02"]);
    
    07, and
    console.log(years["2"] !== years["02"]);
    
    08 objects): their values are copied into the new array.

Other methods mutate the array that the method was called on, in which case their return value differs depending on the method: sometimes a reference to the same array, sometimes the length of the new array.

The following methods create new arrays with

console.log(years["2"] !== years["02"]);
09:

  • const arrayLike = {
      0: "a",
      1: "b",
      length: 2,
    };
    console.log(Array.prototype.join.call(arrayLike, "+")); // 'a+b'
    
    4
  • const arrayLike = {
      0: "a",
      1: "b",
      length: 2,
    };
    console.log(Array.prototype.join.call(arrayLike, "+")); // 'a+b'
    
    7
  • const arrayLike = {
      0: "a",
      1: "b",
      length: 2,
    };
    console.log(Array.prototype.join.call(arrayLike, "+")); // 'a+b'
    
    8
  • const arrayLike = {
      0: "a",
      1: "b",
      length: 2,
    };
    console.log(Array.prototype.join.call(arrayLike, "+")); // 'a+b'
    
    9
  • Array.prototype.flat.call({}); // []
    
    3
  • method(callbackFn, thisArg)
    
    1
  • method(callbackFn, thisArg)
    
    5 (to construct the array of removed elements that's returned)

Note that

const a = { length: 0.7 };
Array.prototype.push.call(a);
console.log(a.length); // 0
8 and
const a = { length: 0.7 };
Array.prototype.push.call(a);
console.log(a.length); // 0
9 do not use
console.log(years["2"] !== years["02"]);
09 to create new arrays for each group entry, but always use the plain
fruits[5] = "mango";
console.log(fruits[5]); // 'mango'
console.log(Object.keys(fruits)); // ['0', '1', '2', '5']
console.log(fruits.length); // 6
9 constructor. Conceptually, they are not copying methods either.

The following methods mutate the original array:

  • const arrayLike = {
      0: "a",
      1: "b",
      length: 2,
    };
    console.log(Array.prototype.join.call(arrayLike, "+")); // 'a+b'
    
    5
  • const a = { length: 0.7 };
    Array.prototype.push.call(a);
    console.log(a.length); // 0
    
    3
  • console.log(years["2"] !== years["02"]);
    
    23
  • method(callbackFn, thisArg)
    
    4
  • Array.prototype.flat.call({}); // []
    
    6
  • console.log(years["2"] !== years["02"]);
    
    26
  • Array.prototype.flat.call({}); // []
    
    9
  • method(callbackFn, thisArg)
    
    5
  • console.log(years["2"] !== years["02"]);
    
    29

Many array methods take a callback function as an argument. The callback function is called sequentially and at most once for each element in the array, and the return value of the callback function is used to determine the return value of the method. They all share the same signature:

method(callbackFn, thisArg)

Where

console.log(years["2"] !== years["02"]);
30 takes three arguments:

console.log(years["2"] !== years["02"]);
31

The current element being processed in the array.

console.log(years["2"] !== years["02"]);
32

The index of the current element being processed in the array.

console.log(years["2"] !== years["02"]);
33

The array that the method was called upon.

What

console.log(years["2"] !== years["02"]);
30 is expected to return depends on the array method that was called.

The

console.log(years["2"] !== years["02"]);
35 argument (defaults to
const arrayLike = {
  0: "a",
  1: "b",
  length: 2,
};
console.log(Array.prototype.join.call(arrayLike, "+")); // 'a+b'
1) will be used as the
console.log(years["2"] !== years["02"]);
37 value when calling
console.log(years["2"] !== years["02"]);
30. The
console.log(years["2"] !== years["02"]);
37 value ultimately observable by
console.log(years["2"] !== years["02"]);
30 is determined according to the usual rules: if
console.log(years["2"] !== years["02"]);
30 is , primitive
console.log(years["2"] !== years["02"]);
37 values are wrapped into objects, and
const arrayLike = {
  0: "a",
  1: "b",
  length: 2,
};
console.log(Array.prototype.join.call(arrayLike, "+")); // 'a+b'
1/
console.log(years["2"] !== years["02"]);
44 is substituted with
console.log(years["2"] !== years["02"]);
45. The
console.log(years["2"] !== years["02"]);
35 argument is irrelevant for any
console.log(years["2"] !== years["02"]);
30 defined with an arrow function, as arrow functions don't have their own
console.log(years["2"] !== years["02"]);
37 binding.

All iterative methods are and , although they behave differently with .

The following methods are iterative:

  • const arrayLike = {
      0: "a",
      1: "b",
      length: 2,
    };
    console.log(Array.prototype.join.call(arrayLike, "+")); // 'a+b'
    
    6
  • const arrayLike = {
      0: "a",
      1: "b",
      length: 2,
    };
    console.log(Array.prototype.join.call(arrayLike, "+")); // 'a+b'
    
    7
  • const a = { length: 0.7 };
    Array.prototype.push.call(a);
    console.log(a.length); // 0
    
    4
  • const a = { length: 0.7 };
    Array.prototype.push.call(a);
    console.log(a.length); // 0
    
    5
  • const a = { length: 0.7 };
    Array.prototype.push.call(a);
    console.log(a.length); // 0
    
    6
  • const a = { length: 0.7 };
    Array.prototype.push.call(a);
    console.log(a.length); // 0
    
    7
  • const arrayLike = {
      0: "a",
      1: "b",
      length: 2,
    };
    console.log(Array.prototype.join.call(arrayLike, "+")); // 'a+b'
    
    9
  • Array.prototype.flat.call({}); // []
    
    0
  • const a = { length: 0.7 };
    Array.prototype.push.call(a);
    console.log(a.length); // 0
    
    8
  • const a = { length: 0.7 };
    Array.prototype.push.call(a);
    console.log(a.length); // 0
    
    9
  • Array.prototype.flat.call({}); // []
    
    3
  • Array.prototype.flat.call({}); // []
    
    8

In particular,

const arrayLike = {
  0: "a",
  1: "b",
  length: 2,
};
console.log(Array.prototype.join.call(arrayLike, "+")); // 'a+b'
6,
const a = { length: 0.7 };
Array.prototype.push.call(a);
console.log(a.length); // 0
4,
const a = { length: 0.7 };
Array.prototype.push.call(a);
console.log(a.length); // 0
5,
const a = { length: 0.7 };
Array.prototype.push.call(a);
console.log(a.length); // 0
6,
const a = { length: 0.7 };
Array.prototype.push.call(a);
console.log(a.length); // 0
7, and
Array.prototype.flat.call({}); // []
8 do not always invoke
console.log(years["2"] !== years["02"]);
30 on every element — they stop iteration as soon as the return value is determined.

There are two other methods that take a callback function and run it at most once for each element in the array, but they have slightly different signatures from typical iterative methods (for example, they don't accept

console.log(years["2"] !== years["02"]);
35):

  • Array.prototype.flat.call({}); // []
    
    4
  • Array.prototype.flat.call({}); // []
    
    5

The

Array.prototype.flat.call({}); // []
9 method also takes a callback function, but it is not an iterative method. It mutates the array in-place, doesn't accept
console.log(years["2"] !== years["02"]);
35, and may invoke the callback multiple times on an index.

Array methods are always generic — they don't access any internal data of the array object. They only access the array elements through the

fruits.length = 10;
console.log(fruits); // ['banana', 'apple', 'peach', empty x 2, 'mango', empty x 4]
console.log(Object.keys(fruits)); // ['0', '1', '2', '5']
console.log(fruits.length); // 10
console.log(fruits[8]); // undefined
3 property and the indexed elements. This means that they can be called on array-like objects as well.

const arrayLike = {
  0: "a",
  1: "b",
  length: 2,
};
console.log(Array.prototype.join.call(arrayLike, "+")); // 'a+b'

Normalization of the length property

The

fruits.length = 10;
console.log(fruits); // ['banana', 'apple', 'peach', empty x 2, 'mango', empty x 4]
console.log(Object.keys(fruits)); // ['0', '1', '2', '5']
console.log(fruits.length); // 10
console.log(fruits[8]); // undefined
3 property is and then clamped to the range between 0 and 253 - 1.
console.log(years["2"] !== years["02"]);
75 becomes
fruits.length = 10;
console.log(fruits); // ['banana', 'apple', 'peach', empty x 2, 'mango', empty x 4]
console.log(Object.keys(fruits)); // ['0', '1', '2', '5']
console.log(fruits.length); // 10
console.log(fruits[8]); // undefined
1, so even when
fruits.length = 10;
console.log(fruits); // ['banana', 'apple', 'peach', empty x 2, 'mango', empty x 4]
console.log(Object.keys(fruits)); // ['0', '1', '2', '5']
console.log(fruits.length); // 10
console.log(fruits[8]); // undefined
3 is not present or is
const arrayLike = {
  0: "a",
  1: "b",
  length: 2,
};
console.log(Array.prototype.join.call(arrayLike, "+")); // 'a+b'
1, it behaves as if it has value
fruits.length = 10;
console.log(fruits); // ['banana', 'apple', 'peach', empty x 2, 'mango', empty x 4]
console.log(Object.keys(fruits)); // ['0', '1', '2', '5']
console.log(fruits.length); // 10
console.log(fruits[8]); // undefined
1.

Array.prototype.flat.call({}); // []

Some array methods set the

fruits.length = 10;
console.log(fruits); // ['banana', 'apple', 'peach', empty x 2, 'mango', empty x 4]
console.log(Object.keys(fruits)); // ['0', '1', '2', '5']
console.log(fruits.length); // 10
console.log(fruits[8]); // undefined
3 property of the array object. They always set the value after normalization, so
fruits.length = 10;
console.log(fruits); // ['banana', 'apple', 'peach', empty x 2, 'mango', empty x 4]
console.log(Object.keys(fruits)); // ['0', '1', '2', '5']
console.log(fruits.length); // 10
console.log(fruits[8]); // undefined
3 always ends as an integer.

const a = { length: 0.7 };
Array.prototype.push.call(a);
console.log(a.length); // 0

Array-like objects

The term refers to any object that doesn't throw during the

fruits.length = 10;
console.log(fruits); // ['banana', 'apple', 'peach', empty x 2, 'mango', empty x 4]
console.log(Object.keys(fruits)); // ['0', '1', '2', '5']
console.log(fruits.length); // 10
console.log(fruits[8]); // undefined
3 conversion process described above. In practice, such object is expected to actually have a
fruits.length = 10;
console.log(fruits); // ['banana', 'apple', 'peach', empty x 2, 'mango', empty x 4]
console.log(Object.keys(fruits)); // ['0', '1', '2', '5']
console.log(fruits.length); // 10
console.log(fruits[8]); // undefined
3 property and to have indexed elements in the range
fruits.length = 10;
console.log(fruits); // ['banana', 'apple', 'peach', empty x 2, 'mango', empty x 4]
console.log(Object.keys(fruits)); // ['0', '1', '2', '5']
console.log(fruits.length); // 10
console.log(fruits[8]); // undefined
1 to
console.log(years["2"] !== years["02"]);
85. (If it doesn't have all indices, it will be functionally equivalent to a .)

Many DOM objects are array-like — for example,

console.log(years["2"] !== years["02"]);
86 and
console.log(years["2"] !== years["02"]);
87. The
console.log(years["2"] !== years["02"]);
88 object is also array-like. You can call array methods on them even if they don't have these methods themselves.

console.log(years["2"] !== years["02"]);
0

console.log(years["2"] !== years["02"]);
89

Creates a new

fruits[5] = "mango";
console.log(fruits[5]); // 'mango'
console.log(Object.keys(fruits)); // ['0', '1', '2', '5']
console.log(fruits.length); // 6
9 object.

console.log(years["2"] !== years["02"]);
91

Returns the

fruits[5] = "mango";
console.log(fruits[5]); // 'mango'
console.log(Object.keys(fruits)); // ['0', '1', '2', '5']
console.log(fruits.length); // 6
9 constructor.

console.log(years["2"] !== years["02"]);
93

Creates a new

fruits[5] = "mango";
console.log(fruits[5]); // 'mango'
console.log(Object.keys(fruits)); // ['0', '1', '2', '5']
console.log(fruits.length); // 6
9 instance from an array-like object or iterable object.

console.log(years["2"] !== years["02"]);
95

Returns

fruits.length = 2;
console.log(Object.keys(fruits)); // ['0', '1']
console.log(fruits.length); // 2
6 if the argument is an array, or
console.log(years["2"] !== years["02"]);
97 otherwise.

console.log(years["2"] !== years["02"]);
98

Creates a new

fruits[5] = "mango";
console.log(fruits[5]); // 'mango'
console.log(Object.keys(fruits)); // ['0', '1', '2', '5']
console.log(fruits.length); // 6
9 instance with a variable number of arguments, regardless of number or type of the arguments.

const fruits = [];
fruits.push("banana", "apple", "peach");
console.log(fruits.length); // 3
00

Reflects the number of elements in an array.

const fruits = [];
fruits.push("banana", "apple", "peach");
console.log(fruits.length); // 3
01

Contains property names that were not included in the ECMAScript standard prior to the ES2015 version and that are ignored for

const fruits = [];
fruits.push("banana", "apple", "peach");
console.log(fruits.length); // 3
02 statement-binding purposes.

const fruits = [];
fruits.push("banana", "apple", "peach");
console.log(fruits.length); // 3
03

Returns the array item at the given index. Accepts negative integers, which count back from the last item.

const fruits = [];
fruits.push("banana", "apple", "peach");
console.log(fruits.length); // 3
04

Returns a new array that is the calling array joined with other array(s) and/or value(s).

const fruits = [];
fruits.push("banana", "apple", "peach");
console.log(fruits.length); // 3
05

Copies a sequence of array elements within an array.

const fruits = [];
fruits.push("banana", "apple", "peach");
console.log(fruits.length); // 3
06

Returns a new array iterator object that contains the key/value pairs for each index in an array.

const fruits = [];
fruits.push("banana", "apple", "peach");
console.log(fruits.length); // 3
07

Returns

fruits.length = 2;
console.log(Object.keys(fruits)); // ['0', '1']
console.log(fruits.length); // 2
6 if every element in the calling array satisfies the testing function.

const fruits = [];
fruits.push("banana", "apple", "peach");
console.log(fruits.length); // 3
09

Fills all the elements of an array from a start index to an end index with a static value.

const fruits = [];
fruits.push("banana", "apple", "peach");
console.log(fruits.length); // 3
10

Returns a new array containing all elements of the calling array for which the provided filtering function returns

fruits.length = 2;
console.log(Object.keys(fruits)); // ['0', '1']
console.log(fruits.length); // 2
6.

const fruits = [];
fruits.push("banana", "apple", "peach");
console.log(fruits.length); // 3
12

Returns the value of the first element in the array that satisfies the provided testing function, or

const arrayLike = {
  0: "a",
  1: "b",
  length: 2,
};
console.log(Array.prototype.join.call(arrayLike, "+")); // 'a+b'
1 if no appropriate element is found.

const fruits = [];
fruits.push("banana", "apple", "peach");
console.log(fruits.length); // 3
14

Returns the index of the first element in the array that satisfies the provided testing function, or

const fruits = [];
fruits.push("banana", "apple", "peach");
console.log(fruits.length); // 3
15 if no appropriate element was found.

const fruits = [];
fruits.push("banana", "apple", "peach");
console.log(fruits.length); // 3
16

Returns the value of the last element in the array that satisfies the provided testing function, or

const arrayLike = {
  0: "a",
  1: "b",
  length: 2,
};
console.log(Array.prototype.join.call(arrayLike, "+")); // 'a+b'
1 if no appropriate element is found.

const fruits = [];
fruits.push("banana", "apple", "peach");
console.log(fruits.length); // 3
18

Returns the index of the last element in the array that satisfies the provided testing function, or

const fruits = [];
fruits.push("banana", "apple", "peach");
console.log(fruits.length); // 3
15 if no appropriate element was found.

const fruits = [];
fruits.push("banana", "apple", "peach");
console.log(fruits.length); // 3
20

Returns a new array with all sub-array elements concatenated into it recursively up to the specified depth.

const fruits = [];
fruits.push("banana", "apple", "peach");
console.log(fruits.length); // 3
21

Returns a new array formed by applying a given callback function to each element of the calling array, and then flattening the result by one level.

const fruits = [];
fruits.push("banana", "apple", "peach");
console.log(fruits.length); // 3
22

Calls a function for each element in the calling array.

const fruits = [];
fruits.push("banana", "apple", "peach");
console.log(fruits.length); // 3
23 Experimental

Groups the elements of an array into an object according to the strings returned by a test function.

const fruits = [];
fruits.push("banana", "apple", "peach");
console.log(fruits.length); // 3
24 Experimental

Groups the elements of an array into a

const fruits = [];
fruits.push("banana", "apple", "peach");
console.log(fruits.length); // 3
25 according to values returned by a test function.

const fruits = [];
fruits.push("banana", "apple", "peach");
console.log(fruits.length); // 3
26

Determines whether the calling array contains a value, returning

fruits.length = 2;
console.log(Object.keys(fruits)); // ['0', '1']
console.log(fruits.length); // 2
6 or
console.log(years["2"] !== years["02"]);
97 as appropriate.

const fruits = [];
fruits.push("banana", "apple", "peach");
console.log(fruits.length); // 3
29

Returns the first (least) index at which a given element can be found in the calling array.

const fruits = [];
fruits.push("banana", "apple", "peach");
console.log(fruits.length); // 3
30

Joins all elements of an array into a string.

const fruits = [];
fruits.push("banana", "apple", "peach");
console.log(fruits.length); // 3
31

Returns a new array iterator that contains the keys for each index in the calling array.

const fruits = [];
fruits.push("banana", "apple", "peach");
console.log(fruits.length); // 3
32

Returns the last (greatest) index at which a given element can be found in the calling array, or

const fruits = [];
fruits.push("banana", "apple", "peach");
console.log(fruits.length); // 3
15 if none is found.

const fruits = [];
fruits.push("banana", "apple", "peach");
console.log(fruits.length); // 3
34

Returns a new array containing the results of invoking a function on every element in the calling array.

const fruits = [];
fruits.push("banana", "apple", "peach");
console.log(fruits.length); // 3
35

Removes the last element from an array and returns that element.

const fruits = [];
fruits.push("banana", "apple", "peach");
console.log(fruits.length); // 3
36

Adds one or more elements to the end of an array, and returns the new

fruits.length = 10;
console.log(fruits); // ['banana', 'apple', 'peach', empty x 2, 'mango', empty x 4]
console.log(Object.keys(fruits)); // ['0', '1', '2', '5']
console.log(fruits.length); // 10
console.log(fruits[8]); // undefined
3 of the array.

const fruits = [];
fruits.push("banana", "apple", "peach");
console.log(fruits.length); // 3
38

Executes a user-supplied "reducer" callback function on each element of the array (from left to right), to reduce it to a single value.

const fruits = [];
fruits.push("banana", "apple", "peach");
console.log(fruits.length); // 3
39

Executes a user-supplied "reducer" callback function on each element of the array (from right to left), to reduce it to a single value.

const fruits = [];
fruits.push("banana", "apple", "peach");
console.log(fruits.length); // 3
40

Reverses the order of the elements of an array in place. (First becomes the last, last becomes first.)

const fruits = [];
fruits.push("banana", "apple", "peach");
console.log(fruits.length); // 3
41

Removes the first element from an array and returns that element.

const fruits = [];
fruits.push("banana", "apple", "peach");
console.log(fruits.length); // 3
42

Extracts a section of the calling array and returns a new array.

const fruits = [];
fruits.push("banana", "apple", "peach");
console.log(fruits.length); // 3
43

Returns

fruits.length = 2;
console.log(Object.keys(fruits)); // ['0', '1']
console.log(fruits.length); // 2
6 if at least one element in the calling array satisfies the provided testing function.

const fruits = [];
fruits.push("banana", "apple", "peach");
console.log(fruits.length); // 3
45

Sorts the elements of an array in place and returns the array.

const fruits = [];
fruits.push("banana", "apple", "peach");
console.log(fruits.length); // 3
46

Adds and/or removes elements from an array.

const fruits = [];
fruits.push("banana", "apple", "peach");
console.log(fruits.length); // 3
47

Returns a localized string representing the calling array and its elements. Overrides the

const fruits = [];
fruits.push("banana", "apple", "peach");
console.log(fruits.length); // 3
48 method.

const fruits = [];
fruits.push("banana", "apple", "peach");
console.log(fruits.length); // 3
49

Returns a string representing the calling array and its elements. Overrides the

const fruits = [];
fruits.push("banana", "apple", "peach");
console.log(fruits.length); // 3
50 method.

const fruits = [];
fruits.push("banana", "apple", "peach");
console.log(fruits.length); // 3
51

Adds one or more elements to the front of an array, and returns the new

fruits.length = 10;
console.log(fruits); // ['banana', 'apple', 'peach', empty x 2, 'mango', empty x 4]
console.log(Object.keys(fruits)); // ['0', '1', '2', '5']
console.log(fruits.length); // 10
console.log(fruits[8]); // undefined
3 of the array.

const fruits = [];
fruits.push("banana", "apple", "peach");
console.log(fruits.length); // 3
53

Returns a new array iterator object that contains the values for each index in the array.

const fruits = [];
fruits.push("banana", "apple", "peach");
console.log(fruits.length); // 3
54

An alias for the

console.log(years["2"] !== years["02"]);
04 method by default.

This section provides some examples of common array operations in JavaScript.

Note: If you're not yet familiar with array basics, consider first reading JavaScript First Steps: Arrays, which , and includes other examples of common array operations.

This example shows three ways to create new array: first using , then using the

console.log(years["2"] !== years["02"]);
89 constructor, and finally using
const fruits = [];
fruits.push("banana", "apple", "peach");
console.log(fruits.length); // 3
57 to build the array from a string.

console.log(years["2"] !== years["02"]);
1

This example uses the

method(callbackFn, thisArg)
0 method to create a string from the
const fruits = [];
fruits.push("banana", "apple", "peach");
console.log(fruits.length); // 3
59 array.

console.log(years["2"] !== years["02"]);
2

This example shows how to access items in the

const fruits = [];
fruits.push("banana", "apple", "peach");
console.log(fruits.length); // 3
59 array by specifying the index number of their position in the array.

console.log(years["2"] !== years["02"]);
3

This example uses the

method(callbackFn, thisArg)
2 method to find the position (index) of the string
const fruits = [];
fruits.push("banana", "apple", "peach");
console.log(fruits.length); // 3
62 in the
const fruits = [];
fruits.push("banana", "apple", "peach");
console.log(fruits.length); // 3
59 array.

console.log(years["2"] !== years["02"]);
4

This example shows two ways to check if the

const fruits = [];
fruits.push("banana", "apple", "peach");
console.log(fruits.length); // 3
59 array contains
const fruits = [];
fruits.push("banana", "apple", "peach");
console.log(fruits.length); // 3
62 and
const fruits = [];
fruits.push("banana", "apple", "peach");
console.log(fruits.length); // 3
66: first with the
console.log(years["2"] !== years["02"]);
00 method, and then with the
method(callbackFn, thisArg)
2 method to test for an index value that's not
const fruits = [];
fruits.push("banana", "apple", "peach");
console.log(fruits.length); // 3
15.

console.log(years["2"] !== years["02"]);
5

This example uses the

method(callbackFn, thisArg)
4 method to append a new string to the
const fruits = [];
fruits.push("banana", "apple", "peach");
console.log(fruits.length); // 3
59 array.

console.log(years["2"] !== years["02"]);
6

This example uses the

console.log(years["2"] !== years["02"]);
23 method to remove the last item from the
const fruits = [];
fruits.push("banana", "apple", "peach");
console.log(fruits.length); // 3
59 array.

console.log(years["2"] !== years["02"]);
7

Note:

console.log(years["2"] !== years["02"]);
23 can only be used to remove the last item from an array. To remove multiple items from the end of an array, see the next example.

This example uses the

method(callbackFn, thisArg)
5 method to remove the last 3 items from the
const fruits = [];
fruits.push("banana", "apple", "peach");
console.log(fruits.length); // 3
59 array.

console.log(years["2"] !== years["02"]);
8

This example uses the

method(callbackFn, thisArg)
5 method to truncate the
const fruits = [];
fruits.push("banana", "apple", "peach");
console.log(fruits.length); // 3
59 array down to just its first 2 items.

console.log(years["2"] !== years["02"]);
9

This example uses the

console.log(years["2"] !== years["02"]);
26 method to remove the first item from the
const fruits = [];
fruits.push("banana", "apple", "peach");
console.log(fruits.length); // 3
59 array.

const fruits = [];
fruits.push("banana", "apple", "peach");
console.log(fruits.length); // 3
0

Note:

console.log(years["2"] !== years["02"]);
26 can only be used to remove the first item from an array. To remove multiple items from the beginning of an array, see the next example.

This example uses the

method(callbackFn, thisArg)
5 method to remove the first 3 items from the
const fruits = [];
fruits.push("banana", "apple", "peach");
console.log(fruits.length); // 3
59 array.

const fruits = [];
fruits.push("banana", "apple", "peach");
console.log(fruits.length); // 3
1

This example uses the

console.log(years["2"] !== years["02"]);
29 method to add, at index
fruits.length = 10;
console.log(fruits); // ['banana', 'apple', 'peach', empty x 2, 'mango', empty x 4]
console.log(Object.keys(fruits)); // ['0', '1', '2', '5']
console.log(fruits.length); // 10
console.log(fruits[8]); // undefined
1, a new item to the
const fruits = [];
fruits.push("banana", "apple", "peach");
console.log(fruits.length); // 3
59 array — making it the new first item in the array.

const fruits = [];
fruits.push("banana", "apple", "peach");
console.log(fruits.length); // 3
2

This example uses the

method(callbackFn, thisArg)
5 method to remove the string
const fruits = [];
fruits.push("banana", "apple", "peach");
console.log(fruits.length); // 3
62 from the
const fruits = [];
fruits.push("banana", "apple", "peach");
console.log(fruits.length); // 3
59 array — by specifying the index position of
const fruits = [];
fruits.push("banana", "apple", "peach");
console.log(fruits.length); // 3
62.

const fruits = [];
fruits.push("banana", "apple", "peach");
console.log(fruits.length); // 3
3

This example uses the

method(callbackFn, thisArg)
5 method to remove the strings
const fruits = [];
fruits.push("banana", "apple", "peach");
console.log(fruits.length); // 3
62 and
const fruits = [];
fruits.push("banana", "apple", "peach");
console.log(fruits.length); // 3
93 from the
const fruits = [];
fruits.push("banana", "apple", "peach");
console.log(fruits.length); // 3
59 array — by specifying the index position of
const fruits = [];
fruits.push("banana", "apple", "peach");
console.log(fruits.length); // 3
62, along with a count of the number of total items to remove.

const fruits = [];
fruits.push("banana", "apple", "peach");
console.log(fruits.length); // 3
4

This example uses the

method(callbackFn, thisArg)
5 method to replace the last 2 items in the
const fruits = [];
fruits.push("banana", "apple", "peach");
console.log(fruits.length); // 3
59 array with new items.

const fruits = [];
fruits.push("banana", "apple", "peach");
console.log(fruits.length); // 3
5

This example uses a

const fruits = [];
fruits.push("banana", "apple", "peach");
console.log(fruits.length); // 3
98 loop to iterate over the
const fruits = [];
fruits.push("banana", "apple", "peach");
console.log(fruits.length); // 3
59 array, logging each item to the console.

const fruits = [];
fruits.push("banana", "apple", "peach");
console.log(fruits.length); // 3
6

But

const fruits = [];
fruits.push("banana", "apple", "peach");
console.log(fruits.length); // 3
98 is just one of many ways to iterate over any array; for more ways, see Loops and iteration, and see the documentation for the
const arrayLike = {
  0: "a",
  1: "b",
  length: 2,
};
console.log(Array.prototype.join.call(arrayLike, "+")); // 'a+b'
6,
const arrayLike = {
  0: "a",
  1: "b",
  length: 2,
};
console.log(Array.prototype.join.call(arrayLike, "+")); // 'a+b'
7,
const arrayLike = {
  0: "a",
  1: "b",
  length: 2,
};
console.log(Array.prototype.join.call(arrayLike, "+")); // 'a+b'
9,
Array.prototype.flat.call({}); // []
3,
Array.prototype.flat.call({}); // []
4, and
Array.prototype.flat.call({}); // []
5 methods — and see the next example, which uses the
Array.prototype.flat.call({}); // []
0 method.

This example uses the

Array.prototype.flat.call({}); // []
0 method to call a function on each element in the
const fruits = [];
fruits.push("banana", "apple", "peach");
console.log(fruits.length); // 3
59 array; the function causes each item to be logged to the console, along with the item's index number.

const fruits = [];
fruits.push("banana", "apple", "peach");
console.log(fruits.length); // 3
7

This example uses the

const arrayLike = {
  0: "a",
  1: "b",
  length: 2,
};
console.log(Array.prototype.join.call(arrayLike, "+")); // 'a+b'
4 method to merge the
const fruits = [];
fruits.push("banana", "apple", "peach");
console.log(fruits.length); // 3
59 array with a
fruits[5] = "mango";
console.log(fruits[5]); // 'mango'
console.log(Object.keys(fruits)); // ['0', '1', '2', '5']
console.log(fruits.length); // 6
12 array, to produce a new
fruits[5] = "mango";
console.log(fruits[5]); // 'mango'
console.log(Object.keys(fruits)); // ['0', '1', '2', '5']
console.log(fruits.length); // 6
13 array. Notice that
const fruits = [];
fruits.push("banana", "apple", "peach");
console.log(fruits.length); // 3
59 and
fruits[5] = "mango";
console.log(fruits[5]); // 'mango'
console.log(Object.keys(fruits)); // ['0', '1', '2', '5']
console.log(fruits.length); // 6
12 remain unchanged.

const fruits = [];
fruits.push("banana", "apple", "peach");
console.log(fruits.length); // 3
8

This example shows three ways to create a new array from the existing

const fruits = [];
fruits.push("banana", "apple", "peach");
console.log(fruits.length); // 3
59 array: first by using spread syntax, then by using the
fruits[5] = "mango";
console.log(fruits[5]); // 'mango'
console.log(Object.keys(fruits)); // ['0', '1', '2', '5']
console.log(fruits.length); // 6
17 method, and then by using the
method(callbackFn, thisArg)
1 method.

const fruits = [];
fruits.push("banana", "apple", "peach");
console.log(fruits.length); // 3
9

All built-in array-copy operations (spread syntax,

console.log(years["2"] !== years["02"]);
93,
const fruits = [];
fruits.push("banana", "apple", "peach");
console.log(fruits.length); // 3
42, and
const fruits = [];
fruits.push("banana", "apple", "peach");
console.log(fruits.length); // 3
04) create shallow copies. If you instead want a deep copy of an array, you can use
fruits[5] = "mango";
console.log(fruits[5]); // 'mango'
console.log(Object.keys(fruits)); // ['0', '1', '2', '5']
console.log(fruits.length); // 6
22 to convert the array to a JSON string, and then
fruits[5] = "mango";
console.log(fruits[5]); // 'mango'
console.log(Object.keys(fruits)); // ['0', '1', '2', '5']
console.log(fruits.length); // 6
23 to convert the string back into a new array that's completely independent from the original array.

fruits[5] = "mango";
console.log(fruits[5]); // 'mango'
console.log(Object.keys(fruits)); // ['0', '1', '2', '5']
console.log(fruits.length); // 6
0

You can also create deep copies using the

fruits[5] = "mango";
console.log(fruits[5]); // 'mango'
console.log(Object.keys(fruits)); // ['0', '1', '2', '5']
console.log(fruits.length); // 6
24 method, which has the advantage of allowing transferable objects in the source to be transferred to the new copy, rather than just cloned.

Finally, it's important to understand that assigning an existing array to a new variable doesn't create a copy of either the array or its elements. Instead the new variable is just a reference, or alias, to the original array; that is, the original array's name and the new variable name are just two names for the exact same object (and so will always evaluate as ). Therefore, if you make any changes at all either to the value of the original array or to the value of the new variable, the other will change, too:

fruits[5] = "mango";
console.log(fruits[5]); // 'mango'
console.log(Object.keys(fruits)); // ['0', '1', '2', '5']
console.log(fruits.length); // 6
1

The

const fruits = [];
fruits.push("banana", "apple", "peach");
console.log(fruits.length); // 3
23 methods can be used to group the elements of an array, using a test function that returns a string indicating the group of the current element.

Here we have a simple inventory array that contains "food" objects that have a

fruits[5] = "mango";
console.log(fruits[5]); // 'mango'
console.log(Object.keys(fruits)); // ['0', '1', '2', '5']
console.log(fruits.length); // 6
26 and a
fruits[5] = "mango";
console.log(fruits[5]); // 'mango'
console.log(Object.keys(fruits)); // ['0', '1', '2', '5']
console.log(fruits.length); // 6
27.

fruits[5] = "mango";
console.log(fruits[5]); // 'mango'
console.log(Object.keys(fruits)); // ['0', '1', '2', '5']
console.log(fruits.length); // 6
2

To use

const a = { length: 0.7 };
Array.prototype.push.call(a);
console.log(a.length); // 0
8, you supply a callback function that is called with the current element, and optionally the current index and array, and returns a string indicating the group of the element.

The code below uses an arrow function to return the

fruits[5] = "mango";
console.log(fruits[5]); // 'mango'
console.log(Object.keys(fruits)); // ['0', '1', '2', '5']
console.log(fruits.length); // 6
27 of each array element (this uses to unpack the
fruits[5] = "mango";
console.log(fruits[5]); // 'mango'
console.log(Object.keys(fruits)); // ['0', '1', '2', '5']
console.log(fruits.length); // 6
27 element from the passed object). The result is an object that has properties named after the unique strings returned by the callback. Each property is assigned an array containing the elements in the group.

fruits[5] = "mango";
console.log(fruits[5]); // 'mango'
console.log(Object.keys(fruits)); // ['0', '1', '2', '5']
console.log(fruits.length); // 6
3

Note that the returned object references the same elements as the original array (not deep copies). Changing the internal structure of these elements will be reflected in both the original array and the returned object.

If you can't use a string as the key, for example, if the information to group is associated with an object that might change, then you can instead use

const fruits = [];
fruits.push("banana", "apple", "peach");
console.log(fruits.length); // 3
24. This is very similar to
fruits[5] = "mango";
console.log(fruits[5]); // 'mango'
console.log(Object.keys(fruits)); // ['0', '1', '2', '5']
console.log(fruits.length); // 6
32 except that it groups the elements of the array into a
const fruits = [];
fruits.push("banana", "apple", "peach");
console.log(fruits.length); // 3
25 that can use an arbitrary value (object or primitive) as a key.

The following creates a chessboard as a two-dimensional array of strings. The first move is made by copying the

fruits[5] = "mango";
console.log(fruits[5]); // 'mango'
console.log(Object.keys(fruits)); // ['0', '1', '2', '5']
console.log(fruits.length); // 6
34 in
fruits[5] = "mango";
console.log(fruits[5]); // 'mango'
console.log(Object.keys(fruits)); // ['0', '1', '2', '5']
console.log(fruits.length); // 6
35 to
fruits[5] = "mango";
console.log(fruits[5]); // 'mango'
console.log(Object.keys(fruits)); // ['0', '1', '2', '5']
console.log(fruits.length); // 6
36. The old position at
fruits[5] = "mango";
console.log(fruits[5]); // 'mango'
console.log(Object.keys(fruits)); // ['0', '1', '2', '5']
console.log(fruits.length); // 6
37 is made blank.

fruits[5] = "mango";
console.log(fruits[5]); // 'mango'
console.log(Object.keys(fruits)); // ['0', '1', '2', '5']
console.log(fruits.length); // 6
4

Here is the output:

fruits[5] = "mango";
console.log(fruits[5]); // 'mango'
console.log(Object.keys(fruits)); // ['0', '1', '2', '5']
console.log(fruits.length); // 6
5

fruits[5] = "mango";
console.log(fruits[5]); // 'mango'
console.log(Object.keys(fruits)); // ['0', '1', '2', '5']
console.log(fruits.length); // 6
6

Results in

fruits[5] = "mango";
console.log(fruits[5]); // 'mango'
console.log(Object.keys(fruits)); // ['0', '1', '2', '5']
console.log(fruits.length); // 6
7

The result of a match between a

fruits[5] = "mango";
console.log(fruits[5]); // 'mango'
console.log(Object.keys(fruits)); // ['0', '1', '2', '5']
console.log(fruits.length); // 6
38 and a string can create a JavaScript array that has properties and elements which provide information about the match. Such an array is returned by
fruits[5] = "mango";
console.log(fruits[5]); // 'mango'
console.log(Object.keys(fruits)); // ['0', '1', '2', '5']
console.log(fruits.length); // 6
39 and
fruits[5] = "mango";
console.log(fruits[5]); // 'mango'
console.log(Object.keys(fruits)); // ['0', '1', '2', '5']
console.log(fruits.length); // 6
40.

Bagaimana cara yang benar untuk menulis array javascript?

Cara Membuat Array pada Javascript Pada javascript, array dapat kita buat dengan tanda kurung siku ( [...] ). Contoh: var products = []; Maka variabel products akan berisi sebuah array kosong.

Method apa yang dapat kita gunakan untuk memilah elemen array berdasarkan kondisi tertentu dan akan membuat sebuah array baru?

filter() Metode ini berfungsi untuk membuat sebuah array baru dengan memperhatikan kondisi tertentu pada setiap elemen dari array yang sudah ada.

Array cocok digunakan untuk apa?

Array adalah larik yang berisi kumpulan data dengan tipe serupa. Teknologi ini dapat digunakan untuk mempermudah penghitungan data karena mengelompokkan data-data berdasarkan kesamaannya. Untuk mempermudah pemahaman Anda mengenai hal ini, simak analogi berikut.

Bagaimana cara mengosongkan nilai data pada array?

Mengosongkan array adalah salah satu konsep penting yang terlibat jadi berikut adalah beberapa metode yang dapat digunakan..
Menggunakan property .length. Fungsi properti . ... .
Atur Ulang dengan nilai Array kosong. Ini adalah cara tercepat untuk mengosongkan Array. ... .
Menggunakan fungsi Array splice().