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.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/javascript/object-property-method/array-valueof.php