"" + some form value

G

Gene Wirchenko

Dear JavaScripters:

What does "" + some form value do? For example,
""+document.Contest.Last.value
Won't document.Contest.Last.value be string regardless? I tried
forcing one such to a number, and after the assignment, typeof()
reported it was a string.

Is this ""+ a bit of superstition, or am I missing something?

Sincerely,

Gene Wirchenko
 
T

Thomas 'PointedEars' Lahn

Richard said:
Assuming that - document.Contest.Last.value - if a reference to the -
value - property of a form element then the concatenation is actually
worthless as such - value - property values are already string
primitives. Unfortunately forced type-conversion methods often ends up
being applied as a mystical incantation, and so in contexts where they
actually have no effect.

Incidentally, to force type conversion to a string primitive the String
constructor can be used without the - new - operator, as in:-

String(document.Contest.Last.value)

- Which has been both recommended against on the grounds that it could
be confused with a miss-typed - new String(s); - and recommended as a
self-documenting and explicit means of forcing type-conversion to a
string. I am leaning towards the latter. Concatenating the empty string
has been shown to be the most runtime efficient type-conversion to
string primitive operation.

There is no consensus on what is most runtime-efficient here. In fact, the
results at <http://jsperf.com/string-conversion-speed> vary so much between
implementations, runtime environments, and even consecutive tests in the
very same runtime environment, and String() is indeed self-documenting, that
I am going to keep my original, ECMAScript-supported approach¹ of using
String() when I do not know the type of the value and if it(s object
representation) has a toString() method.


PointedEars
___________
¹) the `+' operator requires more algorithmic steps for the type conversion
than String()
 
D

Dr J R Stockton

In comp.lang.javascript message <fuodc799nhk0c9ofihe817rjtkjid2cq79@4ax.
What does "" + some form value do? For example,
""+document.Contest.Last.value
Won't document.Contest.Last.value be string regardless?

Not inevitably.

document.Contest = {} ;
document.Contest.Last = {} ;
document.Contest.Last.value = new Date() ;
99 ;
document.Contest.Last.value.getHours() ;

gives me 18 (YMMV)
I tried
forcing one such to a number, and after the assignment, typeof()
reported it was a string.

Is this ""+ a bit of superstition, or am I missing something?

It ensures a string value; for example, this

var U ; typeof ( ''+U) // gives 'string'
typeof (+''+U) // gives 'number'
typeof (''+ +U) // gives 'string'

Some coders find it easier to force a string rather than to become
confident that it is and always will be inevitably a string already.
 
G

Gene Wirchenko

Gene Wirchenko wrote:
[snip]

Incidentally, to force type conversion to a string primitive the String
constructor can be used without the - new - operator, as in:-

String(document.Contest.Last.value)

- Which has been both recommended against on the grounds that it could
be confused with a miss-typed - new String(s); - and recommended as a
self-documenting and explicit means of forcing type-conversion to a
string. I am leaning towards the latter. Concatenating the empty string

I think I will, too.
has been shown to be the most runtime efficient type-conversion to
string primitive operation.


How? Strings can be type-converted into number primitive values using,
say, +string or Number(string) (and commonly in the past or
string*1 , or string-0 ).

With an assignment statement. The following statement was an
alert() to get the typeof() and reported string.

[snip]
No, it appears to be superstition in this case. Though there is a slight
possibility that - document.Contest.Last - was not referring to a form
element in this case (then you only have evidence of poor code design
rather than a mystical incantation).

No, definitely a form element. That is why I was puzzled about
it.

Sincerely,

Gene Wirchenko
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top