473,395 Members | 1,730 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

newbe: where to put ";" in a script?

Hi,

i start with javascript and i saw a lot of examples, but sometimes there is
";" on each end of line, sometimes only certain lines, sometimes nowhere.
So my question is: where must i put ";" in a javascript (in function, out
function)?

thanks for help
ad
Jul 20 '05 #1
8 1977
> i start with javascript and i saw a lot of examples, but sometimes there is
";" on each end of line, sometimes only certain lines, sometimes nowhere.
So my question is: where must i put ";" in a javascript (in function, out
function)?


Put ';' at the end of all non-block statements. jslint will help you to place
them properly.

http://www.crockford.com/javascript/lint.html

Jul 20 '05 #2
thanks

"Douglas Crockford" <no****@laserlink.net> wrote in message
news:bf**********@sun-news.laserlink.net...
i start with javascript and i saw a lot of examples, but sometimes there is ";" on each end of line, sometimes only certain lines, sometimes nowhere. So my question is: where must i put ";" in a javascript (in function, out function)?
Put ';' at the end of all non-block statements. jslint will help you to

place them properly.

http://www.crockford.com/javascript/lint.html

Jul 20 '05 #3
JRS: In article <bf**********@reader08.wxs.nl>, seen in
news:comp.lang.javascript, adrien <hg****@sdgs.fb> posted at Tue, 22 Jul
2003 19:08:42 :-
i start with javascript and i saw a lot of examples, but sometimes there is
";" on each end of line, sometimes only certain lines, sometimes nowhere.
So my question is: where must i put ";" in a javascript (in function, out
function)?

(A) Where there are two or more separate statements on a single line,
you must use a semicolon to separate them.
(B1) At the end of a line, it is often necessary not to put a semi-
colon, because the statement is not yet complete.

(B2) At the end of a line, it is sometimes necessary to put a semi-colon
to show that the statement is complete.

(B3) Otherwise, at the end of a line, please yourself. Many people use
them whenever possible.

(B4) Note that two lines may be valid code, sometimes even meaningful
code, but giving different results, both with and without the semi-
colon. Consider
for (j=0;j<2;j++) // possible ; here
alert(j)
A programmer should know, when about to start a new line, whether the
previous statement is intended to finish. If it is, a semicolon is
permissible and perhaps desirable. If it is not, it must be evidently
incomplete, by having an unclosed bracket or a terminal operator (or
....?).
The system will insert a semicolon (or act as if it has) when it sees
fit; it will not remove one that it dislikes.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
Jul 20 '05 #4
In article <Xi**************@merlyn.demon.co.uk>, Dr John Stockton
<sp**@merlyn.demon.co.uk> writes
<snip>
(A) Where there are two or more separate statements on a single line,
you must use a semicolon to separate them.

<snip>

If you'd bothered to RTFM you wouldn't have written such misleading
advice.

In C-like languages *some* kinds of statement end with a semicolon
(which in javascript can be omitted in some circumstances).
e.g. a = 5; ++a;
Other kinds don't end with a semicolon.
e.g. { }

The rule is different in Pascal-like languages where the semicolon is
not part of the statement.

John
--
John Harris
mailto:jo**@jgharris.demon.co.uk
Jul 20 '05 #5
> >(A) Where there are two or more separate statements on a single line,
you must use a semicolon to separate them.
If you'd bothered to RTFM you wouldn't have written such misleading
advice.

In C-like languages *some* kinds of statement end with a semicolon
(which in javascript can be omitted in some circumstances).
e.g. a = 5; ++a;
Other kinds don't end with a semicolon.
e.g. { }

The rule is different in Pascal-like languages where the semicolon is
not part of the statement.


I think you are being unfair here. JavaScript has a semicolon insertion
mechanism which attempts to correct syntax errors by replacing linefeeds with
semicolons. I think this was a bad idea. I don't trust it. I think that relying
on it is strictly unprofessional. That said,

Semicolon insertion does not work in the middle of a line. So if you put two
statements on the same line, you must use the semicolon to separate them. That
can look Pascal-like. That said,

I think it is bad to put two statements on one line. It impairs readability and
increases the likelihood of editing mistakes.

http://www.crockford.com/javascript/lint.html

Jul 20 '05 #6
JRS: In article <vD**************@jgharris.demon.co.uk>, seen in
news:comp.lang.javascript, John G Harris <jo**@nospam.demon.co.uk>
posted at Wed, 23 Jul 2003 20:37:48 :-
In article <Xi**************@merlyn.demon.co.uk>, Dr John Stockton
<sp**@merlyn.demon.co.uk> writes
<snip>
(A) Where there are two or more separate statements on a single line,
you must use a semicolon to separate them.

<snip>

If you'd bothered to RTFM you wouldn't have written such misleading
advice.

In C-like languages *some* kinds of statement end with a semicolon
(which in javascript can be omitted in some circumstances).
e.g. a = 5; ++a;
Other kinds don't end with a semicolon.
e.g. { }


Please show a circumstance where there are two or more separate
statements on a single line, but a semicolon is not necessary to
separate them.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
Jul 20 '05 #7
Dr John Stockton <sp**@merlyn.demon.co.uk> writes:
Please show a circumstance where there are two or more separate
statements on a single line, but a semicolon is not necessary to
separate them.


if(x==4){x=2}y=3

A *reasonable* example would be harder.
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #8
In article <bf**********@sun-news.laserlink.net>, Douglas Crockford
<no****@laserlink.net> writes
<snip>
Semicolon insertion does not work in the middle of a line. So if you put two
statements on the same line, you must use the semicolon to separate them. That
can look Pascal-like.
I think you've misunderstood my point. I'm pointing out that some kinds
of statement do not end in a semicolon, so the semicolon rules don't
apply to them. For instance,
{ } ++a;
is a perfectly valid line containing two statements.
That said,
I think it is bad to put two statements on one line. It impairs readability and
increases the likelihood of editing mistakes.


I agree with you in general, but there are special cases where I think
one line is better :
<BODY onload="a=true; b=2;">

John
--
John Harris
mailto:jo**@jgharris.demon.co.uk
Jul 20 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: eboogyman | last post by:
I have an HTML form with Firstname, lastname, address I want to save the firstname, lastname and address appended to a file in say results.txt, in the below format 1 entry per line... ...
4
by: Ralf Meuser | last post by:
Hi there I'm trying to create a button which will exit my php-session un go to my main page. I thought of some thing like this: .... <script language=JavaScript> function fonct_form(form) {
3
by: NecroJoe | last post by:
I am using PHP to generate a little javascript for one of my pages. In short it allows a user to select a value from a list and pop it into a form field on a seperate page. This works well unless...
22
by: David. E. Goble | last post by:
Hi All; I have a few of these; sigsImages="sigs/finished1.jpg" sigsImages="sigs/foghorn.jpg" lower I have;
7
by: Jean Pierre Daviau | last post by:
Hi, <script language="javascript" type="text/javascript"> if(navigator.appName.indexOf("Netscape") != -1){ document.writeln('<link rel="stylesheet" href="~styles/aquarelle_ns.css"...
9
by: Yaro | last post by:
Hello DB2/NT 8.1.3 Sorry for stupid questions. I am newbe in DB2. 1. How can I read *.sql script (with table and function definitions) into a database? Tool, command... 2. In Project Center...
7
by: RFS666 | last post by:
Hello, I would like to use variables with a type in jscript.NET. I declare them as follows: var x : double = 5.03; This doesn't work in my script, that I write to the page in codebehind with...
13
by: kurtj | last post by:
Hello Gurus: I have a validation script (below) that is somehow messed up. If the Name field is blank, I get the alert message, then the browser window goes to a blank document with the word...
17
by: Eric_Dexter | last post by:
def simplecsdtoorc(filename): file = open(filename,"r") alllines = file.read_until("</CsInstruments>") pattern1 = re.compile("</") orcfilename = filename + "orc" for line in alllines: if not...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.