Skip to main content
Create new
Introduction
Reactivity
Props
Logic
Events
Bindings
Lifecycle
Stores
Motion
Transitions
Animations
Easing
SVG
Actions
Classes
Component composition
Context API
Special elements
Module context
Debugging
7GUIs
Miscellaneous
App.svelte
<script>
let givenName, familyName;
function update() {
givenName = 'Li Hau';
familyName = 'Tan';
}
</script>
Name: {familyName} {givenName}

<button on:click={update}>Update</button>
fetching https://unpkg.com/svelte@5.20.5/src/internal/client/index.js
import 'svelte/internal/disclose-version';
import 'svelte/internal/flags/legacy';
import * as $ from 'svelte/internal/client';

var root = $.template(` <button>Update</button>`, 1);

export default function App($$anchor) {
let givenName = $.mutable_state(),
familyName = $.mutable_state();

function update() {
$.set(givenName, 'Li Hau');
$.set(familyName, 'Tan');
}

$.next();

var fragment = root();
var text = $.first_child(fragment);
var button = $.sibling(text);

$.template_effect(() => $.set_text(text, `Name: ${$.get(familyName) ?? ''} ${$.get(givenName) ?? ''} `));
$.event('click', button, update);
$.append($$anchor, fragment);
}
result = svelte.compile(source, {
generate: ,
});
/* Add a <style> tag to see the CSS output */
		
			
				
  • Root {
    • css: null
    • js: []
    • start: 124
    • end: 197
    • type: "Root"
    • fragment: Fragment {...}
      • type: "Fragment"
      • nodes: [...] (6)
        • Text {...}
          • type: "Text"
          • start: 123
          • end: 130
          • raw: "Name: "
          • data: "Name: "
          }
        • ExpressionTag {...}
          • type: "ExpressionTag"
          • start: 130
          • end: 142
          • expression: Identifier {...}
            • type: "Identifier"
            • start: 131
            • end: 141
            • loc: {...}
              • start: {...}
                • line: 8
                • column: 7
                }
              • end: {...}
                • line: 8
                • column: 17
                }
              }
            • name: "familyName"
            }
          }
        • Text {...}
          • type: "Text"
          • start: 142
          • end: 143
          • raw: " "
          • data: " "
          }
        • ExpressionTag {...}
          • type: "ExpressionTag"
          • start: 143
          • end: 154
          • expression: Identifier {...}
            • type: "Identifier"
            • start: 144
            • end: 153
            • loc: {...}
              • start: {...}
                • line: 8
                • column: 20
                }
              • end: {...}
                • line: 8
                • column: 29
                }
              }
            • name: "givenName"
            }
          }
        • Text {...}
          • type: "Text"
          • start: 154
          • end: 156
          • raw: " "
          • data: " "
          }
        • RegularElement {...}
          • type: "RegularElement"
          • start: 156
          • end: 197
          • name: "button"
          • attributes: [...] (1)
            • OnDirective {...}
              • start: 164
              • end: 181
              • type: "OnDirective"
              • name: "click"
              • expression: Identifier {...}
                • type: "Identifier"
                • start: 174
                • end: 180
                • loc: {...}
                  • start: {...}
                    • line: 10
                    • column: 18
                    }
                  • end: {...}
                    • line: 10
                    • column: 24
                    }
                  }
                • name: "update"
                }
              • modifiers: []
              }
            ]
          • fragment: Fragment {...}
            • type: "Fragment"
            • nodes: [...] (1)
              • Text {...}
                • type: "Text"
                • start: 182
                • end: 188
                • raw: "Update"
                • data: "Update"
                }
              ]
            }
          }
        ]
      }
    • options: null
    • instance: Script {...}
      • type: "Script"
      • start: 0
      • end: 123
      • context: "default"
      • content: Program {...}
        • type: "Program"
        • start: 8
        • end: 114
        • loc: {...}
          • start: {...}
            • line: 1
            • column: 0
            }
          • end: {...}
            • line: 7
            • column: 0
            }
          }
        • body: [...] (2)
          • VariableDeclaration {...}
            • type: "VariableDeclaration"
            • start: 11
            • end: 37
            • loc: {...}
              • start: {...}
                • line: 2
                • column: 2
                }
              • end: {...}
                • line: 2
                • column: 28
                }
              }
            • declarations: [...] (2)
              • VariableDeclarator {...}
                • type: "VariableDeclarator"
                • start: 15
                • end: 24
                • loc: {...}
                  • start: {...}
                    • line: 2
                    • column: 6
                    }
                  • end: {...}
                    • line: 2
                    • column: 15
                    }
                  }
                • id: Identifier {...}
                  • type: "Identifier"
                  • start: 15
                  • end: 24
                  • loc: {...}
                    • start: {...}
                      • line: 2
                      • column: 6
                      }
                    • end: {...}
                      • line: 2
                      • column: 15
                      }
                    }
                  • name: "givenName"
                  }
                • init: null
                }
              • VariableDeclarator {...}
                • type: "VariableDeclarator"
                • start: 26
                • end: 36
                • loc: {...}
                  • start: {...}
                    • line: 2
                    • column: 17
                    }
                  • end: {...}
                    • line: 2
                    • column: 27
                    }
                  }
                • id: Identifier {...}
                  • type: "Identifier"
                  • start: 26
                  • end: 36
                  • loc: {...}
                    • start: {...}
                      • line: 2
                      • column: 17
                      }
                    • end: {...}
                      • line: 2
                      • column: 27
                      }
                    }
                  • name: "familyName"
                  }
                • init: null
                }
              ]
            • kind: "let"
            }
          • FunctionDeclaration {...}
            • type: "FunctionDeclaration"
            • start: 40
            • end: 113
            • loc: {...}
              • start: {...}
                • line: 3
                • column: 2
                }
              • end: {...}
                • line: 6
                • column: 3
                }
              }
            • id: Identifier {...}
              • type: "Identifier"
              • start: 49
              • end: 55
              • loc: {...}
                • start: {...}
                  • line: 3
                  • column: 11
                  }
                • end: {...}
                  • line: 3
                  • column: 17
                  }
                }
              • name: "update"
              }
            • expression: false
            • generator: false
            • async: false
            • params: []
            • body: BlockStatement {...}
              • type: "BlockStatement"
              • start: 58
              • end: 113
              • loc: {...}
                • start: {...}
                  • line: 3
                  • column: 20
                  }
                • end: {...}
                  • line: 6
                  • column: 3
                  }
                }
              • body: [...] (2)
                • ExpressionStatement {...}
                  • type: "ExpressionStatement"
                  • start: 64
                  • end: 85
                  • loc: {...}
                    • start: {...}
                      • line: 4
                      • column: 4
                      }
                    • end: {...}
                      • line: 4
                      • column: 25
                      }
                    }
                  • expression: AssignmentExpression {...}
                    • type: "AssignmentExpression"
                    • start: 64
                    • end: 84
                    • loc: {...}
                      • start: {...}
                        • line: 4
                        • column: 4
                        }
                      • end: {...}
                        • line: 4
                        • column: 24
                        }
                      }
                    • operator: "="
                    • left: Identifier {...}
                      • type: "Identifier"
                      • start: 64
                      • end: 73
                      • loc: {...}
                        • start: {...}
                          • line: 4
                          • column: 4
                          }
                        • end: {...}
                          • line: 4
                          • column: 13
                          }
                        }
                      • name: "givenName"
                      }
                    • right: Literal {...}
                      • type: "Literal"
                      • start: 76
                      • end: 84
                      • loc: {...}
                        • start: {...}
                          • line: 4
                          • column: 16
                          }
                        • end: {...}
                          • line: 4
                          • column: 24
                          }
                        }
                      • value: "Li Hau"
                      • raw: "'Li Hau'"
                      }
                    }
                  }
                • ExpressionStatement {...}
                  • type: "ExpressionStatement"
                  • start: 90
                  • end: 109
                  • loc: {...}
                    • start: {...}
                      • line: 5
                      • column: 4
                      }
                    • end: {...}
                      • line: 5
                      • column: 23
                      }
                    }
                  • expression: AssignmentExpression {...}
                    • type: "AssignmentExpression"
                    • start: 90
                    • end: 108
                    • loc: {...}
                      • start: {...}
                        • line: 5
                        • column: 4
                        }
                      • end: {...}
                        • line: 5
                        • column: 22
                        }
                      }
                    • operator: "="
                    • left: Identifier {...}
                      • type: "Identifier"
                      • start: 90
                      • end: 100
                      • loc: {...}
                        • start: {...}
                          • line: 5
                          • column: 4
                          }
                        • end: {...}
                          • line: 5
                          • column: 14
                          }
                        }
                      • name: "familyName"
                      }
                    • right: Literal {...}
                      • type: "Literal"
                      • start: 103
                      • end: 108
                      • loc: {...}
                        • start: {...}
                          • line: 5
                          • column: 17
                          }
                        • end: {...}
                          • line: 5
                          • column: 22
                          }
                        }
                      • value: "Tan"
                      • raw: "'Tan'"
                      }
                    }
                  }
                ]
              }
            }
          ]
        • sourceType: "module"
        }
      • attributes: []
      }
    }
The AST is not public API and may change at any point in time
Compile Svelte in your Head - Reactivity 2 • Playground • Svelte