1
182719
Hi, can someone please explain variables and what I am doing wrong
here?
<script>
x=1;
alert(x);
</script>
here?
<script>
x=1;
alert(x);
</script>
Hi, can someone please explain variables and what I am doing wrong
here?
<script>
x=1;
alert(x);
[email protected] said:Hi, can someone please explain variables and what I am doing
wrong here?
Nothing.
<script>
x=1;
alert(x);
</script>
The type attribute is required:
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.
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.
RobG said:x=1;
When declaring variables, you should always use var. [...]
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.
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).
Hi, can someone please explain variables and what I am doing wrong
here?
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.