Syntax
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.
_10// This is a comment on a single line._10// Another comment line that is not executed._10_10let 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 (*/
):
_10/* This is a comment which_10spans multiple lines. */
Comments may be nested.
_10/* /* this */ is a valid comment */
Multi-line comments are balanced.
_10/* 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 **/
.
_10/// This is a documentation comment for `x`._10/// It spans multiple lines._10_10let x = 1
_10/**_10 This is a documentation comment_10 which also spans multiple lines._10**/
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.
_29// Valid: title-case_29//_29PersonID_29_29// Valid: with underscore_29//_29token_name_29_29// Valid: leading underscore and characters_29//_29_balance_29_29// Valid: leading underscore and numbers_29_8264_29_29// Valid: characters and number_29//_29account2_29_29// Invalid: leading number_29//_291something_29_29// Invalid: invalid character #_29_#1_29_29// Invalid: various invalid characters_29//_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.
_11// Declare a constant, without a semicolon._11//_11let a = 1_11_11// Declare a variable, with a semicolon._11//_11var b = 2;_11_11// Declare a constant and a variable on a single line, separated by semicolons._11//_11let d = 1; var e = 2