This post presents a JavaScript parsing puzzle where the expression 'x = 1' followed by 'x' and '--> 0' unexpectedly prints 1. The trick is that '-->' at the beginning of a line is a valid comment token in JavaScript, a legacy feature preserved from the days when developers wrapped scripts in HTML comments to hide them from older browsers that didn't understand the script tag. This hack was standardized in ECMAScript 2015 to avoid breaking old websites, demonstrating how backward compatibility concerns shape language design.
Mar 12, 2025 Programming LanguagesSoftware Engineering