JavaScript Array valueOf()

Posted by

Method

JavaScript Array valueOf() Method

The JavaScript Array valueOf() method returns an array containing the values of an array object. This method is used to get a real array from an array-like object.

Syntax

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

arrayObject.valueOf()

[/dm_code_snippet]

Parameter Values

This method does not take any parameter.

Description

The JavaScript Array valueOf() method returns the primitive value of the specified array object. This method is used to get a real array from an array-like object.

Example 1

This example illustrates the JavaScript Array valueOf() method.

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

let myArray = [1, 2, 3];
let myRealArray = myArray.valueOf();

console.log(myRealArray); // [1, 2, 3]

[/dm_code_snippet]

Example 2

This example illustrates the JavaScript Array valueOf() method with an array-like object.

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

let myArrayLike = {0: "a", 1: "b", 2: "c", length: 3};
let myRealArray = Array.prototype.slice.call(myArrayLike);

console.log(myRealArray); // ["a", "b", "c"]

[/dm_code_snippet]

Browser Support

The Array valueOf() method is supported in all major browsers, including Internet Explorer, Chrome, Firefox, and Safari.