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 4 of 4
  1. #1
    New Coder
    Join Date
    Feb 2012
    Posts
    85
    Thanks
    8
    Thanked 0 Times in 0 Posts

    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!

  • #2
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,146
    Thanks
    39
    Thanked 505 Times in 499 Posts

    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 10:49 PM.

  • #3
    New to the CF scene
    Join Date
    Oct 2012
    Posts
    9
    Thanks
    0
    Thanked 4 Times in 4 Posts
    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.

  • Users who have thanked slickuser for this post:

    aaronoafc (10-10-2012)

  • #4
    Senior Coder DanInMa's Avatar
    Join Date
    Nov 2010
    Location
    Salem,Ma
    Posts
    1,577
    Thanks
    13
    Thanked 248 Times in 248 Posts
    ^ --- better answer


  •  

    Posting Permissions

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