Path: csiph.com!feeder.erje.net!1.eu.feeder.erje.net!newsfeed.datemas.de!weretis.net!feeder1.news.weretis.net!news.solani.org!.POSTED!not-for-mail From: "Christoph M. Becker" Newsgroups: comp.lang.javascript Subject: Re: JSlint finds for loops intolerable Date: Sun, 28 Aug 2016 14:28:40 +0200 Organization: solani.org Lines: 61 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-Trace: solani.org 1472387313 26933 eJwNyckBACAIA7CVhFrUceTafwT9JoSJxZpGm2w2EBKuO6XK+2sWf2reobZ54fDyIbak6+A8JMgRKg== (28 Aug 2016 12:28:33 GMT) X-Complaints-To: abuse@news.solani.org NNTP-Posting-Date: Sun, 28 Aug 2016 12:28:33 +0000 (UTC) User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.2.0 X-User-ID: eJwFwYEBwDAEBMCVCK+Mg+b3HyF3sNDYzwPhIEiB+shw9ywr6rqZaf8ZaNHTvExMrUulZT8jlhEv X-NNTP-Posting-Host: eJwFwQkBwDAIA0BLK0+onRGIfwm9S8cBK5CIVEqbM4R4Lc+nQg8vuKV2aNGodc1P2lpNPENEEpQ= Cancel-Lock: sha1:wRuNuJEIH1son7Go7esaQbMGfco= In-Reply-To: Xref: csiph.com comp.lang.javascript:31191 On 28.08.2016 at 12:56, emf wrote: > In the current JSLint help webpage (http://jslint.com/help.html) it now > says: > > "Tolerate for statement: > true if the for statement should be allowed > It is almost always better to use the array methods instead." JSLint, as well as its author, are highly opinitated. :-) > Getting rid of the for loops is a big change. Reflecting on it, however, > the for loop is an awkward construction -- it's just something you learn > as a matter of fact when you begin and that time on you take it for > granted. Instead of > > for (var i = 0; i < 10; i += 1) { > ... > } > > the while loop does seem more straightforward: > > var i = 0; > while (i < 10) { > ... > i += 1; > } I would prefer the `while` only, if you can count down, e.g. var i = 10; while (--i) { ... } > After stopping using for loops, I can imagine a time when I won't be > able to understand why I once almost always preferred to use them. Perhaps because they clearly signal a loop with N iterations? > I do not understand, however, the comment about array methods. I seldom > find use for them. Can someone explain what Crawford implies?? Instead of looping with `for` or `while` over all elements of an array, you can instead do: myArray.forEach(function(el) { ... }); Once you get the hang of using Array.prototype.forEach(), you'll probably find many places where a more specific array method is more appropriate (for instance, .filter() and map()), and these can simplify the code (even more). Note that this programming style is often referred to as applicative or functional programming. You may consider to learn about it (I can recommend ). -- Christoph M. Becker