Does JavaScript Automatically Convert Types?

JavaScript is built to be innovative and smart. Some other languages aren’t built to auto-convert types to other types, so an expression can be easily evaluated.

In JavaScript, it can automatically change numbers to strings and words to numbers as long as it’s in the appropriate setting.

Take for example:

JavaScript can automatically convert null into a number type

console.log(null * 3);
//0

Notice how the answer is now 0. It’s almost as if null converted into 0.

In fact, that’s exactly what happened!

And when you multiply 0 and 3, you get 0.

JavaScript can convert a string into a number

Check out the example below:

console.log("3" - 2);
//1

Here, you see that “3” is technically a string instead of a number.

In this expression, you are subtracting a string (“3”) by a number (2).

JavaScript intelligently changed “3” into plain old number 3.

3 – 2 = 1.

JavaScript can convert a number into a string?

console.log("3" + 2);
//32

To reiterate and prove what’s going on, this expression of code adds the number (2) to the string (“3”).

It seems here that JavaScript instead has kept the string and converted the number 2 to a string itself.

When you add two strings together, what you’re doing is called concatenation. That’s when you sort of glue two strings together.

In this case, when you add a string “3” to a number 2, JavaScript converts 2 into something like “2”. Then it just “glues the 3 and 2 together, resulting in 32.

But here’s a question for you. You were probably expecting the result to be 5 right? I mean, in the example above where “3” – 2 ended up being 1 means that “3” was converted to the number 3.

However, in this example, you can see that “3” was not converted to the number 3, while the second number 2 was converted to a string like “2” instead!

That’s why we see 32, the concatenation of the strings 3 and 2.

My guess is that in the case of the addition operator, concatenation takes precedence over the concept of adding two numbers.

Can JavaScript convert a string that’s spelled as a number into a number?

This sounds a little confusing but here’s an example of what I’m talking about:

console.log("two" * 2);
//NaN

As you can see here, this line of code has an error message called NaN which stands for Not a Number.

It turns out that JavaScript can’t actually read the string and determine on its own that “five” is the same as a regular number 5.

You can’t really change the letters two into 5. Also, you can’t multiply a string to a number either.

Since they didn’t know how to convert the value, it returned a NaN.

Does JavaScript automatically convert boolean values?

Let’s check out some true and false conversion concepts.

console.log(false == 0);
//true

Do you understand why this results in truth?

It’s because the false eventually converts into 0, and when 0 == 0, then it’s good!

console.log(true == 32);
//false

Now, I’ve deliberately tried setting true to equal to 32 and if JavaScript were to convert this 32 into a boolean (true or false), then this expression would’ve been true.

However, I believe here, true was converted into 1 by javascript, and since 1 doesn’t equal 32,

False.

console.log(true == 1);
//true

Here, it proves that true converts into 1 because that’s the only possible reason that the expression (true == 1) can evaluate as true.

What is type coercion for converting types in JavaScript

When JavaScript automatically converts a type into another, this is called type coercion.

One thing you have to know is that Javascript’s type coercion follows a certain set of rules.

For example, when we evaluated the keyword null in an expression, JavaScript would automatically convert null into 0.

When we evaluated the “3” with the subtraction operator (previously), JavaScript turned it into the number 3.

Can you see what JavaScript is doing? It’s actually changing a string to a number!

However, when you try to evaluate “3” with the addition operator, JavaScript doesn’t convert the 3 into a number. Instead, it changes the 2 into a string and then concatenates it! In this case, we see JavaScript changing a number into a string!

Also, when something isn’t easily converted to a number right away, then JavaScript will NaN. An example of this is “two.”

It doesn’t exactly turn into a 2. I mean you and I both can easily see that this is the number 2, but JavaScript doesn’t know that. It just isn’t built to think that way.

If you continue to evaluate arithmetic operations using the result of NaN, you will continue to get the return of NaN in the next expression, leaving you with a series of errors. You may have to keep an eye out for that!

Are two NaN’s equal to each other?

console.log(NaN == NaN);
//false

Apparently, two NaN’s are not actually equal to each other.

Are null and undefined combinations equal to each other?

console.log(undefined == undefined);
//true

However, undefined and undefined do equal each other.

console.log(null == null);
//true

And two nulls are also equal to each other.

Or how about when we test whether a null is equal to an undefined?

console.log(null == undefined);
//true

So we see here that different arrangements of null and undefined are all equal to each other.

As a result, undefined and null should be considered interchangeable.

There’s one more thing I want to check…

Is null or defined equal to 0?

console.log(null == 0);
//false
console.log(undefined == 0);
//false

So undefined and null are not equal to 0.

Leave a Comment