Jscript examples - to var or not to var?

The DOpus11 help includes several example scripts.
Looking at two of the jscript examples, I'm curious about the usage/not of "var".

In "Simple Script Function", none of the variable/object instantiations use the "var" keyword, which controls (as I understand it) global/local scope (and perhaps other effects too).
In "Adding a new Internal Command", some variables do use var, and some don't.

Is there any rhyme or reason to this? Does it matter at all?

Javascript newbie, obviously...

I am new to JS, too. As i understand this:

If you initialize the variable immediately, you do not have to use 'var'.

throws an exception:

[code]@Script JScript

function OnClick(clickData) {

test;
test = 'Hello';  // <- ERROR
DOpus.Output(test);

}
[/code]

this

[code]@Script JScript

function OnClick(clickData) {

var test;
test = 'Hello';
DOpus.Output(test);

}[/code]
is the same as

[code]@Script JScript

function OnClick(clickData) {

test = 'Hello';
DOpus.Output(test);

}[/code]

[quote="Dinkelhopper"]If you initialize the variable immediately, you do not have to use 'var'.

[code]@Script JScript

function OnClick(clickData) {

test;
test = 'Hello';  // <- ERROR
DOpus.Output(test);

}
[/code]

[code]@Script JScript

function OnClick(clickData) {

var test;
test = 'Hello';
DOpus.Output(test);

}[/code][/quote]
It is recommended that you use var, and not using it is bad practice in my opinion.

Not using var declares the variable in the parent or global scope, not in the current.

function test(){
  var x=12;
  hello=42;
}
test();
DOpus.Output(hello);
DOpus.Output(x);

The above will output 42 for the hello variable, and an error for 'x'.

:thumbsup:

Learning never stops :slight_smile:

@eitheta
Yes, I does matter and you already said why. The var keyword controls local/global scope.

@dinkel
No it's not the same.

If you leave out the "var" keyword in your function and directly assign a value to a new variable, the variable's scope will be global and is accessable from anywhere, not just from within your function. For me it's very questionable to make use of that feature. If you want to create global variables from functions, there's a nicer way, but I guess you didn't had that in mind in the first place .

Simply put: Use "var".

EDIT: Thanks myamor, you won this reply-race. o)

Thanks for the replies.
So I'm trying to figure out if the author(s) of those example scripts intentionally omitted var, because some of those variables need to be global scope. If so, then it's not obvious (to me) why/how.

I'm guessing that the answer is no.
I'm guessing that var is being handled loosely in these scripts because it doesn't matter in this case. Perhaps in script functions, there is no parent scope, so it doesn't make any difference?

Well, accidents happen, but I assume it is the same reason as "always".

He/she initially learned to do it that way (or that it didn't matter), and has to remind him/herself to use it.
In other words, one of the reasons why not using "var" is bad practice (in addition to its effect on the code).

Btw, the global scope is usually a function's parent scope.