JavaScript variables Free Web Design Tutorials
Home   HTML CSS JavaScript PHP MySQL Usability Glossary
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
©2009 www.webdesignworkmate.co.uk all rights reserved 
Design and Production by smallbizonline website design © 2000-2009
Valid HTML 4.01! Level Double-A conformance icon, W3C-WAI Web Content Accessibility Guidelines 1.0Valid CSS!
Recommended Reading
Javascript the definitive guide

Javascript and DHTML cookbook

Pro javascript techniques

simply javascript