Comments are trivia. Their text does not become an AST value, but the parser preserves line-terminator information needed by JavaScript's grammar. A line break inside a block comment counts at a restricted boundary too, such as after return or throw, or before => (functions).
// a line comment runs to the end of the line/** @type {number} */exportdefault -42.5;
Form
Syntax
line comment
// ...
block comment
/* ... */
A line comment runs to the next newline or to the end of the file. A block comment runs to the first */, may span lines, and is an error if the file ends first. Their text may be any characters, non-ASCII included, except U+2028 and U+2029, which are refused in a comment as everywhere outside a string (line terminators): JavaScript ends a line comment at either, so what follows one is code there and never comment text here, and reads either inside a block comment as a line break.
Block comments carry JSDoc/TypeScript type declarations, which is why the language has them: a .f.js file is type-checked as JavaScript, and JSDoc is how it says what its types are.
A comment can separate tokens where whitespace can, which is between any two (trivia). At unrestricted boundaries, the ; that ends a statement may follow a comment on the same line or a later one (module structure), as any other token may. This does not make newlines interchangeable with spaces at restricted boundaries, nor a comment with a newline: where a statement omits its ;, the next one begins a new line only if a newline stands between them — inside a block comment, or ending a line comment — as JavaScript counts it.
Comments belong to the module language. A .json input containing one is an error, because JSON has no comments.
Examples
The compiler demo offers the programs comments in module.f.js, each with its real output.
The demo on this page runs sample.f.js, this page's example as a function of one number — a line comment, a JSDoc comment and a block comment inside an expression — with every output fjs compile makes of it: the expression graph (EDAG), the Rust and the WebAssembly that Rust builds to. Its Run button calls the default export with 1 and -42.5, as JavaScript and as WebAssembly, and shows what each call returned or threw.
// a line comment runs to the end of the lineexportdefault (...a) => /* a block comment */a[0] * 2;
fjs compile accepts this module, and it runs as JavaScript and as WebAssembly alike.
A FunctionalScript module and what fjs compile makes of it: the expression graph it lowers to, the Rust it compiles to on the NaNVM runtime, and the WebAssembly that Rust builds to. Run calls its default export with 1, then -42.5, first as the JavaScript it already is and then as WebAssembly, and shows what each call returned or threw and how long one call takes, timed over 1000 calls.
FunctionalScript source:
/**
* The example of [`README.md`](./README.md) (Comments) as a function of one
* number, for the page's WASM demo: a line comment, a JSDoc comment and a
* block comment inside an expression.
*
* @module
*/// a line comment runs to the end of the line/** @type {(...a: readonly number[]) => number} */exportdefault (...a) => /* a block comment */a[0] * 2;
Expression graph (EDAG) that fjs compile lowers it to:
The same graph, as fjs compile writes it to a .edag.data.js file: