JavaScript automatically converts an array to a comma-separated string when a primitive value is anticipated.
This phenomenon occurs consistently when attempting to output an array.
Both of these examples will yield identical outcomes:
Example
const fruits = [“Banana”, “Orange”, “Apple”, “Mango”]; document.getElementById(“demo”).innerHTML = fruits.toString(); |
Example
const fruits = [“Banana”, “Orange”, “Apple”, “Mango”]; document.getElementById(“demo”).innerHTML = fruits; |
NOTE: Every JavaScript object possesses a toString() method. |