Get the latest tech news

A Perplexing JavaScript Parsing Puzzle


What does this print? x =1 x -->0 Think it through, then try it in a browser console! Answer and explanation in the dropdown. Show answer It prints 1. wait wtf At the beginning of a line (and only at the beginning of a line), --> starts a comment. The JavaScript is parsed as x=1; x; // 0 The browser then displays the value of the last expression, which of course is 1.

Older browsers in common use (like Navigator 1) had no idea that<script> content was anything special and wrote it as regular text on the page. To ensure graceful degradation, webdevs would wrap their scripts in html comment blocks: I train companies in formal methods, making software development faster, cheaper, and safer.

Get the Android app

Or read this on Hacker News