Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-21-2010, 08:10 PM   PM User | #1
javanewbie7
Regular Coder

 
Join Date: Oct 2010
Posts: 121
Thanks: 25
Thanked 0 Times in 0 Posts
javanewbie7 is an unknown quantity at this point
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.
javanewbie7 is offline   Reply With Quote
Old 12-21-2010, 08:17 PM   PM User | #2
DJCMBear
Senior Coder

 
DJCMBear's Avatar
 
Join Date: Mar 2010
Location: United Kindom
Posts: 1,173
Thanks: 14
Thanked 136 Times in 136 Posts
DJCMBear is on a distinguished road
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
DJCMBear is offline   Reply With Quote
Old 12-21-2010, 08:17 PM   PM User | #3
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,203
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
??? 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.
Old Pedant is online now   Reply With Quote
Old 12-21-2010, 08:27 PM   PM User | #4
javanewbie7
Regular Coder

 
Join Date: Oct 2010
Posts: 121
Thanks: 25
Thanked 0 Times in 0 Posts
javanewbie7 is an unknown quantity at this point
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.
javanewbie7 is offline   Reply With Quote
Old 12-21-2010, 08:34 PM   PM User | #5
DJCMBear
Senior Coder

 
DJCMBear's Avatar
 
Join Date: Mar 2010
Location: United Kindom
Posts: 1,173
Thanks: 14
Thanked 136 Times in 136 Posts
DJCMBear is on a distinguished road
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
DJCMBear is offline   Reply With Quote
Old 12-21-2010, 08:37 PM   PM User | #6
javanewbie7
Regular Coder

 
Join Date: Oct 2010
Posts: 121
Thanks: 25
Thanked 0 Times in 0 Posts
javanewbie7 is an unknown quantity at this point
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.
javanewbie7 is offline   Reply With Quote
Old 12-21-2010, 08:53 PM   PM User | #7
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,203
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is online now   Reply With Quote
Old 12-21-2010, 08:54 PM   PM User | #8
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,203
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is online now   Reply With Quote
Old 12-21-2010, 09:20 PM   PM User | #9
DJCMBear
Senior Coder

 
DJCMBear's Avatar
 
Join Date: Mar 2010
Location: United Kindom
Posts: 1,173
Thanks: 14
Thanked 136 Times in 136 Posts
DJCMBear is on a distinguished road
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
DJCMBear is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:35 PM.


Advertisement
Log in to turn off these ads.