Home  >  Article  >  Web Front-end  >  Why Does Console.log() Display Modified Values Instead of Original Values in Chrome?

Why Does Console.log() Display Modified Values Instead of Original Values in Chrome?

Patricia Arquette
Patricia ArquetteOriginal
2024-10-20 12:34:02379browse

Why Does Console.log() Display Modified Values Instead of Original Values in Chrome?

Console.log() Inconsistencies in Chrome when Logging Objects and Arrays

In Google Chrome, unexpected behavior occurs when using console.log() to log nested arrays or objects. If you modify the innermost value of an array or object after logging it, console.log() will display the modified value, not the original values at the time of logging.

Steps to Reproduce

To recreate the issue:

  1. Create a nested array, e.g., [[345, "test"]].
  2. Log the array to the console with console.log().
  3. Modify the inner array value, e.g., test0 = 1111111.
  4. Console.log() will now output [[1111111, "test"]].

This inconsistency also affects objects and arrays stored in variables (testb, testc).

Underlying Issue

After extensive research, it was discovered that this issue was previously reported as a bug in WebKit (https://bugs.webkit.org/show_bug.cgi?id=35801). Chromium developers acknowledged the issue, but stated that a fix was unlikely due to technical limitations.

Fix and Inconsistencies

However, a fix was implemented in WebKit in 2012. Despite this, Google Chrome has yet to adopt the fix. As a result, console.log() behavior differs depending on whether the console window is open at the time of logging.

  • Console window open: Console.log() will display current values, even if they have changed.
  • Console window initially closed: Console.log() will display the values at the time of logging.

Conclusion

This bug in Google Chrome's console.log() functionality persists and affects debugging efforts. It is recommended to be aware of this inconsistency and consider alternative debugging methods until the issue is resolved in Chrome.

The above is the detailed content of Why Does Console.log() Display Modified Values Instead of Original Values in Chrome?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn