w3resource

JavaScript toSource() Method: Array Object

Description

The toSource() method returns a string which represents the source code of the array.

Version

Implemented in JavaScript 1.3

Syntax

toSource()

Parameters

None

Example:

In the following web document toSource() method returns the source code of the array fruits.

HTML Code

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

JS Code

var fruits = new Array("Orange","Apple","Banana","Chery");
var newParagraph = document.createElement("p"); //creates a new paragraph element
var newText = document.createTextNode(fruits.toSource()); //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

JavaScript Core objects, methods, properties.

Previous: JavaScript sort() Method: Array Object
Next: JavaScript toString() Method: Array Object

Test your Programming skills with w3resource's quiz.



JavaScript: Tips of the Day

Returns the difference (in days) between two dates

Example:

const tips_DiffBetweenDates = (dateInitial, dateFinal) =>
  (dateFinal - dateInitial) / (1000 * 3600 * 24);
console.log(tips_DiffBetweenDates(new Date('2020-05-13'), new Date('2020-06-05')));

Output:

23