spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / programming / javascript / diaries / 2 To page 1current pageTo page 3To page 4To page 5
[previous][next]

Web Project Manager
Aquent
US-PA-Collegeville

Justtechjobs.com Post A Job | Post A Resume
Developer News
Mandrake Linux Founder Back, Virtually
Amazon: We're a Technology Company
Sun Expands MySQL With Closed Source

The JavaScript Diaries: Part 2

Variables

Variables are a basic technique used for storing data in a script. A variable "holds" a specific value. In HTML coding, elements such as <em></em> and <h2></h2> are also known as "containers." The code affects the data which is "contained" within the elements. So it is with a variable. Anything done to a variable affects the value that is contained within it. Variables make data manipulation easier as the value can be acted upon without having to re-enter the original data each time.

When a variable is first stated in the code, it is said to be "declared," using the var reserved word. When data is given to the variable to be stored, it is "initialized." If the data is later changed, the new value is "assigned."

A variable can be declared and initialized using one of two methods. With the first method, you would declare the variable on one line and then initialize it on another line:

  var myName;
      myName="Lee";

Basically these two lines say that I am declaring a variable named myName and the variable named myName contains the value "Lee."

Using the second method, you would declare the variable and initialize it all on the same line:

  var myName="Lee";

This line says I am declaring a variable named myName and its value is "Lee." It's the same as above only shorter. The benefit becomes more apparent when you are using several variables.

Naming Variables

Variable are case sensitive. myValue and MyValue are two different variables according to JavaScript. The same case must be used throughout the entire script.

There is no real "correct" way to name variables, but there is an "unofficial" method. When naming a variable that is only one word, use lowercase: var auto. If a variable is two or more words, capitalize the first letter of each word, var MyAuto or capitalize all the words except the first: var myFavoriteCar. The method of capitalization is a personal choice but must be consistent!

A variable can only begin with either a letter or underscore character ( _ ). The rest of the variable name may contain letters and numbers, the underscore character ( _ ), and a dollar sign ( $ ). Do not use a reserved word for a variable name.

Be sure and give meaningful names to variables. Later, when you begin writing complex scripts and you have several variables, a meaningful name helps you to remember what the variable is used for. For instance, if you are adding together the cost of accessories for a car, the statement:

  totalPrice=750+250+300

doesn't mean too much just looking at it, but if you use meaningful names for your variables, the statement would then make more sense:

  totalPrice=airCond+cdPlayer+magWheels

Since JavaScript, for the most part, ignores whitespace, you can write the variables either way:

  var TheFirst="This string is stored in a variable.";
  var TheFirst = "This string is stored in a variable.";

home / programming / javascript / diaries / 2 To page 1current pageTo page 3To page 4To page 5
[previous][next]

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Whitepapers and eBooks

Symantec Whitepaper: Converging System and Data Protection for Complete Disaster Recovery
Intel Whitepaper: Comparing Two- and Four-Socket Platforms for Server Virtualization
IBM Solutions Brief: Go Green With IBM System xTM And Intel
HP eBook: Simplifying SQL Server Management
IBM Contest: Are You the Next Superstar? Join the "Search for the XML Superstar" Contest to Find Out
Intel PDF: Quad-Core Impacts More Than the Data Center
Intel PDF: Virtualization Delivers Data Center Efficiency
Go Parallel Article: PDC 2008 in Review
Avaya Article: Communication-Enabled Mashups: Empowering Both Business Owners and IT
Intel Whitepaper: Building a Real-World Model to Assess Virtualization Platforms
PDF: Intel Centrino Duo Processor Technology with Intel Core2 Duo Processor
Microsoft Article: Build and Run Virtual Machines with Hyper-V Server 2008
  Go Parallel Article: Q&A with a TBB Junkie
IBM Whitepaper: Innovative Collaboration to Advance Your Business
Internet.com eBook: Real Life Rails
IBM eBook: The Pros and Cons of Outsourcing
Internet.com eBook: Best Practices for Developing a Web Site
IBM CXO Whitepaper: The 2008 Global CEO Study "The Enterprise of the Future"
Avaya Article: Call Control XML in Action - A CCXML Auto Attendant
IBM CXO Whitepaper: Unlocking the DNA of the Adaptable Workforce--The Global Human Capital Study 2008
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
Symantec Whitepaper: Comprehensive Backup and Recovery of VMware Virtual Infrastructure
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
webref The latest from WebReference.com Browse >
Popular JavaScript Framework Libraries: An Overview - Part 3 · Accessing Your MySQL Database from the Web with PHP · Working with the DOM Stylesheets Collection
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Fixing MySQL Replication · Firewall Guide: First Steps to Securing the Enterprise · VoxOx Tames the Tumultuous Communications Tangle

Created: April 29, 2005

URL: