Roll20 uses cookies to improve your experience on our site. Cookies enable you to enjoy certain features, social sharing functionality, and tailor message and display ads to your interests on our site and others. They also help us understand how our site is being used. By continuing to use our site, you consent to our use of cookies. Update your cookie preferences .
×
Create a free account

FASERIP API - Anyone wanna help me understand and expand this script?

Forgot about the var, let, const question.   const = a constantly defined variable that cannot be changed or updated.  Is this global or just within its block {} scope? var = a variable that can be changed, updated and re-declared at a global level. let = a variable that can only be declared once within its block {} scope, but can be updated with said block {} scope. correct?
1656809485

Edited 1656809664
GiGs
Pro
Sheet Author
API Scripter
rcbricker said: Forgot about the var, let, const question.   const = a constantly defined variable that cannot be changed or updated.  Is this global or just within its block {} scope? var = a variable that can be changed, updated and re-declared at a global level. let = a variable that can only be declared once within its block {} scope, but can be updated with said block {} scope. correct? const and let are both within the block scope, whereas var is hoisted to the start of the function. Note: this only applies to initialisations, not declarations. If you do var x; that will be hosited, but if you do var x = 7; that will not be hoisted. The first is an initialisation , the latter is a declaration . For const and let, initialisations and declarations are both treated the same way - as declarations. Note that you can declare let multiple times, but each becomes a separation instance. You can do this: let x = 7; if(x===7) {     let x = 5; x = x +4; } console.info({x}); The console output will show x = 7. If the x created inside the if block is a completely separate variable from the x created at the start of the code.
1657119806
timmaugh
Pro
API Scripter
GiGs said: const and let are both within the block scope, whereas var is hoisted to the start of the function. Note: this only applies to initialisations, not declarations. If you do var x; that will be hosited, but if you do var x = 7; that will not be hoisted. The first is an initialisation , the latter is a declaration . I think you have those backwards, GiGs... the declaration is introducing the new thing ( var x , your first example). Initialization (or, initialisation, if you simply must) would be to give it its first value ( var x = 7 ). To put a fine point on it, though, the second example is both declaring and initializing the variable. The declaration is hoisted, but the initialized value is not: console.log(x); // undefined var x = "hoocha hoocha hoocha, lobster"; console.log(x); // "hoocha, hoocha, hoocha, lobster" Compare that to the same three statements, but replacing var with let. You'll get a "can't reference 'x' before initialization" error.
1657145336

Edited 1657145378
GiGs
Pro
Sheet Author
API Scripter
Well spotted, I used the initialisation and declaration times in the wrong order. Declaration: you declare that this variable exists. Initialisation: you give it a value (initialise it).