I made an array with several strings as values so it becomes one really long line that takes a while to scroll through. To put it on multiple lines I searched and found that I can use a + sign to link the lines, but I'm having a problem. Here's a small example:
<script type="text/javascript">
var x;
var colorArr=["Red","Orange","Yellow",+
"Green","Blue","Purple"];
for(x=0;x<6;x++)
document.write(colorArr[x]+"<br/>");
</script>
This outputs:
Red
Orange
Yellow
NaN
Blue
Purple
Basically whichever element is the first on the line becomes undefined for some reason. How do I do this the correct way?
You don't need the +
, just flow to the next line. Javascript doesn't equate the end of the line with the end of the statement.
var colorArr=["Red","Orange","Yellow",
"Green","Blue","Purple"];
To understand the behavior you're seeing, note that this:
var test = -"test";
alert(test);
Outputs the NaN
(not a number) that you're seeing. The parser is attempting to convert "Green" to a number -- so that it can evaluate what it assumes is a math expression (since it begins with +
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With