Can't understand a helper function in the javascript min-heaps function works

I’m trying to understand min-heaps at the moment and I can’t understand how the exists() method won’t usually evaluate to false in this context, primarily in the heapify function:

class MinHeap {
constructor() {
this.heap = [ null ];
this.size = 0;

popMin() {
if (this.size === 0) {
return null
console.log(\n.. Swap ${this.heap[1]} with last element ${this.heap[this.size]});
this.swap(1, this.size);
const min = this.heap.pop();
console.log(.. Removed ${min} from heap);
return min;

add(value) {
console.log(.. adding ${value});
console.log(added ${value} to heap, this.heap);

bubbleUp() {
let current = this.size;
while (current > 1 && this.heap[getParent(current)] > this.heap[current]) {
console.log(.. swap ${this.heap[current]} with parent ${this.heap[getParent(current)]});
this.swap(current, getParent(current));
console.log(‘…’, this.heap);
current = getParent(current);

heapify() {
let current = 1;
let leftChild = getLeft(current);
let rightChild = getRight(current);

while (this.canSwap(current, leftChild, rightChild)) {
  if(this.exists(leftChild) && this.exists(rightChild)){
    if(leftChild < rightChild){
      this.swap(current, leftChild)
      current = leftChild
    } else if(leftChild > rightChild) {
      this.swap(current, rightChild)
      current = rightChild
  } else {
    this.swap(current, leftChild)
    current = leftChild
  leftChild = getLeft(current);
  rightChild = getRight(current);


exists(index) {
return index <= this.size;

canSwap(current, leftChild, rightChild) {
// Check that one of the possible swap conditions exists
return (
this.exists(leftChild) && this.heap[current] > this.heap[leftChild]
|| this.exists(rightChild) && this.heap[current] > this.heap[rightChild]
swap(a, b) {
[this.heap[a], this.heap[b]] = [this.heap[b], this.heap[a]];

const getParent = current => Math.floor((current / 2));
const getLeft = current => current * 2;
const getRight = current => current * 2 + 1;Preformatted text

It seems like the getLeft and getRight helper functions will increase at a faster rate than this.size since we’re multiplying the values together, and thus (index <= this.size) will usually evaluate to false. Any help would be greatly appreciated

The helper functions that you’re referring to are for finding the child elements of elements in the heap array. In a small heap, you’re correct. It wouldn’t take long to reach an element with no children. In a larger heap it would obviously take more swaps to reach that point.

I’m not sure I understand where your confusion lies, but possibly you are concerned that the multiplication somehow means we’re ending our while loop prematurely, and skipping over some elements of the array that need to be looked at. That is the point of the heap. A min-heap is a quick and efficient way to keep the minimum value of the array identified and ready to retrieve without having to maintain a completely sorted array. Sorting a large array is a very costly operation by comparison.