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.
Top comments (16)
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
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:
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...
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.
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.
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.
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.
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.
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.
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
Okay cool.
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 equalfalse
, so long as:false
ornull
,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.
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 😂
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.
Thank you for putting this together. I appreciate the additional knowledge behind why things are the way they are in JS.
This is the basic idea behind single character line width js code : print "Hello World!"