Reason
  • Документация
  • Попробовать
  • API
  • Сообщество
  • Блог
  • Languages iconРусский
    • 日本語
    • English
    • Deutsch
    • Español
    • Français
    • 한국어
    • Português (Brasil)
    • Українська
    • 中文
    • 繁體中文
    • Помочь с переводом
  • GitHub

›Основы языка

Введение

  • Что и зачем

Установка

  • Установка
  • Плагины для редакторов и IDE

Основы языка

  • Обзор
  • Let Bindings
  • Primitives
  • Basic Structures
  • Types
  • Records
  • Variants
  • Options and nullability
  • Functions
  • Recursion
  • Деструктуризация
  • Pattern Matching
  • Mutable Bindings
  • Loops
  • Modules

Advanced Features

  • JSX
  • External
  • Исключения
  • Объекты

JavaScript

  • Взаимодействие с JavaScript
  • Шпаргалка по синтаксису
  • Pipe First
  • Промис
  • Библиотеки
  • Конвертация из JS

Дополнительно

  • Часто задаваемые вопросы (FAQ)
  • Дополнительные инструменты
Translate

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.

← ОбзорPrimitives →
  • Bindings are Immutable
  • Binding Shadowing
  • Block Scope
  • Mutable Bindings