JavaScript Interview Question #43: Object.toString vs Array.toString

What’s the difference between Object.toString and Array.toString in JavaScript? What’s the output?

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

A regular behavior of the JavaScript function Object.prototype.toString in a browser — is a string …


This content originally appeared on DEV Community and was authored by Coderslang: Become a Software Engineer

coderslang javascript interview question #43

What's the difference between Object.toString and Array.toString in JavaScript? What’s 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. It’s 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

Learn Full-Stack JavaScript


This content originally appeared on DEV Community and was authored by Coderslang: Become a Software Engineer


Print Share Comment Cite Upload Translate Updates
APA

Coderslang: Become a Software Engineer | Sciencx (2021-05-28T12:49:22+00:00) JavaScript Interview Question #43: Object.toString vs Array.toString. Retrieved from https://www.scien.cx/2021/05/28/javascript-interview-question-43-object-tostring-vs-array-tostring/

MLA
" » JavaScript Interview Question #43: Object.toString vs Array.toString." Coderslang: Become a Software Engineer | Sciencx - Friday May 28, 2021, https://www.scien.cx/2021/05/28/javascript-interview-question-43-object-tostring-vs-array-tostring/
HARVARD
Coderslang: Become a Software Engineer | Sciencx Friday May 28, 2021 » JavaScript Interview Question #43: Object.toString vs Array.toString., viewed ,<https://www.scien.cx/2021/05/28/javascript-interview-question-43-object-tostring-vs-array-tostring/>
VANCOUVER
Coderslang: Become a Software Engineer | Sciencx - » JavaScript Interview Question #43: Object.toString vs Array.toString. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/05/28/javascript-interview-question-43-object-tostring-vs-array-tostring/
CHICAGO
" » JavaScript Interview Question #43: Object.toString vs Array.toString." Coderslang: Become a Software Engineer | Sciencx - Accessed . https://www.scien.cx/2021/05/28/javascript-interview-question-43-object-tostring-vs-array-tostring/
IEEE
" » JavaScript Interview Question #43: Object.toString vs Array.toString." Coderslang: Become a Software Engineer | Sciencx [Online]. Available: https://www.scien.cx/2021/05/28/javascript-interview-question-43-object-tostring-vs-array-tostring/. [Accessed: ]
rf:citation
» JavaScript Interview Question #43: Object.toString vs Array.toString | Coderslang: Become a Software Engineer | Sciencx | https://www.scien.cx/2021/05/28/javascript-interview-question-43-object-tostring-vs-array-tostring/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.