473,549 Members | 2,553 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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;
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)?


"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.
(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.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;
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
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: <form runat="server"> <asp:DropDownList AutoPostBack="true" ID="report"...
11
5198
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
2562
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
12431
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 the YourMail.TextBody = equation, but kept getting syntax errors so have since moved my message body to another section. Nonetheless, I'm still...
17
3117
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 that I am using Allen Browne's multi-select list box for a report as a guide. I should also note that my access skills are not the best so I may need...
0
7526
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7457
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7483
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6051
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5092
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3487
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1949
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1063
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
771
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.