Carriage return in values of arrays in Javascript not allowed?
Carriage return in values of arrays in Javascript not allowed?
I had this question about arrays in JavaScript. I couldn't really find an answer. maybe i din look harder, i did look hard enough. or maybe i missed it in the book i am using as my reference.
I had an array and tried to push in quotes.
var quotes =[]; quotes.push("It has become appallingly obvious that our technology has exceeded our humanity - Albert Einstein") /*i placed it on the second line to make it more readable*/
This gave me an error. it did not state what error it was. I was going crazy thinking what could it be? i mean, what could i be missing in these 2 lines of code.
Then i placed it all on the same line and guess what, yes no more error. =_=
Can someone enlighten me on why? Or am i missing out on the obvious here.
Any response appreciated. Thank you
Answer by Thor84no for Carriage return in values of arrays in Javascript not allowed?
You can't have newlines in a string. End the string first and concatenate it with a new one, like this;
quotes.push("It has become appallingly obvious that our technology has exceeded " + "our humanity - Albert Einstein");
If you actually want the newline to be part of the string use "\n"
instead.
Answer by Tom for Carriage return in values of arrays in Javascript not allowed?
quotes.push("It has become appallingly obvious that our technology has exceeded " + "our humanity - Albert Einstein");
that would work; I also added a semicolon at the end of your line.
Answer by DemoUser for Carriage return in values of arrays in Javascript not allowed?
Try:
var quotes =[]; quotes.push("It has become appallingly obvious that our technology has exceeded \ our humanity - Albert Einstein");
Answer by EyalAr for Carriage return in values of arrays in Javascript not allowed?
var quotes =[]; quotes.push("It has become appallingly obvious that our technology has exceeded \ our humanity - Albert Einstein");
Notice the \
at the end of line 2.
That's how you write multi-line strings in many languages.
Answer by Dane Balia for Carriage return in values of arrays in Javascript not allowed?
Yes on a single line will work because as you rightly pointed out, a carriage return does not work. That is, in Javascript a carriage return is considered end of a programming statement.
Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72
0 comments:
Post a Comment