{"id":553,"hash":"0da581f05ff61691f301f30304b6e5d27988c099df8760c5e24e5014c30a136e","pattern":"Cannot access &#39;variable_name&#39; before initialization","full_message":"When using reactive variables by declaring them using the $: syntax, you get the following error.\n\nCannot access 'variable_name' before initialization\n\nHere is the code:\n\nApp.svelte\n\n<script>\n    import { ledzep, redhotchilis } from './data.js'\n    \n    $: bandmembers = [...ledzep, ...redhotchilis]\n    \n    let namesWithA = bandmembers.filter(d => {\n            if (d.indexOf('a') > 0) {               \n                return true;\n            }\n        else {\n            return false\n        }\n    })\n    \n</script>\n<h2>Band Members</h2>\n<ul>\n{#each bandmembers as member}\n    <li>{member}</li>\n{/each}\n</ul>\n\n<h2>Members with \"A\" in their names</h2>\n<ul>\n{#each namesWithA as member}\n    <li>{member}</li>\n{/each}\n</ul>\n\ndata.js\n\nexport const ledzep = [\"Jimmy Page\", \"John Bonham\", \"Robert Plant\", \"John Paul Jones\"]\nexport const redhotchilis = [\"Anthony Kiedis\", \"Flea\", \"Chad Smith\", \"Josh Klinghoffer\"]","ecosystem":"npm","package_name":"svelte","package_version":null,"solution":"When you assign variables using $: you cannot assign them as part of other variables declared using let, const, or var.\n\nWhen you do assignments using $:, you can only use them in other variables assigned using $:. In the code posted above, you need to change the following lines:\n\nlet namesWithA = bandmembers.filter(d => {\n    if (d.indexOf('a') > 0) {               \n        return true;\n    }\n    else {\n        return false\n    }\n})\n\nto the following:\n\n$: namesWithA = bandmembers.filter(d => {\n    if (d.indexOf('a') > 0) {               \n        return true;\n    }\n    else {\n        return false\n    }\n})\n\nIt took me a while to figure this out and just wanted to share this with any other Svelte newbies that are starting out in this new technology.","confidence":0.95,"source":"stackoverflow","source_url":"https://stackoverflow.com/questions/56318460/cannot-access-variable-name-before-initialization","votes":49,"created_at":"2026-04-19T04:51:20.925556+00:00","updated_at":"2026-04-19T04:51:20.925556+00:00"}