Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

If let statement

The if..let statement may be used for pattern matching on algebraic data types.

if (let Plus(10, right) = exp) {
    //
}

It may also be used for extracting a non-nullable variable from a test expression, without using a matching pattern; that is, a regular identifier binding.

if (let node = parseAtom()) {
    //
}

if..let can also be useful for, say, iterator results.

if (let [x!,false] = characters.next()) {
    //
} else {
    break;
}

Syntax

    IfLetStatementabbrev :
      if ( IfLetVariable ) Substatementabbrev
      if ( IfLetVariable ) SubstatementnoShortIf else Substatementabbrev
    IfLetStatementfull :
      if ( IfLetVariable ) Substatementfull
      if ( IfLetVariable ) SubstatementnoShortIf else Substatementfull
    IfLetStatementnoShortIf :
      if ( IfLetVariable ) SubstatementnoShortIf else SubstatementnoShortIf
    IfLetVariable :
      VariableDefinitionKind VariableBindingallowIn