title: Replace the empty element of an array with another array or with another element in javascript | javascript | array
body: <p>let say I have an array in javascript with the following content.</p>
  
  <pre><code>let arr = [1, 2, , 4]; // [1, 2, empty, 4]
  arr[5] = 6; // [1, 2, empty, 4, empty, 6]
  </code></pre>
  
  <p>as you can see that the arr variable has an empty value in the 2nd and 4th position. and I want to fill these empty values with the array provided as for filling like</p>
  
  <pre><code>let replace_empty_with = [3, 5];
  </code></pre>
  
  <p>So, first of all, I want to know whether an array has empty value or not so I use the following methods</p>
  
  <pre><code>arr.includes(undefined);
  // or
  Object.values(arr).length == arr.length 
  </code></pre>
  
  <p>Now how can I fill the empty value of the original array(arr variable) with the value of the replace_empty_with array variable?</p>
  
  <p><strong>Desired output</strong>
  arr = [1, 2, 3, 4, 5];</p>
  
  <p>I have tried with the forEach function of array to know if the particular element has undefined or null value but I did not get success.</p>
  
  <p>also, I have tried with indexOf to find the first undefined element of the array but it always returns -1 as indexOf(undefined) - state that there is no undefined element in the array.</p>