473,324 Members | 2,548 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,324 software developers and data experts.

Serious question about Variables

Hi, can someone please explain variables and what I am doing wrong
here?

<script>

x=1;

alert(x);

</script>

Oct 29 '07 #1
6 1194
On Oct 29, 2:58 pm, 182...@rock.com wrote:
Hi, can someone please explain variables and what I am doing wrong
here?

<script>
The type attribute is required:

<script type="text/javascript">

x=1;
When declaring variables, you should always use var. In a global
context, it makes it obvious what the scope of the variable is
intended to be. In a function context, it stops the variable from
becoming global (and makes it obvious what the scope should be).

Note that there is no block scope in javascript, and that functions
can access variables in their containing context.

alert(x);
Shows the value of x, which is 1. Since alert() is not a built-in
javascript method, strictly speaking you should test the method first
however if your code is for a web page, it is extremely unlikely that
it isn't supported:

if (typeof window.alert == 'function') {
window.alert(x);
}

but I doubt you will find anyone insisting that you do that.
</script>

--
Rob

Oct 29 '07 #2
18****@rock.com wrote:
Hi, can someone please explain variables and what I am doing wrong
here?

<script>

x=1;

alert(x);

</script>

Please read: http://catb.org/~esr/faqs/smart-questions.html, especially 'be
explicit about your question' and 'when asking about code'

Try posting a complete example (or a link to a page which exhibits your
problem). Explain what you expect the code to do and what it actually does
on your machine.

Without that information anyone who tries to help you just has to guess at
your problem. Your script does exactly what I expect it to do, so either
you have different expectations, or you had something else on your page
interfering with the script.
Oct 29 '07 #3
182...@rock.com wrote:
Hi, can someone please explain variables and what I am doing
wrong here?
Nothing.
<script>
x=1;
alert(x);
</script>
--
Bart

Oct 29 '07 #4
On Oct 29, 5:11 am, RobG <rg...@iinet.net.auwrote:
On Oct 29, 2:58 pm, 182...@rock.com wrote:
Hi, can someone please explain variables and what I am doing wrong
here?
<script>

The type attribute is required:

<script type="text/javascript">
x=1;

When declaring variables, you should always use var. In a global
context, it makes it obvious what the scope of the variable is
intended to be. In a function context, it stops the variable from
becoming global (and makes it obvious what the scope should be).

Note that there is no block scope in javascript, and that functions
can access variables in their containing context.
alert(x);

Shows the value of x, which is 1. Since alert() is not a built-in
javascript method, strictly speaking you should test the method first
however if your code is for a web page, it is extremely unlikely that
it isn't supported:

if (typeof window.alert == 'function') {
window.alert(x);
}
That won't work in IE, which returns "object" for typeof(this.alert.)

You would be better off with:

if (this.alert) {
....
}

Or if you want to be overly cautious:

if (typeof this.alert == 'function' || typeof this.alert == 'object')
{
....
}
>
but I doubt you will find anyone insisting that you do that.
It makes sense to feature test host objects. You have no guarantee
they will exist. The second test doesn't even guarantee that alert is
callable, but it is the best you can do with host objects.

Oct 29 '07 #5
18****@rock.com wrote:
Hi, can someone please explain variables and what I am doing wrong
here?
Well, depending upon what you expect, my guess would be that you put the
script somewhere where it does nothing.

I created a "page" with nothing but:

<H1>Script</H1>
<script>
x=1;
alert(x);
</script>

… then loaded it in my browser. I got an alert box with the message "1"
inside it. Is this what you expected?

--
Steve Swift
http://www.swiftys.org.uk/swifty.html
http://www.ringers.org.uk
Oct 29 '07 #6
In comp.lang.javascript message <11**********************@i13g2000prf.go
oglegroups.com>, Mon, 29 Oct 2007 02:11:30, RobG <rg***@iinet.net.au>
posted:
>
When declaring variables, you should always use var. In a global
context, it makes it obvious what the scope of the variable is
intended to be. In a function context, it stops the variable from
becoming global (and makes it obvious what the scope should be).
That "always" seems a bit too strong. I know of one seemingly-good
reason for declaring a variable, without 'var' but with initialisation,
within a function.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <URL:http://www.merlyn.demon.co.uk/- FAQish topics, acronyms, & links.
Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
Do not Mail News to me. Before a reply, quote with ">" or "" (SonOfRFC1036)
Oct 29 '07 #7

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

Similar topics

28
by: David MacQuigg | last post by:
I'm concerned that with all the focus on obj$func binding, &closures, and other not-so-pretty details of Prothon, that we are missing what is really good - the simplification of classes. There are...
12
by: frank | last post by:
Please help.....! I have built a site that works/worked absolutely fine on my test server. When I transferred it to a remote web host (the intended permanent home of the site) something very...
142
by: Herr Lucifer | last post by:
As the founder of .NET framework, Microsoft claims that it invention will be the next best platform for programming in a near future. Now it is 2005, ..NET is 5 years old, and can talk and walk for...
10
by: BBFrost | last post by:
We just recently moved one of our major c# apps from VS Net 2002 to VS Net 2003. At first things were looking ok, now problems are starting to appear. So far ... (1) ...
5
by: Colonel Kernel | last post by:
I've found what seems to be a very serious (although obscure) bug in VC++.NET 2003 when using Loki's class hierarchy-generation facilities. The compiler seems to generate an invalid v-table for my...
2
by: WJ | last post by:
This post is a follow up from the original post dated Oct 16, 2004 "I have this problem, pls help!" created by Paul FI. These bugs are rather serious and we would like to know how to get around. ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.