Help - Search - Members - Calendar
Full Version: The Basics Of Javascript 3
13Dots Forum > Tutorials And Articles > PHP-HTML-CSS
DanWilliamson
This is the third tutorial in the javascript basics written by me and this is where we get quite advanced and will use Functions, Variables, alerts, document writes etc without explaining them so if you don't understand them please read the other two tutorials.

Arrays are used in my opinion so that you don't have hundreds of variables and just a few nice arrays so it's used to hold variables. Here is a quick example of code using variables and then code using arrays.

CODE
<script type="text/javascript">
<!--

    var monday = Monday;
    var tuesday = Tuesday;
    var wednesday = Wednesday;
    var thursday = Thursday;
    var friday = Friday;
    var saturday = Saturday;
    var sunday = Sunday;

  document.write("Todsay is monday");

//-->
</script>


So there we have the variables and the document write, sorry if there are any mistakes as I jotted that up quickly. Here is the array version which is much better.

CODE
<script type="text/javascript">
<!--

    var days = new Array("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday");

  document.write("The day is " + days[5] + "!");

//-->
</script>


This will output 'The day is Saturday!' and can be used differently or whatever but the basic rundow is that you define a variable then the equal operator and type new Array(""); I used days of the week so we didn't go too advanced but heres another way to create arrays and this is the way that I first ever learned.

CODE
<script type="text/javascript">
<!--

    var days = new Array(3);
        days[0] = "Monday";
    days[1] = "Tuesday";
    days[2] = "Wednesday";

//-->
</script>


Although the code is longer it is more easily set out for the beginner to arrays, also you must note that new Array(3); was used as you have [0][1][2] which are the (3) if that made any sense to yeah, anyway I haven't added all the days of the week as that would be really long and I don't have the typing energy. We haven't used arrays for much yet but here is something we could do with the if/else condition but I will save that for another day.

Hopefully I will write a few more javascript tutorials for 13Dots as it's been great victory.gif
RedDragon
Somebody is on fire!

Thanks for Sharing Dan smile.gif
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2010 Invision Power Services, Inc.