How to get diagonal numbers between two number in a matrix?

You could take the absolute delta and check with the remainder operator if the delta is multiple of the length minus one or plus one.

function check(array, i, j) {
   var length = Math.sqrt(array.length),
       delta = Math.abs(i - j),
       lines = Math.abs(Math.floor(i / length) - Math.floor(j / length));
   
   return delta === lines * (length - 1) || delta === lines * (length + 1);
}

var array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];

console.log(check(array, 0, 15)); // true
console.log(check(array, 3, 12)); // true
console.log(check(array, 11, 6)); // true
console.log(check(array, 9, 6)); // true

console.log(check(array, 4, 15)); // false
console.log(check(array, 8, 12)); // false
console.log(check(array, 8, 3)); // false
.as-console-wrapper { max-height: 100% !important; top: 0; }

simply get the col and row, and check if delta is the same.

(don't really need to take an array, so I just take it's dimension)

function check(dim,a,b){
  let [x1,y1]=[Math.floor(a/dim),a%dim]
  let [x2,y2]=[Math.floor(b/dim),b%dim]
  return Math.abs(x1-x2)==Math.abs(y1-y2)
}

console.log(check(4,0,15))
console.log(check(4,3,12))
console.log(check(4,11,6))
console.log(check(4,9,6))
console.log(check(4,4,15))
console.log(check(4,8,12))
console.log(check(4,6,12))