Syntax

2 min read

Comments

Comments can be used to document code. A comment is text that is not executed.

Single-line comments start with two slashes (//). These comments can go on a line by themselves or they can go directly after a line of code.

1
// This is a comment on a single line.
2
// Another comment line that is not executed.
3
4
let x = 1 // Here is another comment after a line of code.

Multi-line comments start with a slash and an asterisk (/*) and end with an asterisk and a slash (*/):

1
/* This is a comment which
2
spans multiple lines. */

Comments may be nested.

1
/* /* this */ is a valid comment */

Multi-line comments are balanced.

1
/* this is a // comment up to here */ this is not part of the comment */

Documentation Comments

Documentation comments (also known as "doc-strings" or "doc-comment") are a special set of comments that can be processed by tools, for example to generate human-readable documentation, or provide documentation in an IDE.

Doc-comments either start with three slashes (///) on each line, or are surrounded by /** and **/.

1
/// This is a documentation comment for `x`.
2
/// It spans multiple lines.
3
4
let x = 1
1
/**
2
This is a documentation comment
3
which also spans multiple lines.
4
**/

Names

Names may start with any upper or lowercase letter (A-Z, a-z) or an underscore (_). This may be followed by zero or more upper and lower case letters, underscores, and numbers (0-9). Names may not begin with a number.

1
// Valid: title-case
2
//
3
PersonID
4
5
// Valid: with underscore
6
//
7
token_name
8
9
// Valid: leading underscore and characters
10
//
11
_balance
12
13
// Valid: leading underscore and numbers
14
_8264
15
16
// Valid: characters and number
17
//
18
account2
19
20
// Invalid: leading number
21
//
22
1something
23
24
// Invalid: invalid character #
25
_#1
26
27
// Invalid: various invalid characters
28
//
29
!@#$%^&*

Conventions

By convention, variables, constants, and functions have lowercase names; and types have title-case names.

Semicolons

Semicolons (;) are used as separators between declarations and statements. A semicolon can be placed after any declaration and statement, but can be omitted between declarations and if only one statement appears on the line.

Semicolons must be used to separate multiple statements if they appear on the same line.

1
// Declare a constant, without a semicolon.
2
//
3
let a = 1
4
5
// Declare a variable, with a semicolon.
6
//
7
var b = 2;
8
9
// Declare a constant and a variable on a single line, separated by semicolons.
10
//
11
let d = 1; var e = 2