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