diff options
author | Jo-Philipp Wich <jo@mein.io> | 2021-05-18 14:18:04 +0200 |
---|---|---|
committer | Jo-Philipp Wich <jo@mein.io> | 2021-05-18 16:09:36 +0200 |
commit | 5714705aee31a0d561b4cf1b8096bbe19e17a825 (patch) | |
tree | 1c249365983089fb249b1f109f13ed3a590863d0 /README.md | |
parent | 2c2e603204be7ec96cab7504e3ee6951412ad4bc (diff) |
syntax: introduce `const` support
Introduce support for declaring constant variables through the `const`
keyword. Variables declared with `const` follow the same scoping rules
as `let` declared ones.
In contrast to normal variables, `const` ones may not be assigned to
after their declaration. Any attempt to do so will result in a syntax
error during compilation.
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Diffstat (limited to 'README.md')
-rw-r--r-- | README.md | 16 |
1 files changed, 15 insertions, 1 deletions
@@ -154,9 +154,14 @@ through numeric operations, or explicitely, e.g. by invoking functions such as Variable names must start with a letter or an underscore and may only contain the characters `A`..`Z`, `a`..`z`, `0`..`9` or `_`. By prefixing a variable -name with the keyword `let`, it is declared in the local function scope only +name with the keyword `let`, it is declared in the local block scope only and not visible outside anymore. +Variables may also be declared using the `const` keyword. Such variables follow +the same scoping rules as `let` declared ones but they cannot be modified after +they have been declared. Any attempt to do so will result in a syntax error +during compilation. + ```javascript {% @@ -172,6 +177,15 @@ and not visible outside anymore. print(a, "\n"); // outputs "2" print(b, "\n"); // outputs nothing + const c = 3; + print(c, "\n"); // outputs "3" + + c = 4; // raises syntax error + c++; // raises syntax error + + const d; // raises syntax error, const variables must + // be initialized at declaration time + %} ``` |