TypeScript parseInt()

In this chapter, we will explore the parseInt() function in TypeScript. This function parses a string argument and returns an integer of the specified radix (base). Understanding how to use parseInt() is useful for converting strings to integers, especially when working with user inputs or data that may come in string format.

Table of Contents

  1. Definition
  2. Syntax
  3. Examples
  4. Conclusion

1. Definition

The parseInt() function parses a string argument and returns an integer of the specified radix (base). This function is useful for converting strings to integers.

2. Syntax

parseInt(string, radix?);

Parameters

  • string: The value to parse. If string is not a string, it is converted to one. Leading whitespace in the string is ignored.
  • radix (optional): An integer between 2 and 36 that represents the base of the numeral system to be used.

Return Value

The function returns an integer parsed from the given string. If the first character cannot be converted to a number, it returns NaN.

3. Examples

Let's look at some examples to understand how parseInt() works in TypeScript.

Example 1: Basic Usage

In this example, we use parseInt() to convert a string to an integer.

let numStr: string = "42";
let num = parseInt(numStr);
console.log(num); // Output: 42

Example 2: Using a Different Radix

In this example, we use parseInt() to convert a binary string to an integer.

let binaryStr: string = "101010";
let num = parseInt(binaryStr, 2);
console.log(num); // Output: 42

Example 3: Parsing Hexadecimal Numbers

In this example, we use parseInt() to convert a hexadecimal string to an integer.

let hexStr: string = "2A";
let num = parseInt(hexStr, 16);
console.log(num); // Output: 42

Example 4: Handling Leading and Trailing Whitespace

In this example, we use parseInt() to parse a string with leading and trailing whitespace.

let numStr: string = "  42  ";
let num = parseInt(numStr);
console.log(num); // Output: 42

Example 5: Handling Non-Numeric Characters

In this example, we use parseInt() to parse a string with non-numeric characters.

let numStr: string = "42abc";
let num = parseInt(numStr);
console.log(num); // Output: 42

Example 6: Parsing Invalid Strings

In this example, we use parseInt() to parse an invalid string.

let invalidStr: string = "abc";
let num = parseInt(invalidStr);
console.log(num); // Output: NaN

Example 7: Using Different Radix Values

In this example, we use parseInt() with different radix values to parse the same string.

let numStr: string = "10";
console.log(parseInt(numStr, 2));  // Output: 2 (binary)
console.log(parseInt(numStr, 8));  // Output: 8 (octal)
console.log(parseInt(numStr, 10)); // Output: 10 (decimal)
console.log(parseInt(numStr, 16)); // Output: 16 (hexadecimal)

4. Conclusion

In this chapter, we explored the parseInt() function in TypeScript, which is used to parse a string argument and return an integer of the specified radix (base). We covered its definition, syntax, parameters, return value, and provided several examples to demonstrate its usage. Understanding how to use parseInt() effectively is essential for converting strings to integers, especially when working with user inputs or data that may come in string format.

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare