'why is bool always FALSE, cannot reassign bool value in ajax call [duplicate]

learning javascript and making an ajax call as follows:

var emailExists= function (email) {

    var bool = false; 
    $.ajax({
        url: '@Url.Action("checkEmail")',
        type: 'POST',
        data: JSON.stringify({ 'email': email }),
        contentType: "application/json; charset=utf-8",
        async: true,
        success: function (response) {
            if (response.success) {
                console.log('success');
            } else {
                console.log('fail');
                $('#email-exists').show();
                bool = true;
            }
        },
        error: function (xhr, x, y) {
            $("body").removeClass("loader-section");
            $("div.loader-icon").hide();

            console.error('failed', xhr, x, y);
        }
    });
    console.log(bool);
    return bool;
}

as you can see in this line,

            console.log('fail');
            $('#email-exists').show();
            bool = true;

i am setting bool to be true, however, it never reassign the value of the bool variable, and always prints and returns false, despite the outcome of the response of the ajax call



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source