Grab Facebook Graph share count and abbreviate it I have this code that requests the graph API from Facebook to respond with a shared count for a particular URL (could be multiple on the page). Graph returns a full number (135934) and I take that number and abbreviate it into more friendly numbers (135K). Basically I'm looking for any help on improving the code to be more concise or if there's a better way to do something then to learn how. 

  &lt;div&gt;
      &lt;section data-url="url-to-this-article"&gt;
          &lt;h3&gt; Some Article &lt;/h3&gt;
         &lt;div class="box-counter"&gt;&lt;/div&gt; 
      &lt;/section&gt;
      &lt;section data-url="url-to-this-article2"&gt;
          &lt;h3&gt; Some Article 2&lt;/h3&gt;
         &lt;div class="box-counter"&gt;&lt;/div&gt; 
      &lt;/section&gt;
      &lt;section data-url="url-to-this-article3"&gt;
          &lt;h3&gt; Some Article 3&lt;/h3&gt;
         &lt;div class="box-counter"&gt;&lt;/div&gt; 
      &lt;/section&gt;
  &lt;/div&gt;

&lt;script&gt;
    jQuery(function(){
            var theShares=[],
            theNewShares=[];
            var numb;
            if(typeof jQuery==='undefined'){
                console.log('need jQuery');
            }else{
                var c = document.getElementsByClassName('box-counter');
                numb = c.length;
                for (var i = 0; i &lt; numb; i++) {
                    var tempURL = c[i].parentNode.attributes['data-url'].nodeValue;
                    jQuery.getJSON('http://graph.facebook.com/'+tempURL,function(data) {
                        theShares.push(data.shares);
                        insertDuring();
                    });
                }
            }
            function insertDuring(){
                if(numb == theShares.length){
                    var c = document.getElementsByClassName('box-counter');
                    for (var i = 0; i &lt; numb; i++) {
                        var myString = theShares[i].toString();
                        if(theShares[i] &gt;= 1000){
                            if(theShares[i] &gt;= 10000){
                                if(theShares[i] &gt;= 100000){
                                    if(theShares[i] &gt;= 1000000){
                                        var diff = myString.length - 6;

                                        abridgeNumbers(myString,diff,"m",i,false);
                                    }else{
                                        abridgeNumbers(myString,3,"k",i,false);                                 
                                    }
                                }else{
                                    abridgeNumbers(myString,2,"k",i,false);
                                }
                            }
                            else{
                                abridgeNumbers(myString,2,"k",i,true);                          
                            }
                        }else{
                            theNewShares[i] = theShares[i];
                        }
                        c[i].innerHTML = theNewShares[i];
                    }
                }
            }
            function abridgeNumbers(theString,theLimit,theAbbrev,i,k){
                for (var j = 0; j &lt; theLimit; j++) {
                    if(typeof theNewShares[i] === 'undefined'){
                        theNewShares[i] = theString.charAt(j);
                    }else{
                        if(k == true){
                            theNewShares[i] = theNewShares[i] +"."+ theString.charAt(j);
                        }else{
                            theNewShares[i] = theNewShares[i] + theString.charAt(j);
                        }
                    }
                };
                theNewShares[i] = theNewShares[i] + theAbbrev;
            }
        });
&lt;/script&gt;
My first remark is that you're explicitely requiring jQuery for your code to execute but only use it for jQuery.getJSON(), while it may be used elsewhere.

So instead of:

        } else {
        var c = document.getElementsByClassName('box-counter');
        numb = c.length;
        for (var i = 0; i &lt; numb; i++) {
            var tempURL = c[i].parentNode.attributes['data-url'].nodeValue;
            jQuery.getJSON('http://graph.facebook.com/'+tempURL,function(data) {
                theShares.push(data.shares);
                insertDuring();
            });
        }
    }


you may simply write:

        } else {
        jQuery('.box-counter').each( function(i) {
            jQuery.getJSON(
                'http://graph.facebook.com/' + this.parent().data('url'),
                function(data) {
                  theShares.push(data.shares);
                  insertDuring();
                }
            );
        }
    }


You may also replace this:

function insertDuring(){
    if(numb == theShares.length){
        var c = document.getElementsByClassName('box-counter');
        for (var i = 0; i &lt; numb; i++) {
            // your internal code here...
        }
    }
}


by this simpler code:

function insertDuring(){
    jQuery('.box-counter').each( function(i) {
        // your internal code here, unchanged...
        this.innerHTML = theNewShares[i];
    }
}


Now regarding your abridgeNumbers() function:

function abridgeNumbers(theString,theLimit,theAbbrev,i,k){
    for (var j = 0; j &lt; theLimit; j++) {
        if(typeof theNewShares[i] === 'undefined'){
            theNewShares[i] = theString.charAt(j);
        } else {
            if(k == true){
                theNewShares[i] = theNewShares[i] +"."+theString.charAt(j);
            } else {
                theNewShares[i] = theNewShares[i] + theString.charAt(j);
            }
        }
    };
    theNewShares[i] = theNewShares[i] + theAbbrev;
}


You can simplify the code using the ternary operator, so improving readability, like this:

function abridgeNumbers(theString, theLimit, theAbbrev, i, k) {
    for (var j = 0; j &lt; theLimit; j++) {
        if(typeof theNewShares[i] === 'undefined') {
            theNewShares[i] = theString.charAt(j);
        } else {
            theNewShares[i] += (k ? "." :"") + theString.charAt(j);
        }
    }
    theNewShares[i] += theAbbrev;
}


(BTW you may notice that the ";" is not needed at the end of the for block)

In a more advanced way you may also increase performance. In the code above, each step executes the test about theNewShares[i], while it is useful only once. So you may deport it before the loop, using a short notation which ensures to define theNewShares[i] as empty string when it is yet undefined.

function abridgeNumbers(theString, theLimit, theAbbrev, i, k) {
    theNewShares[i] |= '';
    for (var j = 0; j &lt; theLimit; j++) {
        theNewShares[i] += (k ? "." :"") + theString.charAt(j);
    }
    theNewShares[i] += theAbbrev;
}


Finally beyond those purely technical aspects you may look at my second answer below about how to enhance the "conversion" strategy to get friendly representation of the numbers.