inherit
82079
0
Feb 19, 2023 10:21:58 GMT -8
SuperSleuth
137
June 2006
ibeta
|
Post by SuperSleuth on Dec 1, 2009 2:49:48 GMT -8
I was wondering if there was a difference between doing the following:
alert((12345).toString().substr(0, 3)) and alert(("" + 12345+ "").substr(0, 3));
Well, not so much a difference as one being faster or more efficient than the other.
|
|
inherit
100824
0
May 13, 2012 5:37:49 GMT -8
Michael
14,585
March 2007
wrighty
|
Post by Michael on Dec 1, 2009 10:38:07 GMT -8
Why not just
alert("12345".substr(0,3));
|
|
inherit
82079
0
Feb 19, 2023 10:21:58 GMT -8
SuperSleuth
137
June 2006
ibeta
|
Post by SuperSleuth on Dec 1, 2009 13:45:49 GMT -8
I have a variable and it isn't a string, but I want to treat it like one when I document.write() it to the page.
|
|
inherit
100824
0
May 13, 2012 5:37:49 GMT -8
Michael
14,585
March 2007
wrighty
|
Post by Michael on Dec 1, 2009 16:28:24 GMT -8
I have a variable and it isn't a string, but I want to treat it like one when I document.write() it to the page. If it's a variable, use .toString()
|
|
inherit
130228
0
Jul 11, 2024 19:19:59 GMT -8
Charles Stover
1,731
August 2008
gamechief
|
Post by Charles Stover on Dec 1, 2009 20:36:18 GMT -8
There is no difference in terms of the outcome. The method it goes about achieving the outcome is different, and toString() is better. The "" + variable + "" involves converting variable toString() and the appending nothing. Whereas variable.toString() just converts it to a string. Not to mention standards.
|
|
inherit
82079
0
Feb 19, 2023 10:21:58 GMT -8
SuperSleuth
137
June 2006
ibeta
|
Post by SuperSleuth on Dec 1, 2009 20:55:11 GMT -8
Yeah, I just came across this link (http://stackoverflow.com/questions/1572708/is-conversion-to-string-using-int-value-bad-practice) and it mentions why toString (well, valueOf (Java), but it explains why the "" + variable is bad practice) is better.
|
|