How to .substr() a integer in Javascript

What about ...

var integer = 1234567;
var subStr = integer.toString().substr(0, 1);

... ?


Given

var a = 234; 

There are several methods to convert a number to a string in order to retrieve the substring:


Examples

Included are examples of how the given number, a, may be converted/coerced.

Empty string concatenation

(a+'').substr(1,1);        // "3"

Number.prototype.toString method

a.toString().substr(1,1)   // "3"

Template strings

`${a}`.substr(1,1)         // "3"

String object

String(a).substr(1,1)      // "3"

Would converting to a string first be ok?

var x = 12345;
var xSub = x.toString().substr(1,3);

alert(xSub);​ // alerts "234"

Tags:

Javascript