PDA

View Full Version : What is a line of code?


F.N.G.
03-21-2003, 02:48 AM
Can you tell, by looking at source, exactly where each line ends?

The following statements are supposed to explain the concept of a "line":


+----------------------------------+
JavaScript Programmer's Reference
+----------------------------------+

A line of script source text is that fragment of script that is placed between two consecutive line terminators...

---

Line terminators separate individual lines of executable code...

----
In general, a line terminator can occur between any two tokens but cannot appear in a token or inside a string literal...

----

Tokens are the actual components that an executable script is built from. They may be reserved words, identifiers, punctuator symbols or literals.

+----------------------------------+

w h a t ?

+

beetle
03-21-2003, 03:46 AM
Hehe, getting lost in the vernacular, eh?

A line terminator in javascript is just a new line. It's pretty much right about 'token's. Examples of tokens

var
=
'blah'
function
{
(
parseInt

Basically, any singular command, operator, declaration, string, etc.

Consider this example

var myVar = 'blah';
alert( myVar );

Two lines, right? The semicolon isn't required to terminate a line in JS, but it's fairly customary to use them, as all other C-based scripting languages require it. This is where I have a problem with javascript's EOL character being just the newline.

var
myvar
=
'blah'
alert
(
myvar
)

Is the same as above. So, newlines, or EOL characters really don't terminate the line, they just aren't allowed in a few specific places, primarily inside literal strings.

var myVar = 'bl
ah';

is invalid. If javascript used the ; for newlines, then the above would actually be valid.

So, in a nutshell, the EOL character in javascript almost doesn't exist. Placed in a couple wrong places and it causes problems, otherwise, put them wherever you like. Just don't leave them out when they are necessary, otherwise, use semicolons

var myVar = 'blah' alert( myVar )

Won't work (and also won't even error in IE :rolleyes: ) Here's where the semicolon is required

var myVar = 'blah'; alert( myVar )

Personally, I'd like to see it change.

F.N.G.
03-22-2003, 12:06 AM
Thanks for explaining that.

eggman
03-22-2003, 02:15 AM
var myVar = 'blah' alert( myVar )

errors on my IE browser.

In fact, I've never been able to write ill-formed Javascript as it always errors. Is this caused by some browser setting or a setting on my computer?

beetle
03-22-2003, 03:01 AM
Could be, as it doesn't on mine. Odd. Oh well, I'm not gonna lose sleep over it ;)