How to use the unary + operator In Javascript
If we use the ‘+’ operator before any string if the string in numeric it converts it to a number.
Javascript
function convertStoI() { let a = "100" ; let b = +(a); console.log( typeof (b)); let d = + "3 11 43" ; console.log( typeof (d)); } convertStoI(); |
number number
How to convert a string into a integer without using parseInt() function in JavaScript ?
In JavaScript, there is a simple function parseInt() to convert a string to an integer. In order to know more about the function, you can refer to this. In this article, we will learn how to convert the string to an integer without using the parseInt() function.
Advantage of parseInt() over this method. The parseInt() function converts the number which is present in any base to base 10 which is not possible using the method discusses above.
Below are the following methods:
- Using coercion
- Using the Number() function
- Using the unary + operator
- Using Math floor() Method
- Using Math.ceil( ) function