JavaScript function that reuses a modal for adding, deleting, and viewing an item I have a table that has buttons per row that makes views/edit an item in a modal and they have a layout like so:
&lt;td&gt;
    &lt;button class=&quot;btn btn-sm btn-default&quot; onclick=&quot;setModalMode('view', '8')&quot; data-toggle=&quot;modal&quot; data-target=&quot;#license-add&quot; data-id=&quot;8&quot;&gt;
        &lt;i class=&quot;far fa-eye&quot;&gt;&lt;/i&gt;
    &lt;/button&gt;
    &amp;nbsp;
    &lt;button class=&quot;btn btn-sm btn-info&quot; onclick=&quot;setModalMode('update', '8')&quot; data-toggle=&quot;modal&quot; data-target=&quot;#license-add&quot; data-id=&quot;8&quot;&gt;
        &lt;i class=&quot;far fa-edit&quot;&gt;&lt;/i&gt;
    &lt;/button&gt;
    &amp;nbsp;
    &lt;button class=&quot;btn btn-sm btn-danger&quot; onclick=&quot;setModalMode('delete', '8')&quot; data-toggle=&quot;modal&quot; data-target=&quot;#license-add&quot; data-id=&quot;8&quot;&gt;
        &lt;i class=&quot;far fa-trash-alt&quot;&gt;&lt;/i&gt;
    &lt;/button&gt;
&lt;/td&gt;

It calls the setModalMode functiton and toggles the mode of the modal:
let modal = 'div#license-add';
let $form = $('form#modal-form');
let modalMode = ''; // this is used by my AJAX function to point what URL should my requests go
let rowObjects = []; // this gets filled up by values on a different function everytime my table generates

function setModalMode(mode, id) {
    loadingOverlay(false, modal);
    modalMode = mode;

    let title = 'Modal title';
    let inputsDisabled = false;
    let hasData = true;
    let needsButton = true;
    let isDelete = false;

    if (mode === 'add') {
        title = 'Add new license';

        hasData = false;
    } else if (mode === 'update') {
        title = 'Updating license';
    } else if (mode === 'view') {
        title = 'Viewing license';

        needsButton = false;
        inputsDisabled = true;
    } else if (mode === 'delete') {
        title = 'Delete this license?';

        inputsDisabled = true;
        isDelete = true;
    } else {
        return;
    }

    if (hasData) {
        let data = rowObjects['license', id];

        $form.find('input#email').val(data.email);
        $form.find('input#key').val(data.activation_key);
        $form.find('input#expiration').val(moment(data.expiration).format('MM/DD/YYYY'));
        $form.find('select#type-select').val(data.type);
        $form.find('input#hidden-id').val(data.id);
    } else {
        $form.find(':input').val('');
    }

    if (needsButton) {
        if (isDelete) {
          $('#deleteButton', modal).show();
          $('#submitButton', modal).hide();
        } else {
          $('#deleteButton', modal).hide();
          $('#submitButton', modal).show();
        }
    } else {
        $('#submitButton', modal).hide();
        $('#deleteButton', modal).hide();
    }

    $('h4#modal-title', modal).text(title);
    $form.find(':input').prop('disabled', inputsDisabled);
}

Although the code does its job perfectly well, I feel like my code is a mess of if statements
Avoid inline handlers - they have way too many problems to be worth using nowadays, such as a demented scope chain and quote escaping issues. Attach event listeners properly using Javascript with addEventListener instead.
While you could iterate over all buttons and add a listener to each, consider using event delegation instead: add a single click listener to the table, and on click, if the click was on a button, examine the button's attributes or index to see which one was clicked, and examine the data-id attribute to get the ID.
Something along the lines of:
&lt;td&gt;
    &lt;button data-mode=&quot;view&quot; class=&quot;btn btn-sm btn-default&quot; data-toggle=&quot;modal&quot; data-target=&quot;#license-add&quot; data-id=&quot;8&quot;&gt;
        &lt;i class=&quot;far fa-eye&quot;&gt;&lt;/i&gt;
    &lt;/button&gt;
    &amp;nbsp;
    &lt;button data-mode=&quot;update&quot; class=&quot;btn btn-sm btn-info&quot; data-toggle=&quot;modal&quot; data-target=&quot;#license-add&quot; data-id=&quot;8&quot;&gt;
        &lt;i class=&quot;far fa-edit&quot;&gt;&lt;/i&gt;
    &lt;/button&gt;
    &amp;nbsp;
    &lt;button data-mode=&quot;delete&quot; class=&quot;btn btn-sm btn-danger&quot; data-toggle=&quot;modal&quot; data-target=&quot;#license-add&quot; data-id=&quot;8&quot;&gt;
        &lt;i class=&quot;far fa-trash-alt&quot;&gt;&lt;/i&gt;
    &lt;/button&gt;
&lt;/td&gt;

$(table).on('click', 'button', function() {
  const { id, mode } = this.dataset;
  // ...
});

Always use const when you can - see https://softwareengineering.stackexchange.com/questions/278652/how-much-should-i-be-using-let-vs-const-in-es6. let permits reassignment, which makes the code's intent harder to understand at a glance - whenever a developer sees a let, they may well think &quot;This was declared with let, not const, so I need to be on the lookout for where this will get reassigned.&quot;
Give functions names with verbs - the loadingOverlay function might be more readable if it was named something like showLoadingOverlay or loadOverlay or something of the sort. (not sure what it does)
Accidental use of the comma operator - rowObjects['license', id] is equivalent to rowObjects[id]. Did you mean rowObjects['license' + id]? Consider using a linter to warn you about these sorts of potential mistakes automatically.
Array keys should be numeric only - if your keys are going to contain non-numeric strings like license, or if you aren't going to have all elements of an array filled, use an object instead:
const rowObjects = {};

Assigning titles concisely can be done by making an object indexed by mode string. When you need to figure out the title to set, just look up the property on the object: see below.
Abstract separate functionality into functions - see this anecdote. While you don't have to go that far, if you have a non-trivial block of code that does something somewhat separate from the rest of the code in a section, consider putting it into a function instead. I'd put the population of the $form into a function - see below.
Button toggling Instead of needsButton and inputsDisabled, consider hiding both buttons by default, then showing one or the other depending on whether delete, or something other than view was pressed:
const titlesByMode = {
    add: 'Add new license',
    update: 'Updating license',
    view: 'Viewing license',
    delete: 'Delete this license?',
};
$(table).on('click', 'button', function() {
    showLoadingOverlay(false, modal);
    const { id, mode } = this.dataset;
    modalMode = mode;
    if (mode === 'add') {
        $form.find('input').val('');
    } else {
        populateForm(id);
    }
    $form.find(':input').prop('disabled', mode === 'view' || mode === 'delete');

    $('h4#modal-title', modal).text(titlesByMode[mode]);
    // Hide both buttons by default:
    $('#submitButton', modal).hide();
    $('#deleteButton', modal).hide();
    if (mode === 'delete') {
        $('#deleteButton', modal).show();
    } else if (mode !== 'view') {
        $('#submitButton', modal).show();
    }
});

const populateForm = (id) =&gt; {
    const data = rowObjects['license' + id];

    $form.find('input#email').val(data.email);
    $form.find('input#key').val(data.activation_key);
    $form.find('input#expiration').val(moment(data.expiration).format('MM/DD/YYYY'));
    $form.find('select#type-select').val(data.type);
    $form.find('input#hidden-id').val(data.id);
};
```