Implementation of insertion sort in JavaScript
Your function can only sort numbers and strings

The built in Array.sort function offers an optional compare function as a parameter. Adding this feature to your insertionSort function would allow for users to sort any type of object:

function defaultCompare(x, y) {
  if (x &lt; y)
    return -1;
  else if (x &gt; y)
    return 1;
  else
    return 0;
}

function insertionSort(array, compare) {
  compare = compare ? compare : defaultCompare;
  //...
}


You have quite a few variables that aren't necessary

totalElements

You only use this variable once, in the condition of your first for loop. JavaScript has some incredibly fast engines and with JIT-Compiler (just in time compiler) you don't have to worry about micromanaging optimizations like what if js isn't smart enough to know that it's recalculating inputArray.length - 1 each iteration. Defining totalElements arguably makes your code harder to read by adding another variable the scope, and the JS engines are smart-enough/fast-enough that performance isn't likely affected, so I would recommend getting rid of it.

temp

You could eliminate temp by abstracting out a function to swap indices in an array:

function swapIndices(array, index1, index2) {
  var temp = array[index1];
  array[index1] = array[index2];
  array[index2] = temp;
}


This also improves your code's cohesiveness by abstracting out the technicalities of swapping variables, and it allows you to use swapIndices elsewhere in your code.

lastIndex

This one is just a constant for zero. At the very least you should change the var to const:

const lastIndex = 0;


But I'd recommend to just get rid of it.

prevIndex

I find currentIndex - 1 more clear than prevIndex. The former is obviously the index 1 less than currentIndex whereas the latter could also mean the previously swapped element (precedingIndex is another possible name).

Summary

With all the above changes your code would look like:

function defaultCompare(x, y) {
  if (x &lt; y)
    return -1;
  else if (x &gt; y)
    return 1;
  else
    return 0;
}

function swapIndices(array, index1, index2) {
  var temp = array[index1];
  array[index1] = array[index2];
  array[index2] = temp;
}

function insertionSort(array, compare) {
  compare = compare ? compare : defaultCompare;

  for (let i = 0; i &lt; array.length; i++) {
    for (let currIndex = i; currIndex &gt; 0; currIndex--) {
      if (compare(array[currIndex], array[currIndex - 1]) &lt; 0) {
        swapIndices(array, currIndex, currIndex - 1);
      }
      else {
        break;
      }
    }
  }

  return array;
}