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 a = 4;
let b = 9;
let sum;
function calcSum() {
sum = a + b;
}
function sendSumToServer() {
console.log("sending", sum);
}
$: if (!isNaN(a) && !isNaN(b)) {
calcSum();
sendSumToServer();
}
</script>
<label>a: <input type="number" bind:value={a}></label>
<label>b: <input type="number" bind:value={b}></label>
<p>{sum}</p>
sending 13
import 'svelte/internal/disclose-version';
import 'svelte/internal/flags/legacy';
import * as $ from 'svelte/internal/client';

var root = $.template(`<label>a: <input type="number"></label> <label>b: <input type="number"></label> <p> </p>`, 1);

export default function App($$anchor, $$props) {
$.push($$props, false);

let a = $.mutable_state(4);
let b = $.mutable_state(9);
let sum = $.mutable_state();

function calcSum() {
$.set(sum, $.get(a) + $.get(b));
}

function sendSumToServer() {
console.log("sending", $.get(sum));
}

$.legacy_pre_effect(() => ($.get(a), $.get(b)), () => {
if (!isNaN($.get(a)) && !isNaN($.get(b))) {
calcSum();
sendSumToServer();
}
});

$.legacy_pre_effect_reset();

var fragment = root();
var label = $.first_child(fragment);
var input = $.sibling($.child(label));

$.remove_input_defaults(input);
$.reset(label);
result = svelte.compile(source, {
generate: ,
});
/* Add a <style> tag to see the CSS output */
		
			
				
  • Root {
    • css: null
    • js: []
    • start: 228
    • end: 352
    • type: "Root"
    • fragment: Fragment {...}
      • type: "Fragment"
      • nodes: [...] (6)
        • Text {...}
          • type: "Text"
          • start: 227
          • end: 228
          • raw: "\n"
          • data: "\n"
          }
        • RegularElement {...}
          • type: "RegularElement"
          • start: 228
          • end: 282
          • name: "label"
          • attributes: []
          • fragment: Fragment {...}
            • type: "Fragment"
            • nodes: [...] (2)
              • Text {...}
                • type: "Text"
                • start: 235
                • end: 238
                • raw: "a: "
                • data: "a: "
                }
              • RegularElement {...}
                • type: "RegularElement"
                • start: 238
                • end: 274
                • name: "input"
                • attributes: [...] (2)
                  • Attribute {...}
                    • type: "Attribute"
                    • start: 245
                    • end: 258
                    • name: "type"
                    • value: [...] (1)
                      • Text {...}
                        • start: 251
                        • end: 257
                        • type: "Text"
                        • raw: "number"
                        • data: "number"
                        }
                      ]
                    }
                  • BindDirective {...}
                    • start: 259
                    • end: 273
                    • type: "BindDirective"
                    • name: "value"
                    • expression: Identifier {...}
                      • type: "Identifier"
                      • start: 271
                      • end: 272
                      • loc: {...}
                        • start: {...}
                          • line: 16
                          • column: 43
                          }
                        • end: {...}
                          • line: 16
                          • column: 44
                          }
                        }
                      • name: "a"
                      }
                    • modifiers: []
                    }
                  ]
                • fragment: Fragment {...}
                  • type: "Fragment"
                  • nodes: []
                  }
                }
              ]
            }
          }
        • Text {...}
          • type: "Text"
          • start: 282
          • end: 284
          • raw: " "
          • data: " "
          }
        • RegularElement {...}
          • type: "RegularElement"
          • start: 284
          • end: 338
          • name: "label"
          • attributes: []
          • fragment: Fragment {...}
            • type: "Fragment"
            • nodes: [...] (2)
              • Text {...}
                • type: "Text"
                • start: 291
                • end: 294
                • raw: "b: "
                • data: "b: "
                }
              • RegularElement {...}
                • type: "RegularElement"
                • start: 294
                • end: 330
                • name: "input"
                • attributes: [...] (2)
                  • Attribute {...}
                    • type: "Attribute"
                    • start: 301
                    • end: 314
                    • name: "type"
                    • value: [...] (1)
                      • Text {...}
                        • start: 307
                        • end: 313
                        • type: "Text"
                        • raw: "number"
                        • data: "number"
                        }
                      ]
                    }
                  • BindDirective {...}
                    • start: 315
                    • end: 329
                    • type: "BindDirective"
                    • name: "value"
                    • expression: Identifier {...}
                      • type: "Identifier"
                      • start: 327
                      • end: 328
                      • loc: {...}
                        • start: {...}
                          • line: 17
                          • column: 43
                          }
                        • end: {...}
                          • line: 17
                          • column: 44
                          }
                        }
                      • name: "b"
                      }
                    • modifiers: []
                    }
                  ]
                • fragment: Fragment {...}
                  • type: "Fragment"
                  • nodes: []
                  }
                }
              ]
            }
          }
        • Text {...}
          • type: "Text"
          • start: 338
          • end: 340
          • raw: " "
          • data: " "
          }
        • RegularElement {...}
          • type: "RegularElement"
          • start: 340
          • end: 352
          • name: "p"
          • attributes: []
          • fragment: Fragment {...}
            • type: "Fragment"
            • nodes: [...] (1)
              • ExpressionTag {...}
                • type: "ExpressionTag"
                • start: 343
                • end: 348
                • expression: Identifier {...}
                  • type: "Identifier"
                  • start: 344
                  • end: 347
                  • loc: {...}
                    • start: {...}
                      • line: 18
                      • column: 4
                      }
                    • end: {...}
                      • line: 18
                      • column: 7
                      }
                    }
                  • name: "sum"
                  }
                }
              ]
            }
          }
        ]
      }
    • options: null
    • instance: Script {...}
      • type: "Script"
      • start: 0
      • end: 227
      • context: "default"
      • content: Program {...}
        • type: "Program"
        • start: 8
        • end: 218
        • loc: {...}
          • start: {...}
            • line: 1
            • column: 0
            }
          • end: {...}
            • line: 15
            • column: 0
            }
          }
        • body: [...] (6)
          • VariableDeclaration {...}
            • type: "VariableDeclaration"
            • start: 10
            • end: 20
            • loc: {...}
              • start: {...}
                • line: 2
                • column: 1
                }
              • end: {...}
                • line: 2
                • column: 11
                }
              }
            • declarations: [...] (1)
              • VariableDeclarator {...}
                • type: "VariableDeclarator"
                • start: 14
                • end: 19
                • loc: {...}
                  • start: {...}
                    • line: 2
                    • column: 5
                    }
                  • end: {...}
                    • line: 2
                    • column: 10
                    }
                  }
                • id: Identifier {...}
                  • type: "Identifier"
                  • start: 14
                  • end: 15
                  • loc: {...}
                    • start: {...}
                      • line: 2
                      • column: 5
                      }
                    • end: {...}
                      • line: 2
                      • column: 6
                      }
                    }
                  • name: "a"
                  }
                • init: Literal {...}
                  • type: "Literal"
                  • start: 18
                  • end: 19
                  • loc: {...}
                    • start: {...}
                      • line: 2
                      • column: 9
                      }
                    • end: {...}
                      • line: 2
                      • column: 10
                      }
                    }
                  • value: 4
                  • raw: "4"
                  }
                }
              ]
            • kind: "let"
            }
          • VariableDeclaration {...}
            • type: "VariableDeclaration"
            • start: 22
            • end: 32
            • loc: {...}
              • start: {...}
                • line: 3
                • column: 1
                }
              • end: {...}
                • line: 3
                • column: 11
                }
              }
            • declarations: [...] (1)
              • VariableDeclarator {...}
                • type: "VariableDeclarator"
                • start: 26
                • end: 31
                • loc: {...}
                  • start: {...}
                    • line: 3
                    • column: 5
                    }
                  • end: {...}
                    • line: 3
                    • column: 10
                    }
                  }
                • id: Identifier {...}
                  • type: "Identifier"
                  • start: 26
                  • end: 27
                  • loc: {...}
                    • start: {...}
                      • line: 3
                      • column: 5
                      }
                    • end: {...}
                      • line: 3
                      • column: 6
                      }
                    }
                  • name: "b"
                  }
                • init: Literal {...}
                  • type: "Literal"
                  • start: 30
                  • end: 31
                  • loc: {...}
                    • start: {...}
                      • line: 3
                      • column: 9
                      }
                    • end: {...}
                      • line: 3
                      • column: 10
                      }
                    }
                  • value: 9
                  • raw: "9"
                  }
                }
              ]
            • kind: "let"
            }
          • VariableDeclaration {...}
            • type: "VariableDeclaration"
            • start: 34
            • end: 42
            • loc: {...}
              • start: {...}
                • line: 4
                • column: 1
                }
              • end: {...}
                • line: 4
                • column: 9
                }
              }
            • declarations: [...] (1)
              • VariableDeclarator {...}
                • type: "VariableDeclarator"
                • start: 38
                • end: 41
                • loc: {...}
                  • start: {...}
                    • line: 4
                    • column: 5
                    }
                  • end: {...}
                    • line: 4
                    • column: 8
                    }
                  }
                • id: Identifier {...}
                  • type: "Identifier"
                  • start: 38
                  • end: 41
                  • loc: {...}
                    • start: {...}
                      • line: 4
                      • column: 5
                      }
                    • end: {...}
                      • line: 4
                      • column: 8
                      }
                    }
                  • name: "sum"
                  }
                • init: null
                }
              ]
            • kind: "let"
            }
          • FunctionDeclaration {...}
            • type: "FunctionDeclaration"
            • start: 44
            • end: 82
            • loc: {...}
              • start: {...}
                • line: 5
                • column: 1
                }
              • end: {...}
                • line: 7
                • column: 2
                }
              }
            • id: Identifier {...}
              • type: "Identifier"
              • start: 53
              • end: 60
              • loc: {...}
                • start: {...}
                  • line: 5
                  • column: 10
                  }
                • end: {...}
                  • line: 5
                  • column: 17
                  }
                }
              • name: "calcSum"
              }
            • expression: false
            • generator: false
            • async: false
            • params: []
            • body: BlockStatement {...}
              • type: "BlockStatement"
              • start: 63
              • end: 82
              • loc: {...}
                • start: {...}
                  • line: 5
                  • column: 20
                  }
                • end: {...}
                  • line: 7
                  • column: 2
                  }
                }
              • body: [...] (1)
                • ExpressionStatement {...}
                  • type: "ExpressionStatement"
                  • start: 67
                  • end: 79
                  • loc: {...}
                    • start: {...}
                      • line: 6
                      • column: 2
                      }
                    • end: {...}
                      • line: 6
                      • column: 14
                      }
                    }
                  • expression: AssignmentExpression {...}
                    • type: "AssignmentExpression"
                    • start: 67
                    • end: 78
                    • loc: {...}
                      • start: {...}
                        • line: 6
                        • column: 2
                        }
                      • end: {...}
                        • line: 6
                        • column: 13
                        }
                      }
                    • operator: "="
                    • left: Identifier {...}
                      • type: "Identifier"
                      • start: 67
                      • end: 70
                      • loc: {...}
                        • start: {...}
                          • line: 6
                          • column: 2
                          }
                        • end: {...}
                          • line: 6
                          • column: 5
                          }
                        }
                      • name: "sum"
                      }
                    • right: BinaryExpression {...}
                      • type: "BinaryExpression"
                      • start: 73
                      • end: 78
                      • loc: {...}
                        • start: {...}
                          • line: 6
                          • column: 8
                          }
                        • end: {...}
                          • line: 6
                          • column: 13
                          }
                        }
                      • left: Identifier {...}
                        • type: "Identifier"
                        • start: 73
                        • end: 74
                        • loc: {...}
                          • start: {...}
                            • line: 6
                            • column: 8
                            }
                          • end: {...}
                            • line: 6
                            • column: 9
                            }
                          }
                        • name: "a"
                        }
                      • operator: "+"
                      • right: Identifier {...}
                        • type: "Identifier"
                        • start: 77
                        • end: 78
                        • loc: {...}
                          • start: {...}
                            • line: 6
                            • column: 12
                            }
                          • end: {...}
                            • line: 6
                            • column: 13
                            }
                          }
                        • name: "b"
                        }
                      }
                    }
                  }
                ]
              }
            }
          • FunctionDeclaration {...}
            • type: "FunctionDeclaration"
            • start: 84
            • end: 146
            • loc: {...}
              • start: {...}
                • line: 8
                • column: 1
                }
              • end: {...}
                • line: 10
                • column: 2
                }
              }
            • id: Identifier {...}
              • type: "Identifier"
              • start: 93
              • end: 108
              • loc: {...}
                • start: {...}
                  • line: 8
                  • column: 10
                  }
                • end: {...}
                  • line: 8
                  • column: 25
                  }
                }
              • name: "sendSumToServer"
              }
            • expression: false
            • generator: false
            • async: false
            • params: []
            • body: BlockStatement {...}
              • type: "BlockStatement"
              • start: 111
              • end: 146
              • loc: {...}
                • start: {...}
                  • line: 8
                  • column: 28
                  }
                • end: {...}
                  • line: 10
                  • column: 2
                  }
                }
              • body: [...] (1)
                • ExpressionStatement {...}
                  • type: "ExpressionStatement"
                  • start: 115
                  • end: 143
                  • loc: {...}
                    • start: {...}
                      • line: 9
                      • column: 2
                      }
                    • end: {...}
                      • line: 9
                      • column: 30
                      }
                    }
                  • expression: CallExpression {...}
                    • type: "CallExpression"
                    • start: 115
                    • end: 142
                    • loc: {...}
                      • start: {...}
                        • line: 9
                        • column: 2
                        }
                      • end: {...}
                        • line: 9
                        • column: 29
                        }
                      }
                    • callee: MemberExpression {...}
                      • type: "MemberExpression"
                      • start: 115
                      • end: 126
                      • loc: {...}
                        • start: {...}
                          • line: 9
                          • column: 2
                          }
                        • end: {...}
                          • line: 9
                          • column: 13
                          }
                        }
                      • object: Identifier {...}
                        • type: "Identifier"
                        • start: 115
                        • end: 122
                        • loc: {...}
                          • start: {...}
                            • line: 9
                            • column: 2
                            }
                          • end: {...}
                            • line: 9
                            • column: 9
                            }
                          }
                        • name: "console"
                        }
                      • property: Identifier {...}
                        • type: "Identifier"
                        • start: 123
                        • end: 126
                        • loc: {...}
                          • start: {...}
                            • line: 9
                            • column: 10
                            }
                          • end: {...}
                            • line: 9
                            • column: 13
                            }
                          }
                        • name: "log"
                        }
                      • computed: false
                      • optional: false
                      }
                    • arguments: [...] (2)
                      • Literal {...}
                        • type: "Literal"
                        • start: 127
                        • end: 136
                        • loc: {...}
                          • start: {...}
                            • line: 9
                            • column: 14
                            }
                          • end: {...}
                            • line: 9
                            • column: 23
                            }
                          }
                        • value: "sending"
                        • raw: "\"sending\""
                        }
                      • Identifier {...}
                        • type: "Identifier"
                        • start: 138
                        • end: 141
                        • loc: {...}
                          • start: {...}
                            • line: 9
                            • column: 25
                            }
                          • end: {...}
                            • line: 9
                            • column: 28
                            }
                          }
                        • name: "sum"
                        }
                      ]
                    • optional: false
                    }
                  }
                ]
              }
            }
          • LabeledStatement {...}
            • type: "LabeledStatement"
            • start: 148
            • end: 217
            • loc: {...}
              • start: {...}
                • line: 11
                • column: 1
                }
              • end: {...}
                • line: 14
                • column: 2
                }
              }
            • body: IfStatement {...}
              • type: "IfStatement"
              • start: 151
              • end: 217
              • loc: {...}
                • start: {...}
                  • line: 11
                  • column: 4
                  }
                • end: {...}
                  • line: 14
                  • column: 2
                  }
                }
              • test: LogicalExpression {...}
                • type: "LogicalExpression"
                • start: 155
                • end: 177
                • loc: {...}
                  • start: {...}
                    • line: 11
                    • column: 8
                    }
                  • end: {...}
                    • line: 11
                    • column: 30
                    }
                  }
                • left: UnaryExpression {...}
                  • type: "UnaryExpression"
                  • start: 155
                  • end: 164
                  • loc: {...}
                    • start: {...}
                      • line: 11
                      • column: 8
                      }
                    • end: {...}
                      • line: 11
                      • column: 17
                      }
                    }
                  • operator: "!"
                  • prefix: true
                  • argument: CallExpression {...}
                    • type: "CallExpression"
                    • start: 156
                    • end: 164
                    • loc: {...}
                      • start: {...}
                        • line: 11
                        • column: 9
                        }
                      • end: {...}
                        • line: 11
                        • column: 17
                        }
                      }
                    • callee: Identifier {...}
                      • type: "Identifier"
                      • start: 156
                      • end: 161
                      • loc: {...}
                        • start: {...}
                          • line: 11
                          • column: 9
                          }
                        • end: {...}
                          • line: 11
                          • column: 14
                          }
                        }
                      • name: "isNaN"
                      }
                    • arguments: [...] (1)
                      • Identifier {...}
                        • type: "Identifier"
                        • start: 162
                        • end: 163
                        • loc: {...}
                          • start: {...}
                            • line: 11
                            • column: 15
                            }
                          • end: {...}
                            • line: 11
                            • column: 16
                            }
                          }
                        • name: "a"
                        }
                      ]
                    • optional: false
                    }
                  }
                • operator: "&&"
                • right: UnaryExpression {...}
                  • type: "UnaryExpression"
                  • start: 168
                  • end: 177
                  • loc: {...}
                    • start: {...}
                      • line: 11
                      • column: 21
                      }
                    • end: {...}
                      • line: 11
                      • column: 30
                      }
                    }
                  • operator: "!"
                  • prefix: true
                  • argument: CallExpression {...}
                    • type: "CallExpression"
                    • start: 169
                    • end: 177
                    • loc: {...}
                      • start: {...}
                        • line: 11
                        • column: 22
                        }
                      • end: {...}
                        • line: 11
                        • column: 30
                        }
                      }
                    • callee: Identifier {...}
                      • type: "Identifier"
                      • start: 169
                      • end: 174
                      • loc: {...}
                        • start: {...}
                          • line: 11
                          • column: 22
                          }
                        • end: {...}
                          • line: 11
                          • column: 27
                          }
                        }
                      • name: "isNaN"
                      }
                    • arguments: [...] (1)
                      • Identifier {...}
                        • type: "Identifier"
                        • start: 175
                        • end: 176
                        • loc: {...}
                          • start: {...}
                            • line: 11
                            • column: 28
                            }
                          • end: {...}
                            • line: 11
                            • column: 29
                            }
                          }
                        • name: "b"
                        }
                      ]
                    • optional: false
                    }
                  }
                }
              • consequent: BlockStatement {...}
                • type: "BlockStatement"
                • start: 179
                • end: 217
                • loc: {...}
                  • start: {...}
                    • line: 11
                    • column: 32
                    }
                  • end: {...}
                    • line: 14
                    • column: 2
                    }
                  }
                • body: [...] (2)
                  • ExpressionStatement {...}
                    • type: "ExpressionStatement"
                    • start: 183
                    • end: 193
                    • loc: {...}
                      • start: {...}
                        • line: 12
                        • column: 2
                        }
                      • end: {...}
                        • line: 12
                        • column: 12
                        }
                      }
                    • expression: CallExpression {...}
                      • type: "CallExpression"
                      • start: 183
                      • end: 192
                      • loc: {...}
                        • start: {...}
                          • line: 12
                          • column: 2
                          }
                        • end: {...}
                          • line: 12
                          • column: 11
                          }
                        }
                      • callee: Identifier {...}
                        • type: "Identifier"
                        • start: 183
                        • end: 190
                        • loc: {...}
                          • start: {...}
                            • line: 12
                            • column: 2
                            }
                          • end: {...}
                            • line: 12
                            • column: 9
                            }
                          }
                        • name: "calcSum"
                        }
                      • arguments: []
                      • optional: false
                      }
                    }
                  • ExpressionStatement {...}
                    • type: "ExpressionStatement"
                    • start: 196
                    • end: 214
                    • loc: {...}
                      • start: {...}
                        • line: 13
                        • column: 2
                        }
                      • end: {...}
                        • line: 13
                        • column: 20
                        }
                      }
                    • expression: CallExpression {...}
                      • type: "CallExpression"
                      • start: 196
                      • end: 213
                      • loc: {...}
                        • start: {...}
                          • line: 13
                          • column: 2
                          }
                        • end: {...}
                          • line: 13
                          • column: 19
                          }
                        }
                      • callee: Identifier {...}
                        • type: "Identifier"
                        • start: 196
                        • end: 211
                        • loc: {...}
                          • start: {...}
                            • line: 13
                            • column: 2
                            }
                          • end: {...}
                            • line: 13
                            • column: 17
                            }
                          }
                        • name: "sendSumToServer"
                        }
                      • arguments: []
                      • optional: false
                      }
                    }
                  ]
                }
              • alternate: null
              }
            • label: Identifier {...}
              • type: "Identifier"
              • start: 148
              • end: 149
              • loc: {...}
                • start: {...}
                  • line: 11
                  • column: 1
                  }
                • end: {...}
                  • line: 11
                  • column: 2
                  }
                }
              • name: "$"
              }
            }
          ]
        • sourceType: "module"
        }
      • attributes: []
      }
    }
The AST is not public API and may change at any point in time
explicit deps • Playground • Svelte