Javascript Reference - JavaScript Array valueOf() Method








The valueOf() from array returns the same value as array's toString() method.

Browser Support

valueOf() Yes Yes Yes Yes Yes

Syntax

array.valueOf()

Parameters

None

Example


var colors = ["red", "blue", "green"]; 
    
console.log(colors.toString()); //red,blue,green 
console.log(colors.valueOf()); //red,blue,green 
console.log(colors); //red,blue,green 

The code above generates the following result.





Example 2

The following code shows how to use array.valueOf with a two dimensional array.


var numbers = new Array(3,6,7);
var colors = new Array("Blue","Green","Red",numbers);
console.log(colors.valueOf());

The code above generates the following result.

Example 3


<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">return an array</button>
<p id="demo"></p>
<script>
function myFunction() {<!--from w  w w . j  a va  2 s  .c  o  m-->
    var array1 = ["A", "B", "C", "C"];
    document.getElementById("demo").innerHTML = array1.valueOf();
}
</script>
</body>
</html>

The code above is rendered as follows: