...

View Full Version : Very simple question, splitting variable



aaronoafc
10-09-2012, 07:47 PM
Hi,

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

When I type in
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

jmrker
10-09-2012, 08:07 PM
A simple check of the code shows what you expect from your snippet of code is correct.


<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.

slickuser
10-09-2012, 09:31 PM
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.

DanInMa
10-09-2012, 09:46 PM
^ --- better answer



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum