This week's book giveaway is in the Java in General forum.
We're giving away four copies of Event Streams in Action and have Alexander Dean & Valentin Crettaz on-line!
See this thread for details.
Win a copy of Event Streams in Action this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

JavaScript looping?

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all -

I'm a newbie when it comes to JavaScript. When it comes to looping in JavaScript, is it that difficult to grasp at times?

Thanks.

- Josh
 
Bartender
Posts: 1868
81
Android IntelliJ IDE MySQL Database Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to CodeRanch Joshua.

I hope that you find this site welcoming, knowledgeable and sharing.

Joshua Kidd wrote:I'm a newbie when it comes to JavaScript


You may find it helpful to go through this resource https://www.w3schools.com/js/.
It has many explanations for much of the JavaScript language not just loops, but also functions, objects and more

I think that loops in JavaScript are done pretty much the same way as loops in other languages.
Do you have any specific examples or questions on loops?
 
Marshal
Posts: 67273
170
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Pete Letkeman wrote:I think that loops in JavaScript are done pretty much the same way as loops in other languages.


Well, yes and no.

Sure, the for loop still exists, and works pretty much as it does in Java, but in modern JavaScript a more functional approach is favored by using the functional methods defined on collections.

So, the forEach() method is preferred over an imperative for loop in almost all circumstances.

There are also other really useful methods to perform operations on a collection rather than looping with a for statement. For example, .map() for transforms, .filter() to pick and choose elements, .find() for searching, and (really useful) .reduce() for rolling up data. I use that last one a lot.
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Loops in JavaScript are used to execute the same block of code a specified number of times or while a specified condition is true.Mainly,in JavaScript there are two different kind of loops:
(i) for - loops through a block of code a specified number of times
(ii)while - loops through a block of code while a specified condition is true

(1)For Loop::


(2)While Loop::

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!