469,306 Members | 2,376 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,306 developers. It's quick & easy.

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 885
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;
alert(a);
}

function y(z){
a=z;
alert(a);
}

x(9) // 9
alert(a) // 7
y(11) // 11
alert(a) // 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.
(Please change the x'es to dots in my emailaddress)
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;
alert(a);
}

function y(z){
a=z;
alert(a);
}

x(9) // 9
alert(a) // 7
y(11) // 11
alert(a) // 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

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

699 posts views Thread by mike420 | last post: by
99 posts views Thread by Paul McGuire | last post: by
4 posts views Thread by Dave | last post: by
177 posts views Thread by C# Learner | last post: by
12 posts views Thread by Brad Baker | last post: by
11 posts views Thread by deppy_3 | last post: by
20 posts views Thread by W Karas | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.