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.

5
  • my question is different first read full question i know this type of question was asked before Commented Feb 28, 2019 at 7:17
  • 1
    Possible duplicate: stackoverflow.com/questions/1133770/…. Commented Feb 28, 2019 at 7:21
  • there is an oneother answer with using (-) which cast string in to integer Commented Feb 28, 2019 at 7:26
  • 1
    @Rajesh Please don't stuck to the example code, the question says "convert string to integer" Any string ... Commented Feb 28, 2019 at 7: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 Commented Feb 28, 2019 at 7:30

4 Answers 4

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);

Sign up to request clarification or add additional context in comments.

3 Comments

var s = "5.5" What now?
@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 I agree, but well… I guess the OP meant number and not integer anyway. For many, integers are just numbers.
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

1 Comment

I find much more in this explanation.
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);

Comments

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.