How to specify index of JavaScript for statement

Since I've been working in VB and Delphi, I'm always a little confused about how to specify the loop value when writing a C / C ++ for statement such as JavaScript, so make a note of it.

The simple way to write when you want the index value to loop from 0 to length -1 is as follows.

i ++ and i-- respect JsLint and try not to use it.

  var str = 'abc';
  for (var i = 0; i <= str.length - 1; i += 1) {
    console.log(str[i]);
  }
  //Output in order of a b c

However, I think that it is a language characteristic of the C-type for statement, but the end judgment part of the for loop for each loop

i <= str.length - 1



 Because this is evaluated, the process of getting str.length occurs every time, which makes the loop a little slower.

 Therefore, it is better to assign the number of loops to a variable first with an emphasis on speeding up.

```javascript
  var str = 'abc';
  for (var i = 0, il = str.length - 1; i <= il; i += 1) {
    console.log(str[i]);
  }
  //Output in order of a b c

This is also written like this.

  var str = 'abc';
  for (var i = 0, il = str.length; i < il; i += 1) {
    console.log(str[i]);
  }
  //Output in order of a b c

This makes it hard to read the end value of the loop, which still makes me feel a little uncomfortable, but for C / C ++ / Java people, it's not unreadable, so it's a common code. It would be nice to be able to read it.

In the reverse order, it is better to write like this.

  var str = 'abc';
  for (var i = str.length - 1; 0 <= i; i -= 1) {
    console.log(str[i]);
  }
  //Output in order of c b a

Also, if you devise and use the for syntax, you can also do the following.

  for (var i = str.length; i--;) {
    console.log(str[i]);
  }
  //Output in order of c b a

The full text of the operation check is as follows.

index.html


<!DOCTYPE html>
<html lang="ja">
<head>
  <meta charset="utf-8">
  <title></title>
<script>

  var str = 'abc';

  for (var i = 0; i <= str.length - 1; i += 1) {
    console.log(str[i]);
  }
  //Output in order of a b c

  for (var i = 0, il = str.length - 1; i <= il; i += 1) {
    console.log(str[i]);
  }
  //Output in order of a b c

  for (var i = 0, il = str.length; i < il; i += 1) {
    console.log(str[i]);
  }
  //Output in order of a b c

  for (var i = str.length - 1; 0 <= i; i -= 1) {
    console.log(str[i]);
  }
  //Output in order of c b a

  for (var i = str.length; i--;) {
    console.log(str[i]);
  }
  //Output in order of c b a

</script>
</head><body>
</body></html>

Recommended Posts

How to specify index of JavaScript for statement
How to specify validation for time_field
How to get the contents of Map using for statement Memorandum
[SwiftUI] How to specify the abbreviated position of Text
How to loop Java Map (for Each / extended for statement)
How to execute WebCamCapture sample of NyARToolkit for Java
Comparison of how to write Callback function (Java, JavaScript, Ruby)
The idea of C # (lambda expression, for statement) to chew
How to output array values without using a for statement
How to install JMeter for Mac
How to use setDefaultCloseOperation () of JFrame
java Eclipse How to debug javaScript
[Rails] How to change the page title of the browser for each page
How to check for the contents of a java fixed-length string
How to make a groundbreaking diamond using Java for statement wwww
[swift5] How to specify color in hexadecimal
How to use binding.pry for view files
How to install Play Framework 2.6 for Mac
How to name variables 7 selections of discomfort
[java] Summary of how to handle char
How to switch thumbnail images with JavaScript
Summary of how to write annotation arguments
How to create a Maven repository for 2020
How to determine the number of parallels
How to switch the display of the header menu for each transition page
How to build an environment for any version of Ruby using rbenv
[Java] [Maven3] Summary of how to use Maven3
[For Rails beginners] Summary of how to use RSpec (get an overview)
Needed for iOS 14? How to set NSUserTrackingUsageDescription
How to specify id attribute in JSF
[For beginners] How to debug in Eclipse
How to display the amount of disk used by Docker container for each container
[Java] How to get HashMap elements by loop control using extended for statement
How to start with Hyper-V instead of WSL2 on Docker Desktop for Windows
Summary of how to select elements in Selenium
JDBC promises and examples of how to write
[Java] How to test for null with JUnit
How to create a database for H2 Database anywhere
How to find OSS trends for web development
[java] Summary of how to handle character strings
[Rails] Introduction of pry-rails ~ How to debug binding.pry
Summary of how to create JSF self-made tags
Comparison of for instructions to fetch arrays sequentially
Customize how to divide the contents of Recyclerview
[Bcrypt] how to cancel presence: true of has_secure_password
[Java] Summary of how to abbreviate lambda expressions
How to use an array for HashMap keys
How to play audio and music using javascript
[Rails] How to implement unit tests for models
How to get today's day of the week
[For beginners] How to implement the delete function
Output of how to use the slice method
How to make a lightweight JRE for distribution
[Validation] rails How to specify after today's date
[Java] (for MacOS) How to set the classpath
[Swift UI] How to disable ScrollsToTop of ScrollView
How to use JQuery in js.erb of Rails6
[Rails] How to use Gem'rails-i18n' for Japanese support
How to display the result of form input
How to use nginx-ingress-controller with Docker for Mac
[For super beginners] How to use autofocus: true