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.34let 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 which2spans 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 (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.34let x = 1
1/**2This is a documentation comment3which also spans multiple lines.4**/
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-case2//3PersonID45// Valid: with underscore6//7token_name89// Valid: leading underscore and characters10//11_balance1213// Valid: leading underscore and numbers14_82641516// Valid: characters and number17//18account21920// Invalid: leading number21//221something2324// Invalid: invalid character #25_#12627// Invalid: various invalid characters28//29!@#$%^&*
By convention, variables, constants, and functions have lowercase names; and types have title-case names.
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//3let a = 145// Declare a variable, with a semicolon.6//7var b = 2;89// Declare a constant and a variable on a single line, separated by semicolons.10//11let d = 1; var e = 2