473,549 Members | 2,553 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 940
Ed Jay wrote on 19 apr 2006 in comp.lang.javas cript:
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)?

"appropriat e" 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.javas cript 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.javas cript:
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

This thread has been closed and replies have been disabled. Please start a new discussion.

### Similar topics

 699 33350 by: mike420 | last post by: I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro capabilities, unfortunately. I'd like to know if it may be possible to add a powerful macro system to Python, while keeping its amazing syntax, and if it... 99 4561 by: Paul McGuire | last post by: There are a number of messages on the python-dev mail list that indicate that Guido is looking for some concensus to come from this list as to what *one* alternative syntax for decorators we would like him to consider in place of the @ syntax that is currently in 2.4a2. I think special thanks are due to: - Anthony Baxter for his continuing... 4 17203 by: Dave | last post by: Hi, Can anyone tell me the correct syntax when using a variable to control the color property? This works ok: If ABC2flash Then Label61.BackColor = Color.Yellow This doesn't, showing syntax error in source code: Dim ABC1col As String = "Color.Yellow" 177 6889 by: C# Learner | last post by: Why is C syntax so uneasy on the eye? In its day, was it _really_ designed by snobby programmers to scare away potential "n00bs"? If so, and after 50+ years of programming research, why are programming languages still being designed with C's syntax? These questions drive me insane. Every waking minute... 0 1364 by: Travis Oliphant | last post by: This post is to gather feedback from the wider community on PEP 357. It is nearing the acceptance stage and has previously been discussed on python-dev. This is a chance for the wider Python community to comment on the proposal. You will find the PEP attached PEP: 357 12 1684 by: Brad Baker | last post by: I am trying to write a simple ASP.net/C# page which allows users to select some values and produce a report based on a SQL query. I have a self posting dropdown form which allows users to select the type of report to generate: Select the type of report to display: