Automatic type conversion

As you already know, strings in JavaScript must be quoted. It may occure that all characters of the string will be numbers:

let a = '123';

In this case, the variable a is also a string, but with some nuance.

The essence of the nuance is as follows: if you perform any mathematical operation on a string with numbers, this one will be performed as if we really had numbers, not strings:

alert('2' * '3'); // shows 6

In this case, JavaScript registers our try to perform an operation that is illegal for strings, but valid for numbers.

It also registers that our strings are actually quoted numbers. Therefore, JavaScript automatically converts these strings to numbers and performs the appropriate mathematical operation on them.

This works if our strings contain only numbers. If there is at least one letter - JavaScript will consider the operation incorrect and return value NaN as the result of it:

alert('2' * '3s'); // shows NaN

Also, automatic conversion occurs if the operation is mathematically correct but invalid for strings.

For example, strings cannot (but numbers can) be multiplied, divided, subtracted, so in all such operations there will be an automatic conversion of strings with numbers into numbers.

However, strings, like numbers, are added using the + operator. It means that in the case of addition, there will be no automatic transformation:

let a = '1' + '2'; alert(a); // shows '12' - strings concatenation happened

If one of the terms is a number and the other is a string, then the string always wins:

let a = 1 + '2'; alert(a); // shows '12'

In such cases, JavaScript, on the contrary, automatically converts numbers to strings and performs strings concatenation.

However, there are nuances when there are more than two terms: in this case, the conversion to a specific type depends on the order in which the operations are performed.

In the following example, first 2 and 3 will be added as numbers, and then the result will be added to the string '1' already as the string '5', resulting in the string '15':

let a = '1' + (2 + 3); alert(a); // shows '15'

If you remove the parentheses, then everything will be added from left to right. That is, first the number 2 will be added with the string '1' and the result will be the string '12'. Then the number 3 will be added to this string and the result will be the string '123':

let a = '1' + 2 + 3; alert(a); // shows '123'

But in the next example, the numbers 2 and 3 are written at the beginning, so they will first be added as numbers, resulting in the number 5, and then to the string '1' will be added to this number, resulting in the string '51':

let a = 2 + 3 + '1'; alert(a); // shows '51'

Without running the code, determine what will be displayed on the screen:

let a = '5' + '2'; alert(a);

Without running the code, determine what will be displayed on the screen:

let a = '5' + 2; alert(a);

Without running the code, determine what will be displayed on the screen:

let a = 5 + '2'; alert(a);

Without running the code, determine what will be displayed on the screen:

let a = 5 + 2; alert(a);

Without running the code, determine what will be displayed on the screen:

let a = '5' * '2'; alert(a);

Without running the code, determine what will be displayed on the screen:

let a = '5' - '2'; alert(a);

Without running the code, determine what will be displayed on the screen:

let a = '5' / '2'; alert(a);

Without running the code, determine what will be displayed on the screen:

let a = '5' % '2'; alert(a);

Without running the code, determine what will be displayed on the screen:

let a = '5s' * '2'; alert(a);

Without running the code, determine what will be displayed on the screen:

let a = '5s' + '2'; alert(a);

Without running the code, determine what will be displayed on the screen:

let a = (-'5') + (-'2'); alert(a);

Without running the code, determine what will be displayed on the screen:

let a = '5' * 1 + '2' * 1; alert(a);

Without running the code, determine what will be displayed on the screen:

let a = '5' * '1' + '2' * '1'; alert(a);

Without running the code, determine what will be displayed on the screen:

let a = '' + 3 + 1; alert(a);
enru