Basics
Creating a Document
Head and Body Elements
Formatting Text
Creating Lists
Embedding Images
URLs Explained
Linking to Documents
Creating Tables
Forms Form Basics
Input and Textarea
Select Lists
Frames Framesets
The Frame Element
Nested Framesets
Targetting Frames
Inline Frames
Tips and Tricks Meta Tags
Transition Effects
HTML Generators Create a Document
Create a List
Create a Table
HTML
Basics
Implementing CSS
CSS Syntax
Pseudo Classes/Elements
CSS Classes
CSS Properties Font Properties
Color and Background
Text Properties
Border Properties
Margins and Padding
Size and Position
Tips and Tricks Menu Buttons
Special Effects
CSS
Basics
Running a Script
Variables
Expressions and Operators
Objects.Properties.Methods
The Date Object
Strings
Regular Expressions
Defining RegExp Patterns
Branches and Conditions
Loops
Arrays Array Basics
Array Methods
Sorting Arrays

User-Defined Functions
Cookies
Windows
Frames
Tips and Tricks Image Replacement
Using Includes
Form Validation
Debugging
JavaScript
Basics
Creating a Script
Running a Script
Variables
Expressions and Operators
Strings Strings Basics
Strings and Substrings
Replacing Substrings
Regular Expressions
Branches and Conditions
Loops
Arrays Array Basics
Array Functions
Sorting Arrays
User-Defined Functions
Include and Require
Uploading Files
File Functions
Session Variables
Tips and Tricks Page Templates
Form Reply Scripts
Form Validation
JavaScript to PHP
PHP
Basics
Create and Drop
Show and Describe
Insert, Update and Delete
Querying
Join Queries
Functions
Table Locking
PHP/MySQL Functions Accessing a Database
Querying with PHP
Create and Drop with PHP
Insert and Update with PHP
Frequently Used Functions MySQL
Basics
Layout and Navigation
Page Content Style
Web Page Copy
Graphics and Animation
HTML Forms
Accessibility
Legal Requirements
MySQL
JavaScriptBasicsRunning a ScriptJavaScript VariablesVariablesExpressions and OperatorsObjects.Properties.MethodsThe Date ObjectStringsRegular ExpressionsDefining RegExp PatternsBranches and ConditionsLoopsArraysArray MethodsSorting ArraysUser-Defined FunctionsCookiesWindowsFrames

Home > JavaScript > Variablesprinter version

JavaScript Variables

If you have no programming experience then you may be unfamiliar with the concept of variables. A variable is a value, stored in the computer's memory, which can be varied.

To be able to read a variable's value or alter its value we don't need to know where it's stored in memory - JavaScript takes care of that - we just need to know its name.

Variable Names

There are naming conventions that must be followed. Variable names ...



Variable names are usually chosen to reflect their use and if two words make things clearer then underscores or capitalization is normally used e.g. customer_count or CustomerCount.

Data Types

JavaScript supports three primitive data types and two composite data types.

A variable holding a primitive type only holds one value at a time whereas a variable holding a composite type can hold more than one value (composite data types are covered in a later tutorial).

Privitive Data Types



Using Variables

Unlike some languages, JavaScript is untyped. This means that it's possible to assign a numeric value to a variable then later assign a string.

It also means that the language will automatically convert a value from one type to another, where required.

Variable Declaration

Before a variable can be used in a program it should be declared using the var keyword. Multiple variables may be declared in one statement separated by commas.

Examples of Variable Declarations
<script language="javascript" type="text/javascript">
    var count;
    var firstname, lastname;
</script>


Variable Initialization

A value is assigned to a variable using the = sign and initialization may be combined with declaration.

Until a a value is assigned to a variable then its value is undefinded.

Examples of Variable Initialization
<script language="javascript" type="text/javascript">
    var count;
    var firstname = "Dave";
    var lastname = "Clark";
    count = 10;
</script>


Operating on Variables

It's common to want to add or subtract 1 to or from a variable (increment or decrement) and this can be achieved in different ways.

Examples of Increment and Decrement
<script language="javascript" type="text/javascript">
    var count = 10;
    // Different ways to increment count
    count = count + 1;
    count++;

    // Different ways to decrement count
    count = count - 1;
    count--;
</script>


The way increment and decrement behave depends on whether they appear before or after the operand.

If before then it's known as pre-increment or pre-decrement - if after then it's known as post-increment or post-decrement.



The example below illustrates the difference.

Examples of Post-Increment and Pre-Increment
<script language="javascript" type="text/javascript">
    // Example of post-increment
    var count = 10;
    total = count++;
    // After this count = 11 and total = 10

    // Example of pre-increment
    var count = 10;
    total = ++count;
    // After this count = 11 and total = 11
</script>


String Variables

A string variable consists of zero or more characters
e.g. my_string = "Hello there";.

Strings can be added together (concatenated) using the + character.

Examples of Concatenation
<script language="javascript" type="text/javascript">
    my_string = "The beginning" + " and the end";

    first_name = "Charles";
    second_name = "McDonald";
    age = 40;
    full_name = first_name + " " + second_name;
    about_name = full_name + " is aged " + age;
</script>


Previous - JavaScript Running a Script Previous - Running a Script     Next - Expressions and Operators Next - JavaScript Expressions and Operators


Privacy | Terms | Contact | Links | Sitemap | RSS Feeds RSS and JavaScript Feeds
©2010 www.webdesignworkmate.co.uk all rights reserved 
Design and Production by smallbizonline website design © 2000-2010
Valid HTML 4.01! Level Double-A conformance icon, W3C-WAI Web Content Accessibility Guidelines 1.0Valid CSS!
Tips and TricksImage ReplacementUsing IncludesForm ValidationDebugging
Got any JavaScript Tips?
Send me your tip and if it's suitable I'll put it on the site, credit it to you and add a link back to your site.
Recommended Reading
Javascript the definitive guide

Javascript and DHTML cookbook

Pro javascript techniques

simply javascript