Hi Friends 👋,
Welcome To Infinitbility! ❤️
+ unary plus oprator it’s one of the fastest way to convert string to int. let’s take a example
The unary plus operator (+) precedes its operand and evaluates to its operand but attempts to convert it into a number, if it isn’t already.
In the following example, we can able to convert string to number, float or any negative number using
+ unary plus oprator.
1// Int2console.log(+"90") // 9034// Float5console.log(+"90.90") // 90.967// Negative Number8console.log(+"-90") // -90
+ unary plus oprator to convert string to number.
In this example, we will do
- Convert string to integer
- Convert string to decimal
- Convert string to negative number
1// Convert string to integer2let first = "100";3first = +first;4console.log(first); // 10056// Convert string to decimal7let second = "25.386";8second = +second;9console.log(second); // 25.3861011// Convert string to integer12let third = "-100";13third = +third;14console.log(third); // -100
In the above exampls, we have taken multiple string number types examples to convert it into numbers, Let’s check the output.
I hope it’s help you, All the best 👍.
Follow me on Twitter
Join our email list and get notified about new content
No worries, I respect your privacy and I will never abuse your email.
Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.