My Binary Search Implementation I have implemented binary search in JavaScript.

I run it on node.

I pass an comma separated string of numbers in ascending order as first argument and the number to be searched in the second argument.

the code is as follows,

var myArray = [], 
searchNum = undefined;

// below function is used to capture the 
// commandline parameters for array and the
// number to be searched
(function(){
    process.argv.forEach(function (val, index, array) {
        var idx = 0, ar = undefined;

        try{

            if(index === 2){

                ar = val.split(",");

                // convert the numbers present as string values is array
                // to array of numbers
                for(idx = 0; idx &lt; ar.length; idx++){
                    myArray.push(parseInt(ar[idx]));
                }
            }// end of if

            // third index is the number to be searched.
            if(index === 3){
                searchNum = parseInt(val)
            }

        }catch(e){
            console.log(e)
        }

    });
})();

console.log(" SEARCH NUMBER ",searchNum," in array ",myArray);
console.log(" Number ",searchNum," "+binarySearch(myArray,searchNum));

// binary-Search implementation
function binarySearch(myArray,numberToSearch){
    var arrayLength = myArray.length,
        midIndex = parseInt(arrayLength/2), 
        subArray = undefined,
        lowerIndex = 0,
        higherIndex = 0; 

    if(myArray[midIndex] === numberToSearch){
        return "is Found";

    // search the number in left side if array
    // i.e. number is found to the left of the 
    // middle Index 
    }else if(midIndex !== 0 &amp;&amp; myArray[midIndex] &gt; numberToSearch){ 
        lowerIndex = 0;
        higherIndex = midIndex;
        subArray = myArray.slice(lowerIndex,higherIndex); // create the sub-array
        return binarySearch(subArray,numberToSearch); // search the number in the subarray

    // search the number in right side if array
    // i.e. number is found to the right of the 
    // middle Index 
    }else if(midIndex !== 0 &amp;&amp; myArray[midIndex] &lt; numberToSearch){ // search the number in right side if array
        lowerIndex = midIndex + 1;
        higherIndex = arrayLength;
        subArray = myArray.slice(lowerIndex,higherIndex); // create the sub-array
        return binarySearch(subArray,numberToSearch); // search the number in the subarray
    }else{
        return "can't be found";
    }   
}// end of binarySearch method


I run the above as follows

E:\RahulShivsharan\MyPractise\DesignPatternsInJavaScript&gt;node binarySearch.js 34,45,67,89,90,123,345 300
 SEARCH NUMBER  300  in array  [ 34, 45, 67, 89, 90, 123, 345 ]
 Number  300  can't be found

E:\RahulShivsharan\MyPractise\DesignPatternsInJavaScript&gt;node binarySearch.js 34,45,67,89,90,123,345 45
 SEARCH NUMBER  45  in array  [ 34, 45, 67, 89, 90, 123, 345 ]
 Number  45  is Found

E:\RahulShivsharan\MyPractise\DesignPatternsInJavaScript&gt;


Requesting to please review my above implementation, and give your valuable feedback.
Just focusing on the initialization part.


The code is using both index and idx which gives off a code smell, something could be better there
The code is using idx just to loop over an array, apply a function and push. We can do better than this
Comments should be useful, commenting the end of an if statement is not useful

(function(){
  process.argv.forEach(function (val, index, array) {

    try{
      //Add the comma separated numbers to myArray as integers    
      if(index === 2){
        myArray = myArray.concat( val.split(",").map(c=&gt;parseInt(c)) );
      } else if (index === 3){
        //Third index is the number to be searched.
        searchNum = parseInt(val)
      }
    }catch(e){
      console.log(e)
    }
  });
})();



If you think further, then we could skip the whole loop business, and just access the 2 values we need. It would be simpler, and better.

    (function(){
      var args = process.argv;

      try{
        //Add the comma separated numbers to list as integers    
        list = list.concat( args[2].split(",").map(c=&gt;parseInt(c)) );
        //Third index is the number to be searched.
        searchNum = parseInt( args[3] );
      }catch(e){
        console.log(e)
      }
    })();


myArray is not a great name, I preferred list.