function example(arr, type) { if( type == 'asc' ) { var start = 0, end = arr.length, step = 1; } else { start = arr.length; end = -1; step = -1; } for (var x = start; x != end; x += step ) { alert(arr[x]); } }