Merge two sorted arrays that can be of different sizes I think the algorithm implementation itself is reasonably efficient, but I'm not sure about proper use of good practices in software development in the code (variable naming, idioms, comments, error handling in inputs etc).

I'm attempting to get a first developer job, and since interviewers also evaluate how "clean" the code is, the main point of the question is what is considered good practice, but efficiency improvements are also welcome.

mergeSortedArrays = function(arr1, arr2){
  if(!(Array.isArray(arr1) &amp;&amp; Array.isArray(arr2))){
    throw new TypeError("Need two arrays to merge")
  };

  if(arr2.length === 0 &amp;&amp; arr1.length === 0){
    throw new RangeError("Cannot merge empty arrays")
  };

  let i = 0;
  let j = 0;

  const targetSize = arr1.length + arr2.length;
  const mergedArray = [];
  // main loop
  while(mergedArray.length &lt; targetSize){
    /**
     * Works until completion for arrays of the same size
     */
    if(arr1[i] &lt; arr2[j]){
      valueToPush = arr1[i]
      i++
    }else{
      valueToPush = arr2[j]
      j++
    }
    /**
     * For arrays with different sizes, it is safe to push the remainder to the sorted array, given that both inputs are sorted.
     */
    if(valueToPush === undefined){
      const remainingItems = j &gt; arr2.length ? arr1.slice(i) : arr2.slice(j)
      mergedArray.push(...remainingItems)
      break
    }

    mergedArray.push(valueToPush)
  }
  return mergedArray;
}
```
I think considering approach that you chose, your code is alright. A few points:


What I don't like is that you get error when passing 2 empty arrays? Why would you do that? Imagine I am generating arrays of different sizes and using your code to merge and sometimes I would pass 2 empty arrays. Should I really need to handle that as special case in my code? No, just return empty array too.
I'd eliminate targetSize variable and check for j &lt; arr2.length &amp;&amp; i &lt; arr1.length instead.
Checking valueToPush for undefined feels a bit off for me. It works, but doesn't really feel right. In many languages you would get something like IndexOutOfRange error or something. Maybe it's idiomatic in Javascript (probably not), but I think nicer and cleaner would be to check if you are still in range of array, ex j &lt; arr2.length.