How to use the Number() constructor In Javascript
The Number() constructor can be used to convert a string into a number by passing the string value as an argument to it.
Syntax:
Number(stringVariable);
Example: The below code converts a string to number with the help of Number() constructor.
const str = "233";
console.log("Type of str before conversion: ", typeof str);
console.log("Type of str after conversion: ", typeof Number(str));
Output
Type of str before conversion: string Type of str after conversion: number
Convert a String to Number in JavaScript
Converting strings to numbers in JavaScript is a fundamental operation, crucial for various tasks like handling user inputs or data processing. By converting strings to numeric values, JavaScript enables developers to perform mathematical operations and comparisons accurately, enhancing the versatility and functionality of their applications.
Table of Content
- Using the ‘+’ operator
- Using the Number() constructor
- Using the parseInt() method
- Using the parseFloat() method
- Using the Math.floor() method
- Using the Math.ceil() method
- Using the Math.round() method
Contact Us