Hashbang comments
This page describes JavaScript's lexical grammar. JavaScript source text is just a sequence of characters — in order for the interpreter to understand it, the string has to be parsed to a more structured representation. The initial step of parsing is called lexical analysis, in which the text gets scanned from left to right and is converted into a sequence of individual, atomic input elements. Some input elements are insignificant to the interpreter, and will be stripped after this step — they include white space and comments. The others, including identifiers, keywords, literals, and punctuators (mostly operators), will be used for further syntax analysis. Line terminators and multiline comments are also syntactically insignificant, but they guide the process for automatic semicolons insertion to make certain invalid token sequences become valid.
Browser support
| Feature | Desktop | Mobile | ||||
|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Safari | Chrome Android | Safari iOS | |
| 74 | 79 | 67 | 13.1 | 74 | 13.4 | |
Syntax
#!/usr/bin/env node
console.log('Hello from CLI!'); Use cases
-
CLI entry files
Keep a script executable from the command line while still parsing as standard JavaScript.
-
Cross-environment tooling
Use one entry file for local tooling where both shell execution and JavaScript parsing matter.
Cautions
- Hashbang comments are mainly about interoperability; they do not add runtime capability in the browser.
- Only the very first line is treated specially, so placement matters.
Accessibility
- This feature does not affect UI accessibility directly, but consistent tooling can improve developer workflows.
- Document command-line usage clearly so scripts remain understandable to contributors.