githubEdit

💱Does It Mutate¿

What does the term "mutability" mean in programming?

  • "Mutability" refers to an object's ability to undergo change. Mutable objects can be modified, while immutable objects cannot be changed after they are created.


** Identify which of the following JavaScript data types are mutable: number, string, boolean, array**

  • Arrays are mutable.

  • Numbers, strings, and booleans are all immutable.


.concat​

no mutation

Description

The concat() method is used to merge two or more arrays. This method does not change the existing arrays, but instead returns a new array.

Array.prototype.concat ( [ item1 [ , item2 [ , … ] ] ] )

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/concatarrow-up-right

Example

let array1 = ['a', 'b', 'c'];
let array2 = ['d', 'e', 'f'];

console.log(array1.concat(array2));
// expected output: Array ["a", "b", "c", "d", "e", "f"]

.copyWithin()

mutates

Description

The copyWithin() method shallow copies part of an array to another location in the same array and returns it, without modifying its size.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/copyWithinarrow-up-right

Example

.entries()

no mutation

Description

The entries() method returns a new Array Iterator object that contains the key/value pairs for each index in the array.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/entriesarrow-up-right

Example

.every

no mutation

Description

The every() method tests whether all elements in the array pass the test implemented by the provided function.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/everyarrow-up-right

Example

.fill()

mutates

Description

The fill() method fills all the elements of an array from a start index to an end index with a static value.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/fillarrow-up-right

Example

.filter

no mutation

Description

The filter() method creates a new array with all elements that pass the test implemented by the provided function.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filterarrow-up-right

Example

.find()

no mutation

Description

The find() method returns a value of the first element in the array that satisfies the provided testing function. Otherwise undefined is returned.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findarrow-up-right

Example

.findIndex()

no mutation

Description

The findIndex() method returns an index of the first element in the array that satisfies the provided testing function. Otherwise -1 is returned.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndexarrow-up-right

Example

.flat

no mutation

Description

The flat() method creates a new array with all sub-array elements concatenated into it recursively up to the specified depth.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flatarrow-up-right

Example

.forEach

no mutation

Description

The forEach() method executes a provided function once per array element.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEacharrow-up-right

Example

.includes()

no mutation

Description

The includes() method determines whether an array includes a certain element, returning true or false as appropriate.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includesarrow-up-right

Example

.indexOf

no mutation

Description

The indexOf() method returns the first index at which a given element can be found in the array, or -1 if it is not present.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOfarrow-up-right

Example

.join

no mutation

Description

The join() method joins all elements of an array into a string.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/joinarrow-up-right

Example

.keys()

no mutation

Description

The keys() method returns a new Array Iterator that contains the keys for each index in the array.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/keysarrow-up-right

Example

.lastIndexOf

no mutation

Description

The lastIndexOf() method returns the last index at which a given element can be found in the array, or -1 if it is not present. The array is searched backwards, starting at fromIndex.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/lastIndexOfarrow-up-right

Example

.map

no mutation

Description

The map() method creates a new array with the results of calling a provided function on every element in this array.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/maparrow-up-right

Example

.pop

mutates

Description

The pop() method removes the last element from an array and returns that element. This method changes the length of the array.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/poparrow-up-right

Example

.push

mutates

Description

The push() method adds one or more elements to the end of an array and returns the new length of the array.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/pusharrow-up-right

Example

.reduce

no mutation

Description

The reduce() method applies a function against an accumulator and each value of the array (from left-to-right) to reduce it to a single value.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reducearrow-up-right

Example

.reduceRight

no mutation

Description

The reduceRight() method applies a function against an accumulator and each value of the array (from right-to-left) has to reduce it to a single value.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/ReduceRightarrow-up-right

Example

.reverse

mutates

Description

The reverse() method reverses an array in place. The first array element becomes the last, and the last array element becomes the first.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reversearrow-up-right

Example

.shift

mutates

Description

The shift() method removes the first element from an array and returns that element. This method changes the length of the array.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/shiftarrow-up-right

Example

.slice

no mutation

Description

The slice() method returns a shallow copy of a portion of an array into a new array object selected from begin to end (end not included). The original array will not be modified.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slicearrow-up-right

Example

.some

no mutation

Description

The some() method tests whether some element in the array passes the test implemented by the provided function.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/somearrow-up-right

Example

.sort

mutates

Description

The sort() method sorts the elements of an array in place and returns the array. The sort is not necessarily stable. The default sort order is according to string Unicode code points.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sortarrow-up-right

Example

.splice

mutates

Description

The splice() method changes the content of an array by removing existing elements and/or adding new elements.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splicearrow-up-right

Example

.toString

no mutation

Description

The toString() method returns a string representing the specified array and its elements.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/toStringarrow-up-right

Example

.unshift

mutates

Description

The unshift() method adds one or more elements to the beginning of an array and returns the new length of the array.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/unshiftarrow-up-right

Example

.values()

no mutation

Description

The values() method returns a new Array Iterator object that contains the values for each index in the array.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/valuesarrow-up-right

Example

\

Last updated

Was this helpful?