parseInt()

  • Revision slug: JavaScript/Reference/Global_Objects/parseInt
  • Revision title: parseInt
  • Revision id: 78061
  • Created:
  • Creator: Dhtmlkitchen@gmail.com
  • Is current revision? No
  • Comment Code formatting; 8 words added, 6 words removed

Revision Content

 

Summary

Parses a string argument and returns an integer of the specified radix or base.

Core Global Method
Implemented in JavaScript ?
ECMAScript Edition ECMAScript ?

Syntax

var intValue = parseInt(string[, radix]);

Parameters

string 
The value to parse. If string is not a string, then it is converted to one. Leading whitespace in the string is ignored.
radix 
An integer that represents the radix of the above mentioned string. While this parameter is optional, always specify it to eliminate reader confusion and to guarantee predictable behavior.  Different implementations produce different results when a radix is not specified.

Description

parseInt is a top-level function and is not associated with any object.

Function parseInt 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, JavaScript assumes the following:

  • If the input string begins with "0x" or "0X", radix is 16 (hexadecimal).
  • If the input string begins with "0", radix is eight (octal). This feature is non-standard, and some implementations deliberately do not support it (instead using the radix 10).  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 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

Example: Using parseInt

The following examples all return 15:

parseInt(" 0xF", 16);
parseInt(" F", 16);
parseInt("17", 8);
parseInt(021, 8);
parseInt("015", 10);
parseInt(15.99, 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(-10, 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, many implementations interpret a numeric string beginning with a leading 0 as octal. The following may have an octal result.

 

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. If radix is 16, number may also optionally begin with the character pairs 0x or 0X.

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

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

See also

isNaN, parseFloat, Object.valueOf, Number.toString

 

 

{{ languages( { "es": "es/Referencia_de_JavaScript_1.5/Funciones_globales/parseInt", "fr": "fr/R\u00e9f\u00e9rence_de_JavaScript_1.5_Core/Fonctions_globales/parseInt", "ja": "ja/Core_JavaScript_1.5_Reference/Global_Functions/parseInt", "pl": "pl/Dokumentacja_j\u0119zyka_JavaScript_1.5/Funkcje/parseInt" } ) }}

Revision Source

<p> </p>
<h3 name="Summary">Summary</h3>
<p>Parses a string argument and returns an integer of the specified radix or base.</p>
<table class="standard-table"> <thead> <tr> <th class="header" colspan="2">Core Global Method</th> </tr> </thead> <tbody> <tr> <td>Implemented in</td> <td>JavaScript ?</td> </tr> <tr> <td>ECMAScript Edition</td> <td>ECMAScript ?</td> </tr> </tbody>
</table>
<h3 name="Syntax">Syntax</h3>
<pre class="eval">var intValue = parseInt(<em>string</em>[, <em>radix</em>]);
</pre>
<h3 name="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 one. Leading whitespace in the string is ignored.</dd>
</dl>
<dl> <dt><code>radix</code> </dt> <dd>An integer that represents the <var>radix</var> of the above mentioned string. While this parameter is optional, <strong>always specify it</strong> to eliminate reader confusion and to guarantee predictable behavior.  Different implementations produce different results when a radix is not specified.</dd>
</dl>
<h3 name="Description">Description</h3>
<p><code>parseInt</code> is a top-level function and is not associated with any object.</p>
<p>Function <code>parseInt</code> 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, JavaScript assumes the following:</p>
<ul> <li>If the input <code>string</code> begins with "0x" or "0X", <var>radix</var> is 16 (hexadecimal).</li> <li>If the input <code>string</code> begins with "0", <var>radix</var> is eight (octal). This feature is non-standard, and some implementations deliberately do not support it (instead using the radix 10).  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 <code><a href="/en/JavaScript/Reference/Global_Objects/isNaN" title="en/Core_JavaScript_1.5_Reference/Global_Functions/isNaN">isNaN</a></code> 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>
<h3 name="Examples">Examples</h3>
<h4 name="Example:_Using_parseInt">Example: Using <code>parseInt</code></h4>
<p>The following examples all return 15:</p>
<pre class="eval">parseInt(" 0xF", 16);
parseInt(" F", 16);
parseInt("17", 8);
parseInt(021, 8);
parseInt("015", 10);
parseInt(15.99, 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 <code>NaN</code>:</p>
<pre class="eval">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 <code>-15:</code></p>
<pre class="eval">parseInt("-F", 16);
parseInt("-0F", 16);
parseInt("-0XF", 16);
parseInt(-10, 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 <code>224:</code></p>
<pre class="eval">parseInt("0e0", 16);
</pre>
<h3>Octal Interpretations with No Radix</h3>
<p>Although discouraged by ECMAScript 3, many implementations interpret a numeric string beginning with a leading <code>0</code> as octal. The following may have an octal result.</p>
<p> </p>
<pre class="eval">parseInt("0e0"); // 0
parseInt("08"); // 0, '8' is not an octal digit.
</pre>
<blockquote>
<h4>ECMAScript 5 Removes Octal Interpretation</h4>
<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. If radix is <code>16</code>, number may also optionally begin with the character pairs <code>0x</code> or <code>0X</code>.</p>
<p>This differs from ECMAScript 3, which discouraged but allowed octal interpretation.</p>
<p>Since many implementations have not adopted this behavior as of 2011, and because older browsers must be supported, <strong>always specify a radix</strong>.</p>
<h3 name="See_also">See also</h3>
<p><a href="/en/JavaScript/Reference/Global_Objects/isNaN" title="en/Core_JavaScript_1.5_Reference/Global_Functions/isNaN">isNaN</a>, <a href="/en/JavaScript/Reference/Global_Objects/parseFloat" title="en/Core_JavaScript_1.5_Reference/Global_Functions/parseFloat">parseFloat</a>, <a href="/en/JavaScript/Reference/Global_Objects/Object/valueOf" title="en/Core_JavaScript_1.5_Reference/Global_Objects/Object/valueOf">Object.valueOf</a>, <a href="/en/JavaScript/Reference/Global_Objects/Number/toString" title="en/Core_JavaScript_1.5_Reference/Global_Objects/Number/toString">Number.toString</a></p>
<p> </p>
<p> </p>
<p>{{ languages( { "es": "es/Referencia_de_JavaScript_1.5/Funciones_globales/parseInt", "fr": "fr/R\u00e9f\u00e9rence_de_JavaScript_1.5_Core/Fonctions_globales/parseInt", "ja": "ja/Core_JavaScript_1.5_Reference/Global_Functions/parseInt", "pl": "pl/Dokumentacja_j\u0119zyka_JavaScript_1.5/Funkcje/parseInt" } ) }}</p>
</blockquote>
Revert to this revision