DEV Community

loading...
Cover image for JavaScript Interview Question #17: Sum of Two Empty Arrays

JavaScript Interview Question #17: Sum of Two Empty Arrays

coderslang profile image Coderslang: Become a Software Engineer Originally published at learn.coderslang.com on ・1 min read

js-test-17

Is the sum of two arrays equal to false?

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

To analyze this code snippet we need to understand how type conversion works in JS.

When we try to sum two arrays using the + operator, the arrays are first converted to strings and then these strings are concatenated.

An empty array [] is evaluated as an empty string. The sum of two empty strings is still an empty string.

Now, is an empty string equal to false or not?

The comparison here is done using the == operator. This operator is used to check loose equality and does implicit type conversion.

In this case, empty string and false are considered equal and the condition of the if statement will be evaluated to true.

If you want to use a strict comparison that respects the types of values you compare, you should use the strict equality operator ===.

Here, you can find more information on basic math operations is JavaScript.


ANSWER: the string same will be logged to the console.

Learn Full Stack JavaScript

Discussion (16)

pic
Editor guide
Collapse
rkennela2 profile image
Ryan Kennel 🐢

I do not want to work at a place that asks me something that won’t be needed on the job and can easily be googled

Collapse
coderslang profile image
Coderslang: Become a Software Engineer Author

The question is not how easy it is to google something. The point is to check the depth of your knowledge.

With this specific question, the interviewer will determine your skills in:

  • type conversion in JS
  • loose equality operator
  • conditional statements
Collapse
rkennela2 profile image
Ryan Kennel 🐢

I appreciate the interesting tidbit of information but it’s not useful to determine the effectiveness of the dev.

I am much more interested in their understanding of OO and functional programming concepts, ability to test drive JS, yarn/npm, frameworks...

Thread Thread
akashkava profile image
Akash Kava

Dev trying to debug an error for couple of hours just because he doesn't understand how concatenation works, is basically waste of company's money and time. A small mistake can lead to unexpected consequences and delay in achieving deadline. Multiply number of such mistakes with the number of devs who do not know such details, the penalty is a huge. And this penalty is actually paid by entire company, every developer in the company because no one will have time to calculate such small mistakes.

Thread Thread
rolfstreefkerk profile image
Rolf Streefkerk

You may put importance on what can be learned, i would be put importance on what someone understands. Anything can be learned and questions like these do not test if someone has the capacity to learn, but rather what he can memorize or has had experience with.

Thread Thread
akashkava profile image
Akash Kava

I believe if one has not learnt the basics in college/graduation, educating an employee isn't responsibility of an employer unless employee is ready to compensate the pay for learning. This has nothing to do with memorizing, memorizing is knowing each properties of elements of periodic table.

Thread Thread
rolfstreefkerk profile image
Rolf Streefkerk

you seem to think that everyone should have learned these "basics", then you live in fairy tale land.

This is about memorization and not about understanding principles and showing the ability to understand those principles and apply them.

There's a difference here between knowing something and understanding. What you're asking for is that people know, what I'm more interested in is if they understand it.

Collapse
andreidascalu profile image
Andrei Dascalu

No, the question itself doesn't check the depth of knowledge unless you state it such that it requires the detailed explanation as part of the answer.

Otherwise, it goes like this: JavaScript is a loosely typed language that performs lots of magic type conversion. Something empty generally does equal false, so does the sum of two empty things? It's a safe bet to say yes.

Thread Thread
coderslang profile image
Coderslang: Become a Software Engineer Author

You're absolutely right. But the issue is that most wannabe Junior devs wouldn't understand what you just said. What I'm trying to do, is to get them thinking a bit deeper than the regular "crash course" curriculums.

And the question absolutely requires an explanation. Otherwise, everyone has at least a 50% chance of success here.

Collapse
brunooliveira profile image
Bruno Oliveira

While this is interesting to see how some things in JS work, it's really not adequate from the point of view of ensuring that the developer knows anything useful for the job. That should be the goal of the interview

Collapse
zacharythomasstone profile image
Zachary Stone

Okay cool.

Collapse
willsmart profile image
willsmart • Edited

Fun fact: any combo of any number of [], 0, "", whitespace (mix-and-match is ok: " \n\n\n\r \t"), ".", "0" (repeated if you like: "000"), null, false added together will loose equal false, so long as:

  • you're careful never to stringify a false or null,
  • or put whitespace between two zeros or a dot and a zero
  • and if you have a dot, there is only one and there must also be at least one zero
  • other rules I haven't thought of
0+"0"+[]+" \n\t\r  " == false // yep, it's "00 \n\t\r  "
false+0+[]+"00000"+[]+"   " == false // yep, it's "000000   "
0000+null+[]+[]+0+'0'+""+[] == false, // totally, "000"
0+false+null+""+[]+[]+'000'+0 + '.' == false // uhhuh, "00000."

but

[]+[]+""+false != false // the false was concatted as "false". "false" isn't falsey.
Enter fullscreen mode Exit fullscreen mode

On a related note, static typing is great 😁 and there are plenty of tools/libs/langs available to code JS without this JS-framer weirdness.

Collapse
coderslang profile image
Coderslang: Become a Software Engineer Author

Awesome comment! Thanks for the additional info.

I think JS is like poker to some extent. It takes a minute to learn and a lifetime to master πŸ˜‚

Collapse
jmitchell38488 profile image
Justin Mitchell • Edited

Ohhhh this is a straight up bad example. I would not ask a junior this.

Yes, tests knowledge, but this is not an example you're ever likely to see in a real application. Anyone using this code who's not a grad or an intern is going to get a talking to and some help.

Loose equality is just equality, you're thinking strict & non strict equality.

This sort of stuff is code from 10+ years ago.

Collapse
zacharythomasstone profile image
Zachary Stone

Thank you for putting this together. I appreciate the additional knowledge behind why things are the way they are in JS.

Collapse
pengeszikra profile image
Peter Vivo

This is the basic idea behind single character line width js code : print "Hello World!"