Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

javascript
parse
int

How to use the parseInt() function in JavaScript

Educative Answers Team

The parseInt() function is a built-in function in JavaScript which allows users to convert a string to an integer value.

Syntax

The basic syntax of the function is:

svg viewer

Parameters

The parseInt() function takes the following parameters as input:

  • String: The string to convert to an Integer value
  • Radix: Specifies the numeral system i.e., the base of the number

Radix is an optional parameter. In case it is not mentioned, the following assumptions are made:

  1. If the string begins with 0x, the base is considered 16 (hexadecimal)
  2. If the string begins with 0, the base is considered to be either 8 (octal) or 10 (decimal), depending on the system implementation
  3. For any other starting value of the input string, the base is assumed to be 10 (decimal)

Return value

If the function parses the string successfully, the integer value is returned. In case the string cannot be converted to an integer value, NaN is returned.

Note: In case the string starts with a numeric character but is followed by an alpha-numeric character, the string is parsed until the occurrence of the first alphanumeric character

Example

Let’s look at some examples to understand further how to use the parseInt() function:

  • JavaScript

RELATED TAGS

javascript
parse
int
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring