Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert String into integer in java script without using any built in functions

Tags:

javascript

Is there any trick to convert string to integer in javascript without using any function and methods?

var s = "5"
console.log(typeof(s)) // out put is string 
console.log(typeof(parseInt(s))) // i want out put which is number with out using parseInt() or other functions for optimizing code.

Any help would be appreciated. Thanks in advance.

like image 780
code7004 Avatar asked Feb 28 '19 07:02

code7004


1 Answers

You can cast the string to number using unary plus (+). This will do nothing much beside some code optimization.

var s = "5"
s = +s;
console.log(typeof(s), s);

var s = "5.5"
s = +s;
console.log(typeof(s), s);
like image 88
Mamun Avatar answered Oct 05 '22 23:10

Mamun