Skip to content
Advertisements

Number.parseInt()

Why parseInt ?

The Number.parseInt () method is used to convert an array into an integer in JavaScript. If the string does not contain a numeric value, it returns NaN, not a number. Radix returns an integer, which is considered the second parameter of the Number.parseInt () function. The Radix parameter is used to specify which number system to use. If the Radix parameter is not specified, JavaScript assumes the following:

  • If the string starts with “0x”, radix is 16 (hex)
  • If the string starts with “0”, radix is 8 (octal).
  • If the string starts with another value, radix is 10 (decimal).

Syntax:

parseInt(string, radix)

  • string (required): The value to be parsed.
  • radix (optional): Represents the number system to be used.

JavaScript parseInt Examples

Example 1

        let val1 = Number.parseInt(1234);
        let val2 = Number.parseInt(1.55);
        let val3 = Number.parseInt(-8.99);
        let val4 = Number.parseInt(0);
        let val5 = Number.parseInt(null);
        let val6 = Number.parseInt("394");
        let val7 = Number.parseInt("truecodes.org");
        let val8 = Number.parseInt(2000 / 1000);
        let val9 = Number.parseInt(Infinity);
        let val10 = Number.parseInt(-Infinity);
        let val11 = Number.parseInt(0 / 0);
        let val12 = Number.parseInt(function () { });
        let val13 = Number.parseInt(2019 / 02 / 03);

        console.log(val1);
        console.log(val2);
        console.log(val3);
        console.log(val4);
        console.log(val5);
        console.log(val6);
        console.log(val7);
        console.log(val8);
        console.log(val9);
        console.log(val10);
        console.log(val11);
        console.log(val12);
        console.log(val13);

output:

1234
1
-8
0
NaN
394
NaN
2
NaN
NaN
NaN
NaN
336


Example 2

        let one = parseInt("10", 8);
        console.log(one)

        let two = parseInt("0x10");
        console.log(two)

        let three = parseInt("10", 16);
        console.log(three)

        let four = parseInt("10", 10);
        console.log(four)

        let five = parseInt("010");
        console.log(five)

output:

8
16
16
10
10


Browser Support

Chromeyes
Edgeyes
Firefox25
Internet Explorerno
Opera9
Safariyes
Android webviewyes
Chrome for Androidyes
Edge mobileyes
Firefox for Android25
Opera Androidyes
Advertisements

1 Comment »

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: