471,832 Members | 1,554 Online

Syntax questions

Is it important to declare variables, i.e., var x = yada, or is it
perfectly fine to use x = yada; without the 'var?'

Is this syntax appropriate: if (x==y==2)?
--
Ed Jay (remove 'M' to respond by email)
Apr 19 '06 #1
6 909
Ed Jay wrote on 19 apr 2006 in comp.lang.javascript:
Is it important to declare variables, i.e., var x = yada, or is it
perfectly fine to use x = yada; without the 'var?'
The var ensures its scope.

try this:

<script type="text/javascript">

var a = 7;

function x(z){
var a=z;
}

function y(z){
a=z;
}

x(9) // 9
y(11) // 11
</script>

Is this syntax appropriate: if (x==y==2)?

"appropriate" to what?

Is it possible? Yes

x==y==2 being the same as x==(y==2)

returns true if y=2 and x=true
and true if y!=2 and x=false

--
Evertjan.
The Netherlands.
Apr 19 '06 #2
Ed Jay said the following on 4/19/2006 3:57 PM:
Is it important to declare variables, i.e., var x = yada, or is it
perfectly fine to use x = yada; without the 'var?'
It is fine, what changes is it's scope when you define it with and
without var in a function.
Is this syntax appropriate: if (x==y==2)?

It depends on what you are trying to do with that statement.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Apr 19 '06 #3
Evertjan. scribed:
Ed Jay wrote on 19 apr 2006 in comp.lang.javascript:
Is it important to declare variables, i.e., var x = yada, or is it
perfectly fine to use x = yada; without the 'var?'

The var ensures its scope.

try this:

<script type="text/javascript">

var a = 7;

function x(z){
var a=z;
}

function y(z){
a=z;
}

x(9) // 9
y(11) // 11
</script>

Is this syntax appropriate: if (x==y==2)?

"appropriate" to what?

Is it possible? Yes

x==y==2 being the same as x==(y==2)

returns true if y=2 and x=true
and true if y!=2 and x=false

Thanks.

--
Ed Jay (remove 'M' to respond by email)
Apr 19 '06 #4
Randy Webb scribed:
Ed Jay said the following on 4/19/2006 3:57 PM:
Is it important to declare variables, i.e., var x = yada, or is it
perfectly fine to use x = yada; without the 'var?'
It is fine, what changes is it's scope when you define it with and
without var in a function.

I'm seeking global scope, so it appears I'm OK. Thanks.
Is this syntax appropriate: if (x==y==2)?

It depends on what you are trying to do with that statement.

Not what was described as the outcome by Evertjan. :-) I was wondering if
something like if (x==2&&y==2&&z==2) {...} could be repalced with
if (x==y==z==2), but apparently not.

Thanks for the input, folks. I appreciate your taking the time.
--
Ed Jay (remove 'M' to respond by email)
Apr 19 '06 #5
On 19/04/2006 21:50, Ed Jay wrote:

[Creating variables without a var statement]
I'm seeking global scope, so it appears I'm OK. Thanks.

I would still not recommend it. I certainly feel that it helps make code
clearer; globals are announced, rather than implied by an assignment
with no corresponding variable declaration. More importantly, perhaps,
explicit declarations prevent name collisions in MSIE when an element
has an id attribute value that matches a global identifier. Without the
declaration, code will cause a fatal error.

[snip]

Mike

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
Apr 19 '06 #6
Michael Winter wrote:
On 19/04/2006 21:50, Ed Jay wrote:
[Creating variables without a var statement]

This summary is misleading, see below.
I'm seeking global scope, so it appears I'm OK. Thanks.

I would still not recommend it. I certainly feel that it helps make code
clearer; globals are announced, rather than implied by an assignment
with no corresponding variable declaration. More importantly, perhaps,
explicit declarations prevent name collisions in MSIE when an element
has an id attribute value that matches a global identifier. Without the
declaration, code will cause a fatal error.

It is simply a completely different animal.

If something is assigned to an unqualified identifier that was not declared
before, scope chain resolution will create a _property_ of the Global
Object (unless, as you pointed out, there is a host object in the scope
chain before having this property, and introducing a problem), and _not_
a variable (therefore, you can apply `delete' on it, provided that the
owner is the Global Object).

If instead something is assigned to a declared identifier, scope chain
resolution will always target (a property of) the Variable Object of the
execution context the identifier was declared in (which is specified and
implemented to be a native object), and therefore a variable (that cannot
be deleted).
PointedEars
Apr 20 '06 #7