0 added
0 removed
Original
2026-01-01
Modified
2026-03-09
1
<p>You can cast number to string using the .toString() command. But sometimes, on the contrary, you would need to turn a string into a number to compare them.</p>
1
<p>You can cast number to string using the .toString() command. But sometimes, on the contrary, you would need to turn a string into a number to compare them.</p>
2
<p>To do this, you can use the parseInt() command. Example:</p>
2
<p>To do this, you can use the parseInt() command. Example:</p>
3
var string = '1'; console.log(string); // Logs string: "1" (string) console.log(parseInt(string, 10)); // Logs number: 1 (number)<p>Note that the command has two arguments:</p>
3
var string = '1'; console.log(string); // Logs string: "1" (string) console.log(parseInt(string, 10)); // Logs number: 1 (number)<p>Note that the command has two arguments:</p>
4
<ol><li>the string we are trying to turn into a number;</li>
4
<ol><li>the string we are trying to turn into a number;</li>
5
<li>the base of the number system into which we cast the number.</li>
5
<li>the base of the number system into which we cast the number.</li>
6
</ol><p>We are used to working in decimal number system, but in programming we often have to deal with other systems: binary, octal, hexadecimal. This is why parseInt() has a second argument. And although now we are not going into the intricacies of number systems, please do not forget about this feature of the parseInt() command and always explicitly specify the second argument to avoid errors.</p>
6
</ol><p>We are used to working in decimal number system, but in programming we often have to deal with other systems: binary, octal, hexadecimal. This is why parseInt() has a second argument. And although now we are not going into the intricacies of number systems, please do not forget about this feature of the parseInt() command and always explicitly specify the second argument to avoid errors.</p>
7
<p>We will talk more about this command and its features as part of one of the following courses.</p>
7
<p>We will talk more about this command and its features as part of one of the following courses.</p>