parseInt() in JavaScript

  • parseInt() is a built-in function in JavaScript that is used to convert a string to an integer. It takes two parameters: the string to be converted and the base of the number system to use for the conversion.
  • Here's an example of how to use parseInt():

    const str = "123";
    const num = parseInt(str);
    console.log(num); // output: 123

  • In this example, parseInt() converts the string "123" to the integer 123.
  • One of the benefits of using parseInt() is that it can handle different bases of number systems. For example, if you have a string that represents a binary number, you can use parseInt() with a base of 2 to convert it to an integer:

    const binaryStr = "1010";
    const decimalNum = parseInt(binaryStr, 2);
    console.log(decimalNum); // output: 10

  • Another benefit of parseInt() is that it can handle leading and trailing white space in the input string. For example:

    const strWithWhiteSpace = "  123  ";
    const numWithoutWhiteSpace = parseInt(strWithWhiteSpace);
    console.log(numWithoutWhiteSpace); // output: 123

  • In this example, parseInt() removes the leading and trailing white space before converting the string to an integer.
  • The second parameter of parseInt() is the radix, or base, of the number system to use for the conversion. The default value is 10, which means that the function will assume the input string is in base 10. However, you can specify a different radix if the input string is in a different base, such as binary (base 2) or hexadecimal (base 16).
  • If the input string cannot be converted to a valid number, parseInt() will return NaN (not a number). For example:

    const invalidStr = "abc";
    const invalidNum = parseInt(invalidStr);
    console.log(invalidNum); // output: NaN

  • If the input string begins with "0x" or "0X", parseInt() will assume that the string represents a hexadecimal number and will convert it accordingly. For example:

    const hexStr = "0xFF";
    const hexNum = parseInt(hexStr);
    console.log(hexNum); // output: 255

  • If the input string begins with "0", parseInt() will assume that the string represents an octal number (base 8) and will convert it accordingly. However, this behavior is deprecated in ECMAScript 5 and later, so it is recommended to always specify the radix parameter explicitly. For example:

    const octalStr = "012";
    const octalNum = parseInt(octalStr, 8);
    console.log(octalNum); // output: 10

  • It's worth noting that parseInt() is a global function in JavaScript, which means that it can be called from any part of your code without having to import or include any additional modules.
  • When parseInt() encounters a non-numeric character in the input string, it stops converting and returns the numeric value it has accumulated so far. For example:

    const str = "123abc";
    const num = parseInt(str);
    console.log(num); // output: 123

  • If the input string starts with a plus sign (+) or a minus sign (-), parseInt() will convert the string to a signed integer. For example:

    const positiveStr = "+123";
    const negativeStr = "-123";
    const positiveNum = parseInt(positiveStr);
    const negativeNum = parseInt(negativeStr);
    console.log(positiveNum); // output: 123
    console.log(negativeNum); // output: -123

  • The parseInt() function can be used with the Number() constructor to convert a string to a number. The main difference between the two methods is that Number() converts floating-point numbers and scientific notation as well as integers. For example:

    const str = "123.45";
    const num1 = parseInt(str);
    const num2 = Number(str);
    console.log(num1); // output: 123
    console.log(num2); // output: 123.45

  • It's worth noting that parseInt() is a relatively simple function that can be easily replaced with more advanced parsing libraries or regular expressions for more complex parsing tasks. However, for simple conversions between strings and integers, parseInt() remains a useful tool in the JavaScript programmer's toolbox.
  • The parseInt() is that it can be slow compared to other parsing methods, especially when dealing with large strings or when parsing a large number of strings.
  • Finally, it's worth noting that parseInt() is just one of several functions in JavaScript that can be used to convert strings to numbers. Other functions include Number(), parseFloat(), and valueOf(). Each of these functions has its own strengths and weaknesses, so it's important to choose the right function for the task at hand.

No comments:

Post a Comment

Primitive Types in TypeScript

In TypeScript, primitive types are the most basic data types, and they are the building blocks for handling data. They correspond to simple ...