Skip to content

Latest commit

 

History

History
60 lines (44 loc) · 1.24 KB

let-binding.md

File metadata and controls

60 lines (44 loc) · 1.24 KB
title
Let Bindings

Quick overview: Let Bindings

A "let binding" binds values to names. In other languages they might be called a "variable declaration". The binding is immutable and can be referenced by code that comes after them.

let greeting = "hello!";
let score = 10;
let newScore = 10 + score;

Note: If you are coming from JavaScript, these bindings behave like const, not like var orlet.

Bindings are Immutable

Reason let bindings are "immutable", they cannot change after they are created.

let x = 10;
/* Error: Invalid code! */
x = x + 13;

Binding Shadowing

Bindings can be shadowed to give the appearance of updating them. This is a common pattern that should be used when it seems like a variable needs to be updated.

let x = 10;
let x = x + 10;
let x = x + 3;
/* x is 23 */

Block Scope

Bindings can be manually scoped using {}.

let message = {
  let part1 = "hello";
  let part2 = "world";
  part1 ++ " " ++ part2
};
/* `part1` and `part2` not accessible here! */

The last line of a block is implicitly returned.

Mutable Bindings

If you really need a mutable binding then check out the ref feature.