Status
Not open for further replies.

kyonides

Reforged is laughable
Veteran
Joined
Nov 17, 2019
Messages
1,072
Reaction score
499
First Language
English
Primarily Uses
RMXP
I wanted to ask you guys how the scope of variables work in Javascript to learn how they get affected depending on the place they are declared.

In Ruby I know that a local variable can be used inside any method and need to be passed to another method as an argument.
The same happens in C, C++ and Javascript. No problems here.

Yet, what happens if a let variable is defined inside a conditional statement?
Can we call it after the condition has been executed without getting an error message?

In Ruby that would work fine because it doesn't enforce any specific rule about local variables, but it wouldn't work in C and C++ at all. I'd need to define it outside that statement.

What about Javascript?
 
Last edited:

ATT_Turan

Forewarner of the Black Wind
Veteran
Joined
Jul 2, 2014
Messages
8,754
Reaction score
6,570
First Language
English
Primarily Uses
RMMV
In JavaScript, let has block scope.

Yet, what happens if a let variable is defined inside a conditional statement?
You get a syntax error, you can't define a variable inside a conditional.

If you declare it inside a loop, it will be inaccessible outside of that loop.
Screenshot 2023-02-03 145707.jpg
 

Arthran

Veteran
Veteran
Joined
Jun 25, 2021
Messages
945
Reaction score
1,255
First Language
English
Primarily Uses
RMMZ
If a let variable is declared inside a conditional statement code block, then it can only be referenced from inside that same code block. I actually wrote a pretty detailed explanation about variable scope (with examples) the other day. If you're interested in learning more about it, you can find that post here.

*edit* Oops, I may have misunderstood your question. I thought you meant within a code block that is tied to a conditional statement. Can you give an example of why you would ever benefit from trying to define a variable from within a conditional statement itself?
 
Last edited:

ATT_Turan

Forewarner of the Black Wind
Veteran
Joined
Jul 2, 2014
Messages
8,754
Reaction score
6,570
First Language
English
Primarily Uses
RMMV
Oh, when you said "in a conditional statement" did you mean in the resulting code (block)? My understanding of the terminology is the if (this part here) is the conditional statement.

But, regardless, it's still limited in scope to that code block, as the link and Arthran says, and my screenshot shows :smile:
 

Zeriab

Huggins!
Veteran
Joined
Mar 20, 2012
Messages
1,399
Reaction score
1,612
First Language
English
Primarily Uses
Other
If you want to dive deeper here are links to the for-statement specification (ES6)

How scoping works seems to be here:

For some practical examples try testing var and let.
JavaScript:
// Let
for (let i = 0; i < 4; i++) {
    console.log(i)
}
console.log(i)

// Var
for (var i = 0; i < 4; i++) {
    console.log(i)
}
console.log(i)
 
Last edited:

Shaz

Keeper of the Nuts
Global Mod
Joined
Mar 2, 2012
Messages
45,996
Reaction score
16,806
First Language
English
Primarily Uses
RMMV

This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

 
Status
Not open for further replies.

Latest Threads

Latest Posts

Latest Profile Posts

Me: *sudden burst of motivation to do something amazing*
Me, 5 minutes later: "Yeah, that's not happening"
1iVi6I.png


Continuing our countdown with Capsule Monster #13 Tomey Thomas! I loved the idea of a tiny book gaining sentience and being a little wizard. It’s a shame none of his spells are useful XD
Have to be more organised and serious. It helps that im running out of time at work :|. so much to do. Trying to make game studio professional. www.bmpgames.com
imgur links are back!
now I CAN keep getting away with NOT replacing them with updated renders!
yay!
I forgot RPG Maker even existed. Such good memories. Might boot up steam and download MV one more time

Forum statistics

Threads
131,552
Messages
1,220,794
Members
173,220
Latest member
ZecaVn
Top