473,396 Members | 2,039 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 938
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

699
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...
99
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...
4
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...
177
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...
0
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...
12
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...
11
by: deppy_3 | last post by:
Hi! The syntax of fputs() is similar with the syntax of fgets(); For example if we have:fgets(str,maxlen,stdin) which is the syntax of the fputs();
20
by: W Karas | last post by:
Would the fear factor for concepts be slightly reduced if, instead of: concept C<typename T> { typename T::S; int T::mem(); int nonmem(); };
7
by: siocodes | last post by:
I know I am so close on this, but I cannot see what the syntax fix is. I am sending two e-mails via one asp page; one to the gift giver and one to the recipient...I originally had the message text in...
17
by: trose178 | last post by:
Good day all, I am working on a multi-select list box for a standard question checklist database and I am running into a syntax error in the code that I cannot seem to correct. I will also note...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.