Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1109845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T02:18:08+00:00 2026-05-17T02:18:08+00:00

if I have an array myArray which I am looping through: for(var i=0;i<myArray.length;i++) {

  • 0

if I have an array myArray which I am looping through:

for(var i=0;i<myArray.length;i++) {
   if(myArray[i] == myArray[i+1])
      //do something
}

Clearly, myArray[i+1] will fail on the last iteration. Short of testing if i == (myArray.length - 1), is there a clean way to not fail on that if statement and just have it evaluate to false (if myArray[i+1] is out of bounds)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T02:18:08+00:00Added an answer on May 17, 2026 at 2:18 am

    Why not just iterate up to the (N-2)th item?

    for(var i=0;i<myArray.length-1;i++) {
      ...
    

    if you must iterate till the end, the only way to work in general is to explicitly check if the index is valid. Either

    for (var i = 0; i < myArray.length; ++ i)
      if (i+1 != myArray.length) {
        ...
    

    or

    for (var i = 0; i < myArray.length; ++ i)
      if (!(i+1 in myArray)) {
        ...
    

    However, if you can ensure all items in the array cannot be undefined or null, then your original code already works because an index out-of-bound will return undefined, and undefined == x will be false unless x is also undefined or null.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have 3 MC on stage which are all alpha=0 var mcArray:Array = [mc1,mc2,mc3];
I have defined an array like so : var myArray = {myNewArray: ['string1' ,
I have a for loop that loops over one array... for i=1:length(myArray) In this
if I have an array like this: var myArray:Array = [apple,orange,strawberry]; then I want
I have a loop in my script, which iterates over an array - myArray
I have a Session variable which is an array of integers, for example: myArray{1,4,3,5,6,7,9,2,...n}
I have an javascript array, which looks like: var styles = new Array(); styles[0]
I have a very large array which must be 262144 elements in length (and
Hi I have a class which includes an array, I'm not passing this through
I have an array of which I would like to get the length of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.