I'm attempting to return two arrays(multiple values) through declaring them as objects
<script>
function funWithArrays(arr) {
const arr1 = []; //short string
const arr2 = []; //long string
for (var i = 0; i < arr.length; i++) {
if (arr[i].length <= 3) {
arr1.push(arr[i]);
} else {
arr2.push(arr[i]);
}
}
return {
arr1,
arr2
};
}
document.getElementById("results").innerHTML = funWithArrays(["we", "cream", "to", "this", "two"]);
</script>
This is my attempt to do so, however, when I go to run it the output is as follows:
[object Object]
I'm wondering why the actual arrays aren't being returned?
arr1andarr2(those props refer to arrays). Then you're implicitly converting that object to string by assigning it toinnerHTML, which triggers the defaulttoStringfor objects (since yours doesn't define one), which for plain objects returns the string"[object Object]". If you wanted to return an array of arrays, it would bereturn [arr1, arr2];(square brackets), which would convert to a string containing the string version of the array elements (but a bit confusingly, for nested arrays).[ [1, 2, 3], [4, 5, 6] ]converts to the string"1,2,3,4,5,6"(which is exactly what[1, 2, 3, 4, 5, 6]would convert to).