💱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/concat

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/copyWithin

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/entries

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/every

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/fill

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/filter

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/find

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/findIndex

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/flat

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/forEach

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/includes

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/indexOf

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/join

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/keys

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/lastIndexOf

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/map

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/pop

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/push

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/Reduce

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/ReduceRight

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/reverse

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/shift

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/slice

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/some

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/sort

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/splice

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/toString

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/unshift

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/values

Example

\

Last updated

Was this helpful?