Discussions

QConverting a string into a int

I want to know how can I convert a string variable into a integer variable using jquery or javascript

AAnswer to: Converting a string into a int

tsmallfield

You can use parseInt() function to convert a string to an integer.

parseInt("123px", 10); // => 123

Note that the radix is specified as a second argument (which is optional).

And if a string contains only numeric characters,
you can use bitwise operators(>>, <<, ~~, |) as well.

"123.4" >> 0;// => 123
"123.4" | 0; // => 123
~~"123.4"; // => 123

Because these operators expect only integer operands,
its result is always an integer.

Post a question

You can post JavaScript, HTML, CSS related questions with attached reference to actual codes and get the answers likewise.

Ask a question

  • 1. Click the ‘Post a question’ above

  • 2. Enter question details, reference code, and click ‘Add question’ button

  • 3. Question will be paired with the code on display

Add an answer

  • 1. Choose a question

  • 2. Enter answer details, attach the reference code and click ‘add answer’ button

  • 3. Your answer will accompanied with attached code

Tag