473,770 Members | 6,322 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

When does whitespace in JavaScript matter?

Hi,

I'm thinking about code minimization. I can think of a few places where
whitespace matters

a + ++b
a++ + b
a - --b
a-- -b
when a line ends without a semi-colon in which case the new line
character matters.

Any others?

Thanks,
Peter

Jan 8 '07 #1
25 5650
Peter Michaux said the following on 1/8/2007 2:58 PM:
Hi,

I'm thinking about code minimization. I can think of a few places where
whitespace matters
In those 4 instances where whitespace will matter I always wrap it in
parentheses to remove the whitespace issue.
a + ++b
a++ + b
a - --b
a-- -b
when a line ends without a semi-colon in which case the new line
character matters.
That is not always true though:

function someFunction()
{

The newline character there is irrelevant. But, if you replace all new
lines with ;\n then you break the code. Line feeds and semicolons are a
spot where you have to either do it manually or write a JS parser.

Another example where the newline can't be summarily replaced but the
newline doesn't matter is in a loop:

for (i in something)
while(yourWifeI sntPregnant)

Whether the { is on the same line or the next line doesn't matter, the
newline is still irrelevant yet there is no semicolon at the end of it.

You can't summarily say "don't put a semicolon after )" either. Think of
anonymous functions....()
You could check ) to see if it is () or ) but it can still get you in
trouble with a function with no parameter:

function myFunction()
{

}

Where the newline character can't be replaced with a ;newline but if
it's a global anonymous function and you want to remove newlines then
you *must* have the semicolon or it breaks the code.

var x = (function(){
//some code here
})();

Can't remove the semicolon and the newline, one has to be there.

Semicolons and newlines will become your new nightmare :)

--
Randy
Chance Favors The Prepared Mind
comp.lang.javas cript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jan 8 '07 #2
"Peter Michaux" <pe**********@g mail.comwrote:
Hi,

I'm thinking about code minimization. I can think of a few places where
whitespace matters

a + ++b
a++ + b
a - --b
a-- -b
when a line ends without a semi-colon in which case the new line
character matters.
The new line character only matters some of the time. Unfortunately you
aren't going to know whether or not it matters without doing a full parser.
>
Any others?
A keyword followed by a letter or digit: "return 42;" versus "return42;"

Inside strings and regular expressions.

A comment which immediately follows a division:
a = x / /*this had better not be zero: */y;
z = 1;
versus:
a = x //*this had better not be zero: */y;
z = 1;
Jan 8 '07 #3
Randy Webb wrote:
Peter Michaux said the following on 1/8/2007 2:58 PM:
Hi,

I'm thinking about code minimization. I can think of a few places where
whitespace matters
<snip>
Semicolons and newlines will become your new nightmare :)
I imagine so. I may just decide to leave new lines in place.

It's more the comments, blank lines and beginning or mid-line spaces
that are a bigger concern. I have been using jslint to check for
problems before jsmin but jslint has many programmer preferences built
in and not just outright problems that will be harmful when minimizing.
I suppose I could strip jslint down.

What I really want is a command I can type inside a directory that
checks if the javascript files in that directory are minimizable and if
so minimizes them. Following that step all the files are gzipped. This
will save Apache the trouble of having to use mod_deflate for every
single request for a file. I don't know why they didn't build caching
for this into mod_deflate but since I have to do the minimize step
anyway I can also piggy back the compression step on the same command.
I know one thing for sure: I don't want to have to do all this
deployment stuff by hand starting with http://jslint.com anymore.

Peter

Jan 8 '07 #4
"Peter Michaux" <pe**********@g mail.comwrote:
It's more the comments, blank lines and beginning or mid-line spaces
that are a bigger concern. I have been using jslint to check for
problems before jsmin but jslint has many programmer preferences built
in and not just outright problems that will be harmful when minimizing.
I suppose I could strip jslint down.
I did a modified version of jslint which might interest you. You can find
it at http://codespeak.net/svn/kupu/trunk/kupu

The difference is that with my version there are command line options to
suppress most of the warnings so if you decide that you don't mind spurious
semicolons just use --ignore to suppress the ones you don't want. All the
warnings are numbered. See jslint.opts for the options I use for kupu, also
lint.py for a script which runs jslint.js over modified .js files only.
Also error messages all begin with filename and line number so they are
easily parsed by most editors.

Usage:
--
--browser, --nobrowser*
true if the standard browser globals should be predefined
--cap, --nocap*
true if upper case HTML should be allowed
--debug, --nodebug*
true if debugger statements should be allowed
--eqeqeq, --noeqeqeq*
true if === should be required
--error #
Specified messages are fatals
--evil, --noevil*
true if eval should be allowed
--extern #
Add external names
--help
show usage text
--ignore #
Specified messages are ignored
--jscript, --nojscript*
true if jscript deviations should be allowed
--laxLineEnd, --nolaxLineEnd*
true if line breaks should not be checked
--options #
Read additional arguments and options from a file
--passfail, --nopassfail*
true if the scan should stop on first error
--plusplus, --noplusplus*
true if increment/decrement should not be allowed
--redef, --noredef*
true if var redefinition should be allowed
--undef, --noundef*
true if undefined variables are errors
--warn #
Specified messages are warnings
--widget, --nowidget*
true if the Yahoo Widgets globals should be predefined
Jan 8 '07 #5
VK

Peter Michaux wrote:
Hi,

I'm thinking about code minimization. I can think of a few places where
whitespace matters

a + ++b
a++ + b
a - --b
a-- -b
when a line ends without a semi-colon in which case the new line
character matters.

Any others?
I guess Books of ECMA, Book of Tokens is the first place to look for
(ECMAScript 3rd ed., sec.7, "Lexical Conventions")

Besides that there is one cross-browser exception to add, but the book
above first.

Jan 8 '07 #6
Duncan Booth wrote:
"Peter Michaux" <pe**********@g mail.comwrote:
It's more the comments, blank lines and beginning or mid-line spaces
that are a bigger concern. I have been using jslint to check for
problems before jsmin but jslint has many programmer preferences built
in and not just outright problems that will be harmful when minimizing.
I suppose I could strip jslint down.

I did a modified version of jslint which might interest you. You can find
it at http://codespeak.net/svn/kupu/trunk/kupu
Thanks for the link. Your version may come in very handy.

Peter

Jan 8 '07 #7

VK wrote:
Peter Michaux wrote:
Hi,

I'm thinking about code minimization. I can think of a few places where
whitespace matters

a + ++b
a++ + b
a - --b
a-- -b
when a line ends without a semi-colon in which case the new line
character matters.

Any others?

I guess Books of ECMA, Book of Tokens is the first place to look for
(ECMAScript 3rd ed., sec.7, "Lexical Conventions")

Besides that there is one cross-browser exception to add, but the book
above first.
What is the exception?

Peter

Jan 8 '07 #8
VK

Peter Michaux wrote:
I guess Books of ECMA, Book of Tokens is the first place to look for
(ECMAScript 3rd ed., sec.7, "Lexical Conventions")

Besides that there is one cross-browser exception to add, but the book
above first.

What is the exception?
You are free to disregard, because it is not officially required by
specs, just an exploit of the internal tokenizer mechanics. Yet if it's
for public use - they you may account it as well. Any way, for long
string literals (too long to place on one line) instead of
concatenation one uses sometimes backslash trick:

var longString = "aaaaaaaaaaaaaa aaaaaa\
bbbbbbbbbbbbbbb bbbbbbbbbbbbbbb bbbb\
ccccccccccccccc ccccccccccccccc cccc"

If backslash is the very last character before the line break than the
tokenizer will be happy.

Saves a hell of cycles (no runtime concatenation) - but "hacky" of
course.

Jan 8 '07 #9
Peter Michaux wrote:
VK wrote:
>Peter Michaux wrote:
>>Hi,

I'm thinking about code minimization. I can think of a
few places where whitespace matters

a + ++b
a++ + b
a - --b
a-- -b
when a line ends without a semi-colon in which case the new
line character matters.

Any others?

I guess Books of ECMA, Book of Tokens is the first place to
look for (ECMAScript 3rd ed., sec.7, "Lexical Conventions")

Besides that there is one cross-browser exception to add,
but the book above first.

What is the exception?
Haven't you understood yet that VK has no understanding of javascript
and so does not know what is supposed to happen and what is not?
Whatever answer he gives you (if any) it is either going to be one of
his misunderstandin gs, or it will be total irrelevant to the subject of
determining which whitespace can safely be removed from javascript
source code.

It must also be worth pointing out that the more you encourage VK to
post (which is what you have just done) the more he will act to squander
the resources of the group in correcting his nonsense. Which will have
the consequence of denying you the time of the people who could
otherwise maybe usefully respond to your questions.

Richard.
Jan 8 '07 #10

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

Similar topics

16
2303
by: qwweeeit | last post by:
In analysing a very big application (pysol) made of almost 100 sources, I had the need to remove comments. Removing the comments which take all the line is straightforward... Instead for the embedded comments I used the tokenize module. To my surprise the analysed output is different from the input (the last tuple element should exactly replicate the input line) The error comes out in correspondance of a triple string.
3
3574
by: Derek Fountain | last post by:
When is the use of <xsl:text>blah blah</xsl:text> necessary? I'm finding that, although it's used religously in the book I'm learning from, if I miss it out, the output is just the same.
4
6124
by: Dwayne Epps | last post by:
I've created a function that checks form fields that only will have letters. This is the script: <script type="text/javascript" language="javascript"> function validateString(field, msg, min, max) { if (!min) { min = 1 } if (!max) { max = 65535} if (!field.value || field.value.length < min || field.value.max > max) { alert(msg); field.focus();
2
1265
by: The Plankmeister | last post by:
Hi... I hacked about for a bit and got this to work: /^\s+/ So that it matches any whitespace characters at the start of the string. But how can I modify it so that it matches strings made ONLY of whitespace? P.
3
30410
by: o_swas | last post by:
Hello, I have a JavaScript string. I want to replace all consecutive occurrences of whitespace characters like spaces, tabs, newlines, and form feeds with another string. For example, say I have a string consisting of: -- 3 spaces -- The characters "hello"
44
2835
by: rhythmace | last post by:
W3C HTML validator passes this: .... <script type="text/javascript" src="foo.js"> <script type="text/javascript"> ....script in here... </script> ....
3
1604
by: ~john | last post by:
I'm including files via PHP includes and as of now, the javascript code is being included before my header... my header contains my doctype. Does this matter? ~john
9
2557
by: amattie | last post by:
Does anyone have any idea on how I can strip the extra whitespace in the XML that shows up when I receive a response from an ASP.NET 2.0 webservice? This has been discussed before, but no one has ever come up with a good answer to what seems like such a common question. ...
19
1738
by: Prisoner at War | last post by:
Okay, Folks, I guess my real burning concern all along is a "high-level" one: just how does JavaScript interact with CSS? Right now, my newbie self only knows JavaScript and CSS to *co- exist*...but I'm beginning to get the sense that they actually interact -- or, perhaps more precisely, JavaScript acts upon CSS...but how, exactly??
0
9591
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9425
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10225
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8880
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7415
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6676
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5312
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3573
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2816
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.