Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 1106459 of parseInt()

  • Revision slug: Web/JavaScript/Reference/Global_Objects/parseInt
  • Revision title: parseInt()
  • Revision id: 1106459
  • Created:
  • Creator: johlits
  • Is current revision? No
  • Comment
Tags: 

Revision Content

{{jsSidebar("Objects")}}

The parseInt() function parses a string argument and returns an integer of the specified radix (the base in mathematical numeral systems).

Syntax

parseInt(string, radix);

Parameters

string
The value to parse. If string is not a string, then it is converted to a string (using the ToString abstract operation). Leading whitespace in the string is ignored.
radix
An integer between 2 and 36 that represents the radix (the base in mathematical numeral systems) of the above mentioned string. Specify 10 for the decimal numeral system commonly used by humans. Always specify this parameter to eliminate reader confusion and to guarantee predictable behavior. Different implementations produce different results when a radix is not specified, usually defaulting the value to 10.

Return value

An integer number parsed from the given string. If the first character cannot be converted to a number, {{jsxref("NaN")}} is returned.

Description

The parseInt function converts its first argument to a string, parses it, and returns an integer or NaN. If not NaN, the returned value will be the decimal integer representation of the first argument taken as a number in the specified radix (base). For example, a radix of 10 indicates to convert from a decimal number, 8 octal, 16 hexadecimal, and so on. For radices above 10, the letters of the alphabet indicate numerals greater than 9. For example, for hexadecimal numbers (base 16), A through F are used.

If parseInt encounters a character that is not a numeral in the specified radix, it ignores it and all succeeding characters and returns the integer value parsed up to that point. parseInt truncates numbers to integer values. Leading and trailing spaces are allowed.

If radix is undefined or 0 (or absent), JavaScript assumes the following:

  • If the input string begins with "0x" or "0X", radix is 16 (hexadecimal) and the remainder of the string is parsed.
  • If the input string begins with "0", radix is eight (octal) or 10 (decimal).  Exactly which radix is chosen is implementation-dependent.  ECMAScript 5 specifies that 10 (decimal) is used, but not all browsers support this yet.  For this reason always specify a radix when using parseInt.
  • If the input string begins with any other value, the radix is 10 (decimal).

If the first character cannot be converted to a number, parseInt returns NaN.

For arithmetic purposes, the NaN value is not a number in any radix. You can call the {{jsxref("isNaN")}} function to determine if the result of parseInt is NaN. If NaN is passed on to arithmetic operations, the operation results will also be NaN.

To convert number to its string literal in a particular radix use intValue.toString(radix).

Examples

Using parseInt

The following examples all return 15:

parseInt(" 0xF", 16);
parseInt(" F", 16);
parseInt("17", 8);
parseInt(021, 8);
parseInt("015", 10);   // parseInt(015); will return 13
parseInt(15.99, 10);
parseInt("15,123", 10);
parseInt("FXX123", 16);
parseInt("1111", 2);
parseInt("15*3", 10);
parseInt("15e2", 10);
parseInt("15px", 10);
parseInt("12", 13);

The following examples all return NaN:

parseInt("Hello", 8); // Not a number at all
parseInt("546", 2);   // Digits are not valid for binary representations

The following examples all return -15:

parseInt("-F", 16);
parseInt("-0F", 16);
parseInt("-0XF", 16);
parseInt(-15.1, 10)
parseInt(" -17", 8);
parseInt(" -15", 10);
parseInt("-1111", 2);
parseInt("-15e1", 10);
parseInt("-12", 13);

The following example returns 224:

parseInt("0e0", 16);

Octal interpretations with no radix

Although discouraged by ECMAScript 3 and forbidden by ECMAScript 5, many implementations interpret a numeric string beginning with a leading 0 as octal. The following may have an octal result, or it may have a decimal result.  Always specify a radix to avoid this unreliable behavior.

parseInt("0e0"); // 0
parseInt("08"); // 0, '8' is not an octal digit.

ECMAScript 5 removes octal interpretation

The ECMAScript 5 specification of the function parseInt no longer allows implementations to treat Strings beginning with a 0 character as octal values. ECMAScript 5 states:

The parseInt function produces an integer value dictated by interpretation of the contents of the string argument according to the specified radix. Leading white space in string is ignored. If radix is undefined or 0, it is assumed to be 10 except when the number begins with the character pairs 0x or 0X, in which case a radix of 16 is assumed.

This differs from ECMAScript 3, which discouraged but allowed octal interpretation.

Many implementations have not adopted this behavior as of 2013, and because older browsers must be supported, always specify a radix.

A stricter parse function

It is sometimes useful to have a stricter way to parse int values. Regular expressions can help:

filterInt = function (value) {
  if(/^(\-|\+)?([0-9]+|Infinity)$/.test(value))
    return Number(value);
  return NaN;
}

console.log(filterInt('421'));               // 421
console.log(filterInt('-421'));              // -421
console.log(filterInt('+421'));              // 421
console.log(filterInt('Infinity'));          // Infinity
console.log(filterInt('421e+0'));            // NaN
console.log(filterInt('421hop'));            // NaN
console.log(filterInt('hop1.61803398875'));  // NaN
console.log(filterInt('1.61803398875'));     // NaN

Specifications

Specification Status Comment
{{SpecName('ES1')}} {{Spec2('ES1')}} Initial definition.
{{SpecName('ES5.1', '#sec-15.1.2.2', 'parseInt')}} {{Spec2('ES5.1')}}  
{{SpecName('ES6', '#sec-parseint-string-radix', 'parseInt')}} {{Spec2('ES6')}}  
{{SpecName('ESDraft', '#sec-parseint-string-radix', 'parseInt')}} {{Spec2('ESDraft')}}  

Browser compatibility

{{CompatibilityTable}}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatUnknown}} {{CompatVersionUnknown}}
Parses leading-zero strings as decimal, not octal {{CompatVersionUnknown}} 21 {{CompatVersionUnknown}} (in standards mode) {{CompatUnknown}} {{CompatVersionUnknown}}
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}}
Parses leading-zero strings as decimal, not octal {{CompatUnknown}} {{CompatUnknown}} 21 {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}

See also

  • {{jsxref("Global_Objects/parseFloat", "parseFloat()")}}
  • {{jsxref("Number.parseFloat()")}}
  • {{jsxref("Number.parseInt()")}}
  • {{jsxref("Global_Objects/isNaN", "isNaN()")}}
  • {{jsxref("Number.toString()")}}
  • {{jsxref("Object.valueOf")}}

Revision Source

<div>{{jsSidebar("Objects")}}</div>

<p>The <code><strong>parseInt()</strong></code> function parses a string argument and returns an integer of the specified radix (the base in mathematical numeral systems).</p>

<h2 id="Syntax">Syntax</h2>

<pre class="syntaxbox">
parseInt(<em>string</em>, <em>radix</em>);</pre>

<h3 id="Parameters">Parameters</h3>

<dl>
 <dt><code>string</code></dt>
 <dd>The value to parse. If <code>string</code> is not a string, then it is converted to a string (using the <code><a href="https://www.ecma-international.org/ecma-262/6.0/#sec-tostring">ToString</a></code> abstract operation). Leading whitespace in the string is ignored.</dd>
</dl>

<dl>
 <dt><code>radix</code></dt>
 <dd>An integer between 2 and 36 that represents the <var>radix</var> (the base in mathematical numeral systems) of the above mentioned string. Specify <code>10</code> for the decimal numeral system commonly used by humans. <strong>Always specify this parameter</strong> to eliminate reader confusion and to guarantee predictable behavior. Different implementations produce different results when a radix is not specified, usually defaulting the value to 10.</dd>
</dl>

<h3 id="Return_value">Return value</h3>

<p>An integer number parsed from the given string. If the first character cannot be converted to a number, {{jsxref("NaN")}} is returned.</p>

<h2 id="Description">Description</h2>

<p>The <code>parseInt</code> function converts its first argument to a string, parses it, and returns an integer or <code>NaN</code>. If not <code>NaN</code>, the returned value will be the decimal integer representation of the first argument taken as a number in the specified <var>radix</var> (base). For example, a <var>radix</var> of 10 indicates to convert from a decimal number, 8 octal, 16 hexadecimal, and so on. For radices above <code>10</code>, the letters of the alphabet indicate numerals greater than <code>9</code>. For example, for hexadecimal numbers (base 16), <code>A</code> through <code>F</code> are used.</p>

<p>If <code>parseInt</code> encounters a character that is not a numeral in the specified radix, it ignores it and all succeeding characters and returns the integer value parsed up to that point. <code>parseInt</code> truncates numbers to integer values. Leading and trailing spaces are allowed.</p>

<p>If <var>radix</var> is <code>undefined</code> or 0 (or absent), JavaScript assumes the following:</p>

<ul>
 <li>If the input <code>string</code> begins with "0x" or "0X", <var>radix</var> is 16 (hexadecimal) and the remainder of the string is parsed.</li>
 <li>If the input <code>string</code> begins with "0", <var>radix</var> is eight (octal) or 10 (decimal).&nbsp; Exactly which radix is chosen is implementation-dependent.&nbsp; ECMAScript 5 specifies that 10 (decimal) is used, but not all browsers support this yet.&nbsp; For this reason <strong>always specify a radix when using <code>parseInt</code></strong>.</li>
 <li>If the input <code>string</code> begins with any other value, the radix is 10 (decimal).</li>
</ul>

<p>If the first character cannot be converted to a number, <code>parseInt</code> returns <code>NaN</code>.</p>

<p>For arithmetic purposes, the <code>NaN</code> value is not a number in any radix. You can call the {{jsxref("isNaN")}} function to determine if the result of <code>parseInt</code> is <code>NaN</code>. If <code>NaN</code> is passed on to arithmetic operations, the operation results will also be <code>NaN</code>.</p>

<p>To convert number to its string literal in a particular radix use <code>intValue.toString(radix)</code>.</p>

<h2 id="Examples">Examples</h2>

<h3 id="Using_parseInt">Using <code>parseInt</code></h3>

<p>The following examples all return <strong><code>15</code></strong>:</p>

<pre class="brush: js">
parseInt(" 0xF", 16);
parseInt(" F", 16);
parseInt("17", 8);
parseInt(021, 8);
parseInt("015", 10);   // parseInt(015); will return 13
parseInt(15.99, 10);
parseInt("15,123", 10);
parseInt("FXX123", 16);
parseInt("1111", 2);
parseInt("15*3", 10);
parseInt("15e2", 10);
parseInt("15px", 10);
parseInt("12", 13);
</pre>

<p>The following examples all return <strong><code>NaN</code></strong>:</p>

<pre class="brush: js">
parseInt("Hello", 8); // Not a number at all
parseInt("546", 2);   // Digits are not valid for binary representations
</pre>

<p>The following examples all return <strong><code>-15</code></strong>:</p>

<pre class="brush: js">
parseInt("-F", 16);
parseInt("-0F", 16);
parseInt("-0XF", 16);
parseInt(-15.1, 10)
parseInt(" -17", 8);
parseInt(" -15", 10);
parseInt("-1111", 2);
parseInt("-15e1", 10);
parseInt("-12", 13);
</pre>

<p>The following example returns <strong><code>224</code></strong>:</p>

<pre class="brush: js">
parseInt("0e0", 16);
</pre>

<h2 id="Octal_interpretations_with_no_radix">Octal interpretations with no radix</h2>

<p>Although discouraged by ECMAScript 3 and forbidden by ECMAScript 5, many implementations interpret a numeric string beginning with a leading <code>0</code> as octal. The following may have an octal result, or it may have a decimal result.&nbsp; <strong>Always specify a radix to avoid this unreliable behavior.</strong></p>

<pre class="brush: js">
parseInt("0e0"); // 0
parseInt("08"); // 0, '8' is not an octal digit.
</pre>

<h3 id="ECMAScript_5_removes_octal_interpretation">ECMAScript 5 removes octal interpretation</h3>

<p>The ECMAScript 5 specification of the function <code>parseInt</code> no longer allows implementations to treat Strings beginning with a <code>0</code> character as octal values. ECMAScript 5 states:</p>

<p>The <code>parseInt</code> function produces an integer value dictated by interpretation of the contents of the string argument according to the specified radix. Leading white space in string is ignored. If radix is undefined or <code>0</code>, it is assumed to be <code>10</code> except when the number begins with the character pairs <code>0x</code> or <code>0X</code>, in which case a radix of 16 is assumed.</p>

<p>This differs from ECMAScript 3, which discouraged but allowed octal interpretation.</p>

<p>Many implementations have not adopted this behavior as of 2013, and because older browsers must be supported, <strong>always specify a radix</strong>.</p>

<h2 id="A_stricter_parse_function">A stricter parse function</h2>

<p>It is sometimes useful to have a stricter way to parse int values. Regular expressions can help:</p>

<pre class="brush: js">
filterInt = function (value) {
  if(/^(\-|\+)?([0-9]+|Infinity)$/.test(value))
    return Number(value);
  return NaN;
}

console.log(filterInt('421'));               // 421
console.log(filterInt('-421'));              // -421
console.log(filterInt('+421'));              // 421
console.log(filterInt('Infinity'));          // Infinity
console.log(filterInt('421e+0'));            // NaN
console.log(filterInt('421hop'));            // NaN
console.log(filterInt('hop1.61803398875'));  // NaN
console.log(filterInt('1.61803398875'));     // NaN
</pre>

<h2 id="Specifications">Specifications</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('ES1')}}</td>
   <td>{{Spec2('ES1')}}</td>
   <td>Initial definition.</td>
  </tr>
  <tr>
   <td>{{SpecName('ES5.1', '#sec-15.1.2.2', 'parseInt')}}</td>
   <td>{{Spec2('ES5.1')}}</td>
   <td>&nbsp;</td>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-parseint-string-radix', 'parseInt')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td>&nbsp;</td>
  </tr>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-parseint-string-radix', 'parseInt')}}</td>
   <td>{{Spec2('ESDraft')}}</td>
   <td>&nbsp;</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility">Browser compatibility</h2>

<p>{{CompatibilityTable}}</p>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
  </tr>
  <tr>
   <td>Parses leading-zero strings as decimal, not octal</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>21</td>
   <td>{{CompatVersionUnknown}} (in standards mode)</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Chrome for Android</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>IE Mobile</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
  </tr>
  <tr>
   <td>Parses leading-zero strings as decimal, not octal</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>21</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<h2 id="See_also">See also</h2>

<ul>
 <li>{{jsxref("Global_Objects/parseFloat", "parseFloat()")}}</li>
 <li>{{jsxref("Number.parseFloat()")}}</li>
 <li>{{jsxref("Number.parseInt()")}}</li>
 <li>{{jsxref("Global_Objects/isNaN", "isNaN()")}}</li>
 <li>{{jsxref("Number.toString()")}}</li>
 <li>{{jsxref("Object.valueOf")}}</li>
</ul>
Revert to this revision