DEV Community

Cover image for JavaScript Interview Question #20: Can you sum arrays with objects in JS?
Coderslang: Become a Software Engineer
Coderslang: Become a Software Engineer

Posted on • Originally published at learn.coderslang.com on

JavaScript Interview Question #20: Can you sum arrays with objects in JS?

js-test-20

What’s the sum of an empty object and an empty array in JS? Is there any length to be found?
.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

Once again, we’re dealing with type conversion in JavaScript.

Both an empty array and an empty object will be converted to strings.

In the case of an empty array, it will be an empty string.

But for the empty object, we get the string [object Object]!

The length of this string is greater than 10, so we’ll see the first message logged to the console.

More examples of JavaScript typecast are covered here.


ANSWER: the string wow, this is quite long will appear on the screen.

Learn Full Stack JavaScript

Discussion (0)