Welcome to WebmasterWorld Guest from 54.147.63.124

Forum Moderators: open

Message Too Old, No Replies

javascript purpose of semicolon?

dumb question

     
7:30 pm on Aug 6, 2003 (gmt 0)

Junior Member

10+ Year Member

joined:Mar 24, 2003
posts:56
votes: 0



Hi,

Never realized this before. A javascript expression doesn't have to end with a ';'.

So what's the diffrence? For example, between
alert("1") & alert("1");

This kind of things are so hard to find through search engines:).

7:37 pm on Aug 6, 2003 (gmt 0)

Preferred Member

10+ Year Member

joined:Sept 24, 2002
posts:364
votes: 0


it programming, one usually indicates the end of a statement with a semi-colon. Some langauges, such as c or c++, demand it.

Javascript, on the other hand, does not demand it, so if you don't want to put it, then don't.

8:07 pm on Aug 6, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member tedster is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:May 26, 2000
posts:37301
votes: 0


When writing or editing javascript, the semicolon helps a lot visually.

Depending on your editor, it may not be clear otherwise if you actually have a line break in there (ending the line of code). Instead, your editor may just be wrapping the line visually. So without the semicolon, you can make and overlook errors more easily.

10:18 pm on Aug 6, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:July 26, 2003
posts:881
votes: 0


Semicolen is a formally necessary part of JavaScript statements:

[mozilla.org...]
[mozilla.org...]

Practically speaking, it is not needed in most cases. The browser can usually parse statements based on new-lines, line-breaks and other informal conventions.

It is a good idea to put it anyways, though, because for example, a person may have the option to use strict JS in Mozilla turned on, and they might get syntax warnings in the console without it.

Also, in certain cases (e.g., in a bookmarklet) the lack of the semicolen may cause the evaluation of the statement to fail because there is no new-line or space or line-break between two statements (e.g.,

var a="Henry the ",b=7;++b;alert(a+b+"th\nI am, I am...");
).

Jordan

10:23 pm on Aug 6, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2002
posts:18903
votes: 0


If you have multiple, or nested, IF ... THEN ... ELSE statements and you miss off a semi-colon then the code may do something different to what you were expecting in some browsers.
10:36 pm on Aug 6, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:July 26, 2003
posts:881
votes: 0


g1smd:

I had something similar happen just the other day actually, lol. I switch()'d a value and in one of the cases I forgot the semicolen after the break statement, doh. Took me like an hour to figure out what was wrong with it.

Jordan

gph

10:45 pm on Aug 6, 2003 (gmt 0)

Full Member

10+ Year Member

joined:Jan 31, 2002
posts:285
votes: 0


MonkeeSage I did exactly the same thing a couple weeks ago :) I stared at it for ages. I'm not condoning it but I only use semicolons if I have to. It's a bad habit that shows up in things like switch()
12:59 pm on Aug 7, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Nov 13, 2002
posts:818
votes: 0


I believe they're also required if you have more than one statement per line. (for those who like to eliminate all possible redundant whitespace to reduce file sizes)
1:29 pm on Aug 7, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member korkus2000 is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 20, 2002
posts:3732
votes: 0


It is required for multiple statements on one line. If you want to save space by combining lines then the semicolon lets the parser know it is a new statement. On some browsers the semicolon is required for multiple commands inside of one command structure.

if(y=0){
alert("yes");
y=1;
}

Some parsers need the semicolon or you get an error because it runs the to statements together.

3:42 pm on Aug 7, 2003 (gmt 0)

Preferred Member

10+ Year Member

joined:May 14, 2003
posts:376
votes: 0


korkus,

what space are you saving? in your example, at worst case, you're only saving 6 or 9 bytes (3 CRLF pairs and maybe 3 space characters)...

3:49 pm on Aug 7, 2003 (gmt 0)

Senior Member from MT 

WebmasterWorld Senior Member 10+ Year Member

joined:Apr 1, 2003
posts:1843
votes: 0


I think that'd be visual space, i.e. line count

SN

 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members