DEV Community

loading...
Cover image for JS Test #1: Type conversion in JavaScript

JS Test #1: Type conversion in JavaScript

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

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 🚀!
js-test
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

Learn Full Stack JavaScript

Discussion (4)

Collapse
daaitch profile image
Philipp Renoth

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.

Collapse
petermoyano profile image
PeterMoyano

Thanks for the comment, very usefull for us newbies. Where would you recomend checking out some relevant Javascript enterview questions?

Collapse
coderslang profile image
Coderslang: Become a Software Engineer Author

It looks like wouldn't be very happy if someone asked one of those questions during an interview :)

Collapse
jordanaf808 profile image
jordanaf808

so we go from a string, to a boolean, to a number, wow. :)

Forem Open with the Forem app