Miscellaneous

  Home arrow Miscellaneous arrow Page 2 - Introduction to Java Script
MISCELLANEOUS

Introduction to Java Script
By: Codewalkers
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 3
    2004-02-08

    Table of Contents:
  • Introduction to Java Script
  • Variables
  • Prompts
  • If -Then Statements

  •  
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement

    Introduction to Java Script - Variables


    (Page 2 of 4 )

    The first thing that we need to understand about anything dealing with programming is the concept of variables. If you have taken algebra, then you know that variables are. If you haven’t, then you are about to learn. A variable is just some letter or word that has a variable meaning. In other words, the value of that letter or word can change. Variables can be anything, as long as the first character is alphabetic. A, R, dog, can, forget_me_not, and y2, are all variables. Notice that when we use spaces, we must insert an underscore instead of just a space. Variables are always numeric. We can either assign them a specific value, or we can leave them empty, to be filled in by input from a user. If we were to declare a variable, in this case X, as equal to 3, in the formula x+y=5, then y would equal 2.

    Lets say that we wanted to write a script using variables that would do some calculations for us. In this case we will figure how many seconds are in a number of hours. The first thing we need to do is set our variables and name them.

    <Script Language="JavaScript">
    <!—Hide me from the dumb browsers

    /* here is my variable declaration section */

    var sec_per_min=60;
    var min_per_hour=60;
    var hours_per_day=24;
    var days_per_year=365;

    You should notice that each variable begins with the var statement and then a space. It is followed by our variable name. The names that you assign variables should be easy to understand and explain something about the variable. If we had chosen to call sec_per_min X or Y, we could easily misunderstand what we were trying to set. Also remember that all statements in JavaScript end with a semi colon. This helps to separate things out, and your Scripts will not work correctly if you do not have them.

    Ok, now that we have some variables set, we need to do something with them. In this case we will simply do some basic multiplication.

    /* here is my formula section for my variables */

    var sec_per_day=sec_per_min*min_per_hour*hours_per_day;
    var sec_per_year=sec_per_day*days_per_year;

    We have just constructed a complex set of variables to handle some basic multiplication in our page. We have used the value of variables to set the value of other variables! Now we need to do something with this. So far all we have done is set these variables, but we will not get any output without telling JavaScript to do that. Here’s how.

    /* here is how I can write some output to my HTML page */

    document.writeln("<b>There are exactly");
    document.writeln(sec_per_year);
    document.writeln("seconds in one standard year. </b>");

    // stop hiding me -->
    </script>

    With this in our HTML document we will get a single line of text that reads:

    There are exactly 31536000 seconds in one standard year.

    You should notice that we have to put our text formatting options within the quotes of our writeln statements. Now you may ask about using text instead of numeric values in a variable. This is quite simple. To do this we would use a String.

    Stings are just like variables, except that they hold "strings" of letters. These strings may be one letter, or entire pages of text. It doesn’t matter. What you need to understand is the difference between strings and variables. Stings are still declared with the var statement. Below is an example of a string:

    var good_day="I am having an absolutely wonderful day learning JavaScript!";

    More Miscellaneous Articles
    More By Codewalkers

    blog comments powered by Disqus

    MISCELLANEOUS ARTICLES

    - Oracle Database XE: Indexes and Sequences
    - Modifying Tables in Oracle Database XE
    - Oracle Database XE: Tables and Constraints
    - More on Oracle Databases and Datatypes
    - Oracle Database XE Datatypes: Datetime and L...
    - Oracle Database XE Datatypes: Character and ...
    - From Databases to Datatypes
    - Firefox 3.6.6 Released with Improved Plug-in...
    - Attention Bloggers: WordPress 3.0 Now Releas...
    - Reflection in PHP 5
    - Inheritance and Other Advanced OOP Features
    - Advanced OOP Features
    - Linux from Scratch V.6.6 Review
    - Linux Gaining in Strength
    - Install Slackware on Your Old PC


    © 2003-2012 by Developer Shed. All rights reserved. DS Cluster 6 - Follow our Sitemap