Scope question from a newbie

Post Reply
hover27
Newbie
Posts: 14
Joined: Sat Apr 20, 2019 3:49 pm

Scope question from a newbie

Post by hover27 »

Hi,
how can I make the value of a local variable global?
I think in ECMAScript 6 this would work, but not in Switch Scripting (ECMAScript 4).

Code: Select all


var a = 7;
var b = 5;

if (a=7){
job.log(1, b); // 5
var b = 13;
job.log(1, b); // 13
otherstatements;
}
job.log(1, b); // 5


Thank you
Padawan
Advanced member
Posts: 358
Joined: Mon Jun 12, 2017 8:48 pm
Location: Belgium
Contact:

Re: Scope question from a newbie

Post by Padawan »

When you are working with variables, then you have to declare and assign them.

var b;// This is the creation (aka declaration) of the variable. It's location in the script defines in which scope the variable is declared
b=5;// This is the assignment of the variable, this is the point where you put some value in it.

You can do both declaration and assignment in one line, like this:
var b=5;

In your code you first declare b in the beginning of the script and then you initialize it with the value of 5. But then in the function scope you initialize a new b variable and give it the value 13. This new variable b is not available outside the function scope.

The solution is to not declare a new variable b inside the function, but to assign a new value to the existing b variable. You can do so like this:

Code: Select all

var a = 7;
var b = 5;

if (a===7){
	job.log(1, b); // 5
	b = 13;
	job.log(1, b); // 13
}
job.log(1, b); // 13
As you can see I've dropped the var keyword in the function to do this.

You'll also see I've changed your if statement. In your code you use only one equal sign. The correct usage of one equals sign is to assign a new value to a variable. If you want to compare the current value of a variable against something else, then you have to use two or three equals signs.

When you use two equals signs, then you will compare the value, but not the type. This means that a 7 stored as string is equal to a 7 stored as number:
"7" == 7; // This is true
7== 7; // This is true

When you use three equals signs, then you will compare the value and type of the variables. This means that 7 stored as string is not equal to 7 stored as number.
"7" === 7; // This is false
7=== 7; // This is true

In my opinion it is better to use three equal signs instead of two. This way you can be sure that you have a variable with the type you expect and the value you expect.
hover27
Newbie
Posts: 14
Joined: Sat Apr 20, 2019 3:49 pm

Re: Scope question from a newbie

Post by hover27 »

That was exactly, what I was searching for.
Thank you for your very detailed explantation.
bens
Advanced member
Posts: 252
Joined: Thu Mar 03, 2011 10:13 am

Re: Scope question from a newbie

Post by bens »

Image
Padawan
Advanced member
Posts: 358
Joined: Mon Jun 12, 2017 8:48 pm
Location: Belgium
Contact:

Re: Scope question from a newbie

Post by Padawan »

Bens, ihanks! I had to pleasure to have good Jedi Masters :)


(BTW, does that make you Yoda?)
Post Reply