w3resource

JavaScript valueOf() Method: Array Object

Description

The valueOf() method returns the primitive value of an array or the primitive value of its elements.

Version

Implemented in JavaScript 1.1

Syntax

valueOf()

Parameters

None

Example:

In the following web document valueOf() method returns the value of the array object.

HTML Code

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf8" />
<title>JavaScript valueOf() method example</title>
<style type="text/css">
h1 {color:red}
</style>
</head>
<body>
<h1>JavaScript : valueOf() method</h1>
<hr>
<script src="array-valueof-example1.js"></script>
</body>
</html>

JS Code

var value = new Array('$.1', '$.2', '$.3');
var fruits = new Array("Orange", "Apple", "Banana", "Chery",value);
var newParagraph = document.createElement("p"); //creates a new paragraph element
var newText = document.createTextNode(fruits.valueOf()); //creates text along with ouput to be displayed 
newParagraph.appendChild(newText); //created text is appended to the paragraph element created
document.body.appendChild(newParagraph); // created paragraph and text along with output is appended to the document body

View the example in the browser

Practice the example above online

JavaScript valueOf() method example

See also:

JavaScript Core objects, methods, properties.

Previous: JavaScript unshift() Method: Array Object
Next: Javascript Boolean Objects - Properties and Methods

Test your Programming skills with w3resource's quiz.



JavaScript: Tips of the Day

Returns true if the given number is a power of 2, false otherwise

Example:

const tips_isPowerOfTwo = n => !!n && (n & (n - 1)) == 0;
console.log(tips_isPowerOfTwo(0)); 
console.log(tips_isPowerOfTwo(4));
console.log(tips_isPowerOfTwo(16));

Output:

false
true
true