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

  • Home
  • SEARCH
  • 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 3433642
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T07:36:04+00:00 2026-05-18T07:36:04+00:00

I was writing a javascript program and running it in Chrome 7, when I

  • 0

I was writing a javascript program and running it in Chrome 7, when I encountered some strange behavior. Now, in my code, with all the other stuff going on, it took me some time to figure out it wasn’t me.

I have distilled the essence of the code below.

<html>

<script>

var data = [1,2,3,4,5];

var data_copy = [];

for (var i=0; i<data.length; i++){
    data_copy.push(data[i]);
}

console.log("Printing before:");
console.log(data_copy);

//alert(data_copy);

console.log("------------------------");

for (var i=0; i<data_copy.length; i++){
    data_copy[i] = data_copy[i] * 1000;
}

console.log("Printing after:");
console.log(data_copy);

</script>

</html>

When I run this on Chrome 7, I get the output that follows in the Javascript console:

Printing before:
[1000, 2000, 3000, 4000, 5000]
------------------------
Printing after:
[1000, 2000, 3000, 4000, 5000]

How come the first call to console.log prints the changed version of data_copy?

Now, if I uncomment the “alert” and run the same code, I get what you would normally expect:

Printing before:
[1, 2, 3, 4, 5]
------------------------
Printing after:
[1000, 2000, 3000, 4000, 5000]

I also tried the code in node.js and I get the second (normal) output.

Any ideas?

Is this some JIT optimization gone awry?

Or am I missing something obvious?

  • 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-18T07:36:04+00:00Added an answer on May 18, 2026 at 7:36 am

    Change console.log(data_copy) to console.log(String(data_copy)).

    console.log effectively sends the object by reference to Chrome’s Console. alert interrupts your script so the first logged data_copy gets rendered before the later modification; without, the entire script runs to completion before the console renders either data_copy reference.

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

Sidebar

Related Questions

I'm writing a program to add some code to html files I was going
I'm writing some JavaScript code to parse user-entered functions (for spreadsheet-like functionality). Having parsed
I am writing a JavaScript program in Rhino which needs to load other JavaScript
I'm writing some Javascript to resize the large image to fit into the user's
I have problem in some JavaScript that I am writing where the Switch statement
I am writing some UI tests using Selenium and i have a JavaScript Tree
I'm writing a piece of client-side javascript code that takes a function and finds
I am writing some javascript to be executed by the Windows Scripting Host, and
I'm writing a quiz program using PHP and a bit of Javascript. The questions
I'm writing some JavaScript to handle the common scenario of dynamically enabling a form

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.