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 10-09-2012, 07:47 PM   PM User | #1
aaronoafc
New Coder

 
Join Date: Feb 2012
Posts: 75
Thanks: 8
Thanked 0 Times in 0 Posts
aaronoafc is an unknown quantity at this point
Very simple question, splitting variable

Hi,

I'm just looking over some JavaScript tutorials which im running quick tests in firebug within firefox.

When I type in
Code:
var phrase = "This is your phrase";

var words = phrase.split(" ");
and select run in firebug shouldn't the variable phrase be split up into an Array? or am I missing something here? Because at the minute I just get this:

>>> var phrase = "This is your phrase"; var words = phrase.split (" ");

undefined

Thanks
__________________
Don't Click Here!
aaronoafc is offline   Reply With Quote
Old 10-09-2012, 08:07 PM   PM User | #2
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,765
Thanks: 29
Thanked 453 Times in 447 Posts
jmrker will become famous soon enough
Lightbulb

A simple check of the code shows what you expect from your snippet of code is correct.
Code:
<script type="text/javascript">
var phrase = "This is your phrase";

var words = phrase.split(" ");
alert(phrase+'\n'+words.join(','));
</script>
And there are no errors reported in the error console.

Last edited by jmrker; 10-09-2012 at 09:49 PM..
jmrker is offline   Reply With Quote
Old 10-09-2012, 09:31 PM   PM User | #3
slickuser
New to the CF scene

 
Join Date: Oct 2012
Posts: 9
Thanks: 0
Thanked 4 Times in 4 Posts
slickuser is an unknown quantity at this point
Seeing undefined is correct.

What you did was store the array in the variable 'words'. You did not return the array for viewing.

If you did not want to store the array and just view it, then you just need to: phrase.split(' '); If you do want to store the array and then view it in firebug, after your line that creates the array, just type "words" and hit enter.
slickuser is offline   Reply With Quote
Users who have thanked slickuser for this post:
aaronoafc (10-10-2012)
Old 10-09-2012, 09:46 PM   PM User | #4
DanInMa
Senior Coder

 
DanInMa's Avatar
 
Join Date: Nov 2010
Location: Salem,Ma
Posts: 1,307
Thanks: 12
Thanked 204 Times in 204 Posts
DanInMa is on a distinguished road
^ --- better answer
__________________
- Firebug is a web developers best friend! - Learn it, Love it, use it!
- Validate your code! - JQ/JS troubleshooting
- Using jQuery with Other Libraries - Jslint for Jquery/other JS library users
DanInMa 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 01:21 AM.


Advertisement
Log in to turn off these ads.