title: type conversion not working javascript
body: <p>I created a rather large function that takes in an argument, and based on the number's size, properly formats it. It works as expected for any values with the <code>typeof</code> number, however there are a few instances where I need to convert a string to a numeric value. I am trying to do that by using <code>parseInt</code> if its type is not <code>number</code>. When I <code>console.log</code> after the first if-statement, it says its <code>typeof</code> is now number. However when any string is passed through, say, "10000", it just shows up as 10000, and not the expected output of 10,000 with the proper formatting. Below is my code...are there any glaring ways that I might be going about it wrong?</p>
  
  <pre><code>function formatNumber(number) {
      if (typeof number !== 'number') {
          number = parseInt(number);
          return number;
      }
  
      let decimals = 2;
      const isNegative = number &lt; 0;
      const rounded = (number &gt;= 1e+6) ? Math.round(number) : number;
  
      const isInteger = () =&gt; parseInt(number, 10) === parseFloat(number);
  
      const abs = Math.abs(rounded);
      if (abs &lt; 1e+6) {
          decimals = isInteger(abs) ? 0 : decimals;
      }
  
      const formatter = val =&gt; {
          const string = Math.abs(val).toString();
          let formatted = '';
  
          for (let i = string.length - 1, d = 1; i &gt;= 0; i--, d++) {
              formatted = string[i] + formatted;
              if (i &gt; 0 &amp;&amp; d % 3 === 0) {
                  formatted = `,${formatted}`;
              }
          }
          return formatted;
      };
  
      const formatLargeNumbers = (value, decimalPlaces) =&gt; {
          let adjustedValue;
          if (value &gt;= 1e+12) {
              adjustedValue = parseFloat((value / 1e+12).toFixed(decimals));
              return `&lt;span title=${formatter(value)}&gt;${adjustedValue}T&lt;/span&gt;`;
          }
          if (value &gt;= 1e+9) {
              adjustedValue = parseFloat((value / 1e+9).toFixed(decimals));
              return `&lt;span title=${formatter(value)}&gt;${adjustedValue}B&lt;/span&gt;`;
          }
          if (value &gt;= 1e+6) {
              adjustedValue = parseFloat((value / 1e+6).toFixed(decimals));
              return `&lt;span title=${formatter(value)}&gt;${adjustedValue}M&lt;/span&gt;`;
          }
          if (value &gt;= 1e+3 &amp;&amp; value &lt; 1e+6) {
              return value.toLocaleString('en-EN', {
                  minimumFractionDigits: decimalPlaces,
                  maximumFractionDigits: decimalPlaces,
              });
          }
          return value.toFixed(decimals);
      };
  
      if (isNegative) {
          let negativeNumber = Math.abs(rounded);
          if (negativeNumber &gt;= 1e+3) {
              negativeNumber = formatLargeNumbers(negativeNumber);
          }
          return `&lt;span class="negative-value"&gt;(${negativeNumber})&lt;/span&gt;`;
      }
  
      return formatLargeNumbers(rounded, decimals);
  }
  </code></pre>