By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,528 Members | 1,324 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,528 IT Pros & Developers. It's quick & easy.

Syntax -0 at end of replace()

P: n/a
I found this in one of my old scripts but I can't remember what the -0 was
used for. I remember it was a shortcut method but I can't remember for what.
Does this ring a bell with anyone?

var Pattern = /px/;
var T = paddingArry[0].replace(Pattern, "")-0;

David J.
Nov 13 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a

David wrote:
I found this in one of my old scripts but I can't remember what the -0 was
used for. I remember it was a shortcut method but I can't remember for what.
Does this ring a bell with anyone?

var Pattern = /px/;
var T = paddingArry[0].replace(Pattern, "")-0;

David J.
Convert result from string to number.
IMHO, there is more clear way:
var T = parseInt(paddingArray[0])

Nov 13 '06 #2

P: n/a
marss said the following on 11/13/2006 3:39 AM:
David wrote:
>I found this in one of my old scripts but I can't remember what the -0 was
used for. I remember it was a shortcut method but I can't remember for what.
Does this ring a bell with anyone?

var Pattern = /px/;
var T = paddingArry[0].replace(Pattern, "")-0;

David J.

Convert result from string to number.
IMHO, there is more clear way:
var T = parseInt(paddingArray[0])
And if paddingArray[0] contains the string "09".

var T = +paddingArray[0];

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Nov 13 '06 #3

P: n/a
Randy Webb wrote:
marss said the following on 11/13/2006 3:39 AM:
>David wrote:
>>I found this in one of my old scripts but I can't remember what the
-0 was
used for. I remember it was a shortcut method but I can't remember
for what.
Does this ring a bell with anyone?

var Pattern = /px/;
var T = paddingArry[0].replace(Pattern, "")-0;

David J.

Convert result from string to number.
IMHO, there is more clear way: var T = parseInt(paddingArray[0])

And if paddingArray[0] contains the string "09".

var T = +paddingArray[0];
and if paddingArray[0] has the suffix 'px' the result is NaN. I think
parseInt() is fine in this case, but a radix should be supplied:

var T = parseInt(paddingArray[0], 10);

It is unlikely that the value would be reported as '09px' if it is
returned by a DOM function, however it may come from some other source
where a leading zero may occur and better to be safe than sorry.
--
Rob
Nov 13 '06 #4

P: n/a
RobG wrote:
Randy Webb wrote:
>marss said the following on 11/13/2006 3:39 AM:
>>David wrote:
<snip>
>>>var Pattern = /px/;
var T = paddingArry[0].replace(Pattern, "")-0;

Convert result from string to number.
IMHO, there is more clear way: var T = parseInt(paddingArray[0])
And if paddingArray[0] contains the string "09".

var T = +paddingArray[0];

and if paddingArray[0] has the suffix 'px' the result is NaN. I think
parseInt() is fine in this case, but a radix should be supplied:

var T = parseInt(paddingArray[0], 10);

It is unlikely that the value would be reported as '09px' if it is
returned by a DOM function, however it may come from some other source
where a leading zero may occur and better to be safe than sorry.
It is possible for px dimensions to be non-integers, as set or as
reported. If the original replace method was what was desired then:_

var T = +(paddingArry[0].replace(Pattern, ""));

- for speed, or:-

var T = Number(addingArry[0].replace(Pattern, ""));

- for clarity.

- would be the direct alternatives.

Richard.

Nov 13 '06 #5

P: n/a
Thanks everyone for your replies.

David J.
Nov 15 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.