RSS

Multi-line javascript

05 May

I never knew that but there is another way for writing multi-line JavaScript. But there is one – by escaping the line breaks:

var a = 'You can start here \
and continue here \
and then finish it up here.';

The original article can be found here.

The code looks nice but it is not legal according to the ECMAScript-262 Standard that states that:

A ‘LineTerminator’ character cannot appear in a string literal, even if preceded by a backslash \. The
correct way to cause a line terminator character to be part of the string value of a string literal is to use
an escape sequence such as \n or \u000A.

Things changed in the final draft of 5th edition of ECMA-262 Standard that says:

A line terminator character cannot appear in a string literal, except when preceded by a backslash \ as a ‘LineContinuation’ to produce the empty character sequence. The correct way to cause a line terminator character to be part of the string value of a string literal is to use an escape sequence such as \n or \u000A.

Myself I prefer the old way of writing multi-line code that will definitely work with no exceptions.

Advertisements
 
Leave a comment

Posted by on May 5, 2009 in JavaScript

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: