SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot
    Join Date
    Sep 2010
    Posts
    111
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Combining as a String

    I want to be able to combine numbers as strings.

    Code:
    1 & 2 = 12
    or

    Code:
    if (min<10){
         min = 0 & min
    }
    The "&" sign does not work. Any suggestions?

  2. #2
    SitePoint Guru Jason__C's Avatar
    Join Date
    Oct 2009
    Location
    Racoon City
    Posts
    658
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    You need to use the "+" instead of the "&" sign.
    Chuck Norris is so tough,
    mosquitos ask for permission before they bite him

  3. #3
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,527
    Mentioned
    84 Post(s)
    Tagged
    4 Thread(s)
    You can use the String object, or the toString method to work with them as strings, or just use an actual string.

    Code:
    var a = 1,
        b = 2,
        c = String(a) + String(b);
    Code:
    var a = 1,
        b = 2,
        c = a.toString() + b.toString();
    Code:
    if (min < 10) {
         min = '0' + min;
    }
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

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