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 6725805
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:51:03+00:00 2026-05-26T09:51:03+00:00

How exactly does Javascript’s array.reverse() work? Does it go through and swap every element

  • 0

How exactly does Javascript’s array.reverse() work? Does it go through and swap every element of the array? If so, does it take O(n) to swap an array of size n?

I guess the reason I am asking is because I was wondering if array.reverse() was the same as:

for(var i = 0; i < a.length / 2; i++) {
  var holder = a[i];
  a[i] = a[a.length - 1 - i];
  a[a.length - 1 - i] = holder;
}

NOTE: Sorry if the Javascript code I posted is incorrect, it’s pretty late right now.

EDIT: Fixed a.length to a.length / 2.

  • 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-26T09:51:03+00:00Added an answer on May 26, 2026 at 9:51 am

    For the full details of how it works, read the relevant section of the spec. Here’s the algorithm:

    1. Let O be the result of calling ToObject passing the this value as the argument.

      1. Let lenVal be the result of calling the [[Get]] internal method of O with argument “length”.
      2. Let len be ToUint32(lenVal).
      3. Let middle be floor(len/2).
      4. Letlower be 0.
      5. Repeat, while lower ≠ middle

        1. Let upper be len−lower −1.
        2. Let upperP be ToString(upper).
        3. Let lowerP be ToString(lower).
        4. Let lowerValue be the result of calling the [[Get]] internal method of O with argument lowerP.
        5. Let upperValue be the result of calling the [[Get]] internal method of O with argument upperP .
        6. Let lowerExists be the result of calling the [[HasProperty]] internal method of O with argument lowerP.
        7. Let upperExists be the result of calling the [[HasProperty]] internal method of O with argument upperP.
        8. If lowerExists is true and upperExists is true, then

        9. Call the [[Put]] internal method of O with arguments lowerP, upperValue, and true .

        10. Call the [[Put]] internal method of O with arguments upperP, lowerValue, and true .
        11. Else if lowerExists is false and upperExists is true, then
        12. Call the [[Put]] internal method of O with arguments lowerP, upperValue, and true .
        13. Call the [[Delete]] internal method of O, with arguments upperP and true.
        14. Else if lowerExists is true and upperExists is false, then
        15. Call the [[Delete]] internal method of O, with arguments lowerP and true .
        16. Call the [[Put]] internal method of O with arguments upperP, lowerValue, and true .
        17. Else, both lowerExists and upperExists are false
        18. No action is required.
        19. Increase lower by 1.
      6. Return O .
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How exactly does NSInvocation work? Is there a good introduction? I’m specifically having issues
How does the jQuery tag $(this) exactly work? I know how to use it,
How exactly does it relate to jQuery? I know the library uses native javascript
As already asked: how does sort function work in JavaScript, along with the compare
What does this JavaScript do exactly? parent.kill = 1; This is used in a
I am trying to find out exactly how JavaScript timers work. In particular whether
What exactly does the word patch mean when referring to 'submitting a patch'? I've
What exactly does null do performance and storage (space) wise in MySQL? For example:
What exactly does immutable mean - that is, what are the consequences of an
What exactly does putting extern C into C++ code do? For example: extern C

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.