A couple of months back, I've started posting colorful JavaScript code snippets that you can use to check your JS skills on my blog. Every problem has the correct answer and explanation. But I strongly suggest you first try to solve it yourself.
Every day in February I'm going to post a new JS test here on Dev.to. Follow, like, comment if it's something you're interested in.
Let's go 🚀!
What’s going to be printed to the console?
.
.
.
.
.
.
.
.
.
.
.
.
.
In the first line, we define the variable str
and initialize it as a string with the value 1
.
In the second line, there are two typecasts, first !str
gives us false
, and then +false
converts boolean
into a number 0
.
Eventually, in the third line, the typeof
operator looks up the current type of str
which is number
.
ANSWER: string number
will be printed to the console
Top comments (4)
Hey coderslang,
I'm not quite so happy the way you as "coderslang: become a SE" present those interview questions like they are "normal" to the JS world. They are not. Most of them are ugly JS edge-cases no one wants to have in their repository and should be removed in code reviews before they are checked in.
For a senior JS dev it might make sense to understand them, but at least it's enough to know that you should not write such code. Things like
0.1 + 0.1 + 0.1 !== 0.3
is fine to know, but e.g. for "adding arrays" like[1, 1] + [2, 2]
you should agree that even if the result is what you want, you better not write it like that and confuse everybody. Why should you? Typescript will not let you do this because it's rubbish, so why is it worth to ask in an interview?If you as a JS dev get such interview questions, leave immediately and apply for the next job :D, really. Either the company asking you such questions is supposed to have large legacy code bases, because most of the questions are about JS edge cases nobody wants to read or should ever write, but they have right in their projects, or they have no idea which kind of JS dev they want to hire.
Companies with modern experienced JS/TS teams will not ask you such questions or at least only a few just for fun.
Thanks for the comment, very usefull for us newbies. Where would you recomend checking out some relevant Javascript enterview questions?
It looks like wouldn't be very happy if someone asked one of those questions during an interview :)
so we go from a string, to a boolean, to a number, wow. :)