The valueOf()
from array returns the same value as array's toString()
method.
valueOf() |
Yes | Yes | Yes | Yes | Yes |
array.valueOf()
None
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.
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.
<!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: