JavaScript Interview Tips — Implicit Type Conversion

After attending couple of interviews, I would like to start recording down what I have been asked frequently. I hope it can not only help myself to prepare for future interviews, but also give you a direction about what you may be asked in JavaScript technical interviews.

Final Puzzle Piece Falls Into Place — by lexaarts

Implicit type conversion is a basic JavaScript feature to coerce an unexpected value type to the expected type. In simple, when you , it shows instead of . It is because the engine coverts your number into a string as it expects a string when you use the operator in a string (which is concatenation instead of addition).

This sounds like a super basic introduction of JavaScript programming, but this is also the tricky point. You don’t need to understand very deeply in this topics to start JavaScript programming, which becomes a blind spot. The questions about this topic are very conceptual. If you haven’t prepared well enough, you will find it very difficult to answer such questions simply by your experience.

First, I would like start with some sample questions that your interviewers may ask.

Can you predict all the above? If you feel struggling answering any of them, I recommend you to read through this article before attending your interviews. There are three tips for you to answer this type of question.

Tips 1: Determine the expected type

First thing first, if you want to know the output, you need to know the expected type first. For example, when assigning thing into an object, , the engine expects a string as key. Thus, the engine will always try to convert a non-string key into a string.

By applying this rule, we can find out that the effect will totally the same whether you use an integer or a string to assign value. All arithmetic operators ( , , , etc) except plus () expect numbers. If you use these operators, the engine will always try to convert your operands into numbers. For plus operator, it has two meaning, which are addition and concatenation, while concatenation has a higher priority. It means if both any of the operand is string, it will be concatenation.

It is addition here.

As one of the operand is string, it is concatenation here, which converts all non-string to string.

Tips 2: From left to right

The associativity of JavaScript is left-to-right. It means for multiple operators with the same precedence, they should be parsed from left to right. For example, given , the engine actually parses it as .

When handling an expression with different types, we should break it down one by one. For example,

can be parsed as

such that all operators except the first one become concatenation.

Tips 3: Determine the corresponding value after conversion

When it is a digit, it can be converted to string or number freely. Yet, when it is a non-digit, or even an object, it is essential to know what corresponding value it will be converted to.

From number or boolean to string

This one is straight forward. All number can find a corresponding string representation, e.g. to . For boolean, just the string or .

From string to number

This one is more tricky. It will be okay for all digit, just as what mentioned above, e.g. to . This also applies to decimal, to . For all non-numeric value, it will becomes , which means “Not a Number”, to . The tricky point here is that is a number. () If you use on it with another number, it will be addition instead of concatenation. Yet, any arithmetic expression between and number will still result to .

Besides, it won’t memorize your original value, so when you convert it back to string, it will just become . Below is an example when combing with Tips 1:

For the first line, it runs into concatenation. For the second line, the engine converts into and is . Last but not least, there is also one more edge case, which is empty string. Empty string is evaluated as zero, gives .

From boolean to number

Basic computing science concept, to 1, to 0. That’s all.

From object (including array) to number or string

This is the most tricky one. When converting to string, it will call the method. When converting to number, it will call the method. For most default object, it will be when converting into number, when converting in to string.

Well, how about arrays?

LOL, arrays have their own methods.

When you use an object as a key, the engine will just use its corresponding value.

From anything to boolean (falsy and truthy)

If you have been playing with JavaScript for a while, you should already be quite familiar to this one. Hence, I am not going to dig deep for this one. In simple, all these below will be evaluated as false when converting into booelan: , , , , , , . You may check this out for a more detailed explanation.

I have been using JavaScript for several years. Yet, I were screwed up really hard first time I face these kinds of question. My interviewer’s face was like “Wow, you cannot answer it? I thought it was something every JS developer will learn first. LOL.” It was really embarrassing, especially I were actually interviewing for a senior position. Hope this article can save you from that. Please let me know if it is useful for you. Good luck!

Web developer from Hong Kong. Most interested in Angular and Vue. Currently working on a Nuxt.js + NestJS project.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store