5

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.

Wasif Ali
  • 886
  • 1
  • 13
  • 29
code7004
  • 120
  • 1
  • 7
  • my question is different first read full question i know this type of question was asked before – code7004 Feb 28 '19 at 07:17
  • 1
    Possible duplicate: https://stackoverflow.com/questions/1133770/convert-a-string-to-an-integer-in-javascript. – Rajesh Feb 28 '19 at 07:21
  • there is an oneother answer with using (-) which cast string in to integer – manan5439 Feb 28 '19 at 07:26
  • 1
    @Rajesh Please don't stuck to the example code, the question says "_convert string to integer_" Any string ... – Teemu Feb 28 '19 at 07:27
  • 1
    @Teemu So for `s="5.5"`, expected output should be `5` and not `5.5` as `+s` returns... Damn! I didn't think of this – Rajesh Feb 28 '19 at 07:30

4 Answers4

6

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);
Takit Isy
  • 9,688
  • 3
  • 23
  • 47
Mamun
  • 66,969
  • 9
  • 47
  • 59
  • `var s = "5.5"` What now? – Teemu Feb 28 '19 at 07:21
  • 1
    @TakitIsy Exactly, and that's not what was asked ("_convert string to __integer___"). It doesn't matter, though, since OP has accepted an answer which doesn't do what they actually asked for ... – Teemu Mar 04 '19 at 13:47
  • @Teemu I agree, but well… I guess the OP meant *number* and not *integer* anyway. For many, *integers* are just *numbers*. – Takit Isy Mar 04 '19 at 14:57
5

here is a most effective way to convert string into int without using any built in function have a look at code. Thank you:)

var s = "5"
var i = s - 0
console.log(typeof(i)) // you will get a number without using any built in function... because (-) will cast string in to number
manan5439
  • 898
  • 9
  • 24
3

You can try using bit-wise operator s|0. This will convert value to integer. However, this will also convert floating values to integer.

var s = "5"
var y = s|0;
console.log(typeof(y), s, y);

var s = "5.5"
var y = s|0;
console.log(typeof(y), s, y);
Rajesh
  • 24,354
  • 5
  • 48
  • 79
0

Basically if you are searching "whole logic" to convert string to int then you have to find ASCII code for each alphabet in string and then convert it to integer and make sum of all 48 to 57 are ASCII codes are for number 0 to 1. Below is example code

let parseINT = (b) => b.split("").map((e,i) => {
    let value = e.charCodeAt(0) - 48;
    return value * [...Array(((i - b.length) * -1)).keys()].reduce((a, b) => a > 0 ? (a * 10) : 1, 0);
}).reduce((a, b) => a + b, 0)

or short form will be like

let parseINT = (b) => b.split("").map((e,i) => ((e.charCodeAt(0)  <= 57 ? e.charCodeAt(0) : 48) - 48) * [...Array(((i - b.length) * -1)).keys()].reduce((a, b) => a > 0 ? (a * 10) : 1, 0)).reduce((a, b) => a + b, 0);
anshuVersatile
  • 2,030
  • 1
  • 11
  • 18