Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9

Thread: Newbie question

  1. #1
    Regular Coder
    Join Date
    Oct 2010
    Posts
    127
    Thanks
    26
    Thanked 0 Times in 0 Posts

    Newbie question

    Hello,

    I'm in the process of getting a better understanding of javascript (instead of just copying and pasting JQuery script onto my site).

    So I have a(dumb?) question. Can I name a var what ever I want? For example,

    var car_name("Ford Escape");


    Or am I misunderstanding this?

    THanks.

  • #2
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    No you can only var letters, numbers and some special chars such as _ (underscore), but to do what you want you can do this. var car_name = ['Ford Escape']; and then you can do this alert(car_name[0]); // alerts Ford Escape
    Official BinPress hand picked coder.
    For anyone worried about SQL injection go have a look at my small yet powerful script here.
    Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
    I am 1 of 65,608 people to get a Pebble Watch :P

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,965
    Thanks
    79
    Thanked 4,429 Times in 4,394 Posts
    ??? car_name is a fine variable name.

    But what's the bit with "Ford Escape"???

    That's not legal syntax.

    Anyway, no. Variables must consist of only letters, digits, underline, and dollar sign. They can start with any of those characters except a digit.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    Regular Coder
    Join Date
    Oct 2010
    Posts
    127
    Thanks
    26
    Thanked 0 Times in 0 Posts
    OK, I guess this is the example I was thinking of (I got it off of W3Schools) and it's also why I'm confused. SO i use an Array to list the vehicles (or whatever I want to list), correct?


    <script type="text/javascript">
    var x;
    var mycars = new Array();
    mycars[0] = "Saab";
    mycars[1] = "Volvo";
    mycars[2] = "BMW";

    for (x in mycars)
    {
    document.write(mycars[x] + "<br />");
    }
    </script>

    Thanks.

  • #5
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    Most codes these days use the object array to store the array data for example like this.

    Code:
    <script type="text/javascript">
    var x,
        mycars = ["Saab","Volvo","BMW"];
    for (x in mycars) {
      document.write(mycars[x] + "<br />"); // x equals the current array item number
    }
    </script>
    Official BinPress hand picked coder.
    For anyone worried about SQL injection go have a look at my small yet powerful script here.
    Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
    I am 1 of 65,608 people to get a Pebble Watch :P

  • #6
    Regular Coder
    Join Date
    Oct 2010
    Posts
    127
    Thanks
    26
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by DJCMBear View Post
    Most codes these days use the object array to store the array data for example like this.

    Code:
    <script type="text/javascript">
    var x,
        mycars = ["Saab","Volvo","BMW"];
    for (x in mycars) {
      document.write(mycars[x] + "<br />"); // x equals the current array item number
    }
    </script>
    Thanks. That helps.

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,965
    Thanks
    79
    Thanked 4,429 Times in 4,394 Posts
    Yes, excepting that's a more complicated way to create an array than is necessary.

    Try this:
    Code:
    <script type="text/javascript">
    var x;
    var mycars = ["Saab","Volvo","BMW"];
    
    for (x in mycars)
    {
        document.write(mycars[x] + "<br />");
    }
    document.write( "<hr>An easy way to dump the array: " + mycars + "<hr>" );
    document.write( "Or another way:<ul><li>" + mycars.join("</li><li>") + "</li></ul>" );
    </script>
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,965
    Thanks
    79
    Thanked 4,429 Times in 4,394 Posts
    Hah. Bear beat me to it. But look at the other ways to dump arrays. Lots of fun to be had by all.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #9
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    Quote Originally Posted by Old Pedant View Post
    Hah. Bear beat me to it.
    Always funny seeing what you have just posted is the same as someone else's, explaining more or less the same thing.
    Official BinPress hand picked coder.
    For anyone worried about SQL injection go have a look at my small yet powerful script here.
    Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
    I am 1 of 65,608 people to get a Pebble Watch :P


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •