April 7, 2005

JavaScript Variable Scoping

Todays JavaScript Gem comes to us from Scott Andrews new blog, The Strange Zen of JavaScript.

There are two ways to declare variables within JavaScript. One is private, meaning that it can be used/availabe only within that function. The other is public, meaning that it can be used outside of that function.

The Global way to declare:

function Foo(){
    bar = 1;
}

The Private way:

function Foo() {
    var bar = 1;
}

See the difference? Variables declared without the var operator are global by default.

Post Info

Tagged As Coding, Javascript, Reference, Tip

Comments are Open (2)

Posted at 11:33 AM

Comments

Keith

Along with this a good habbit to get into is to not use global variables unless you need to return more than one value. The return method is your friend!

imran

Hi

I did like your blog postings and comments but i have soem problem if some1 could help me out.

Does anyone know how to pass html table data from parent to a child form html table ? Or may be you could let me know any URL related to this topic.

Any kind of help is highly appericiated.

Thanks

Imran Hashmi
http://www.visionstudio.co.uk

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)

Search
Comments on this post
Categories
Info
Tony Stephens
Copyright © 1995-2005
Site Version:
10
Licensed:
Creative Commons
Validate:
XHTML, CSS, 508, RSS
Subscribe
Blog-Fu, Link-Fu