An Interest In:
Web News this Week
- April 1, 2024
- March 31, 2024
- March 30, 2024
- March 29, 2024
- March 28, 2024
- March 27, 2024
- March 26, 2024
May 28, 2021 12:49 pm GMT
Original Link: https://dev.to/coderslang/javascript-interview-question-43-object-tostring-vs-array-tostring-2cim
JavaScript Interview Question 43: Object.toString vs Array.toString
What's the difference between Object.toString
and Array.toString
in JavaScript? Whats the output?
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
A regular behavior of the JavaScript function Object.prototype.toString
in a browser is a string that looks like [object "TYPE"]
. The part "TYPE" is substituted with the type of the Object that toString
is called on. In our case, it's Array
.
console.log(toStringObj.call(arr)); // [object Array]
In the second instance, we call toString
from Array
. Its not the same function as it overrides the standard implementation of Object.prototype.toString
.
Array.prototype.toString
returns a string that consists of all the array elements separated with commas.
console.log(toStringArr.call(arr)); // 1,2,3
ANSWER: 2 strings will appear on the screen:
[object Array]1,2,3
Original Link: https://dev.to/coderslang/javascript-interview-question-43-object-tostring-vs-array-tostring-2cim
Share this article:
Tweet
View Full Article
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To