Data transformations

Often there is a need to convert one data to another. Some transformations are done automatically by javascript. For example:

let number1 = "56";
let number2 = 4;
let result = number1 + number2;
console log(result); // 564

Here the variable number1 represents a string, more specifically the string representation of a number. And the variable number2 represents a number. And in the end, we will get not the number 60, but the string 564.

When adding, transformations in JavaScript are performed according to the principle:

If both operands represent numbers, then normal arithmetic addition occurs.

If the previous condition is not met, then both operands are converted to strings and the strings are concatenated.

Accordingly, in the example above, since the first operand is a string, the second operand is a number that is also converted to a string, and as a result, we get the string “564”, and not the number 60. In fact, we get:

let number1 = "56";
let number2 = 4;
let result = number1 + String(number2);
console log(result); // 564

The function String()and allows you to get the string representation of the value. Thus, the expression String(number2) receives the string representation of number2, that is, from the number 4 it receives the string “4”.

But the default behavior may not always be desirable. For example, in the example above, we want the values ​​to be added up not as strings, but as numbers. In this case, we can use the transform functions.


The parseInt() function is used to convert a string to an integer :

let number1 = "56";
let number2 = 4;
let result = parseInt(number1) + number2;
console log(result); // 60

In this case, the string can have mixed content, for example, “123hello”, that is, in this case, there are numbers, but there are also ordinary characters. The function parseInt()will still try to convert – it sequentially, starting from the first character, reads the numbers until it encounters the first non-digit character:

let num1 = "123hello";
let num2 = parseInt(num1);
console log(num2); // 123

Note that a value that is converted to a number is always converted to a string first. This can lead to unexpected, at first glance, results:

let number1 = parseInt(0.000005); // 0;
console log(number1);

let number2 = parseInt(0.0000005); // 5
console log(number2);

Above, fractional numbers are passed to the parseInt function, and we expect to get the number 0 in both cases. However, when converting number2, we get the number 5. Why?

The example above would be equivalent to the following:

let number1 = parseInt(String(0.000005));   // 0;

let number2 = parseInt(String(0.0000005));  // 5

For fractional numbers less than 10 -6 (0.000001), exponential notation is used, that is, the number 0.0000005 is represented as 5e-7:

console.log(0.0000005);     // 5e-7

Next, the number 5e-7 is converted to the string “5e-7”, and this string parseInt tries to convert to a number. Accordingly, the output is the number 5.

The same goes for very large numbers like 999999999999999999999, which are also represented in exponential form.

NaN and isNaN

If the function parseInt()fails to convert, it returns NaN (Not a Number), which indicates that the string does not represent a number and cannot be converted.

console.log(parseInt("abc"));   // NaN
let type = typeof NaN;
console.log(type);              // number

Interestingly, the NaN value itself (not a number) represents the number type.

The isNaN() special function can be used to check if a string represents a number. If the string is not a number, then the function returns true, if it is a number, then false:

let num1 = "javascript";
let num2 = "22";
let result = isNaN(num1);
console.log(result); // true - num1 is not a number
result = isNaN(num2);
console.log(result); //  false - num2 - this number

Conversion into another number system

Above, we considered the translation of strings into numbers in the decimal system. But we can use the second parameter to explicitly indicate that we want to convert a string to a number on a specific system. For example, converting to a number in binary:

let num1 = "110";
let num2 = parseInt(num1, 2);
console log(num2); // 6

The result will be 6 since 110 in binary is the number 6 in decimal.


To convert strings to fractional numbers, the parseFloat() function is used, which works in a similar way:

let number1 = "46.07";
let number2 = "4.98";
let result = parseFloat(number1) + parseFloat(number2);
console log(result); //51.05