Shorter way of writing my displayError() function? Is there a way I can write this function with less code?

jQuery

function displayError(display, string, xhr) {
    var args = arguments;
    var $error = $("div#error");
    var visible = $error.is(":visible");

    if(args.length == 1 &amp;&amp; typeof display === "boolean" &amp;&amp; display == false) {
        if(visible) {
            $error.slideUp();
        }
    } else if(args.length == 2 &amp;&amp; typeof display === "boolean" &amp;&amp; display == true &amp;&amp; typeof string == "string") {      
        if(visible) {
            $error.fadeOut(function () { 
                $(this).html('&lt;b style="color: #ce1919;"&gt;(!)&lt;/b&gt; '+string).fadeIn();
            });
        } else {
            $error.html('&lt;b style="color: #ce1919;"&gt;(!)&lt;/b&gt; '+string).slideDown();
        }
    } else if(args.length == 3 &amp;&amp; typeof display === "boolean" &amp;&amp; display == true &amp;&amp; typeof string == "string" &amp;&amp; typeof xhr === "object") {
        if(visible) {
            $error.fadeOut(function () { 
                $(this).html('&lt;b style="color: #ce1919;"&gt;('+xhr.status+")&lt;/b&gt; "+string).fadeIn();
            });
        } else {
            $error.html('&lt;b style="color: #ce1919;"&gt;('+xhr.status+")&lt;/b&gt; "+string).slideDown();
        }
    }
}


Based on Marco's answer I came up with this:

function displayError(display, string, xhr) {
    var $error = $("div#error");
    var errVisible = $error.is(":visible");
    var errStatus = typeof xhr === "object" ? xhr.status : "!";
    var errMsg = '&lt;b style="color: #ce1919;"&gt;(' + errStatus + ")&lt;/b&gt; " + string;

    if(display) {
        if(errVisible) {
            $error.fadeOut(function() {
                $(this).html(errMsg).fadeIn();
            });
        } else {
            $error.html(errMsg).slideDown();
        }
    } else {
        $error.slideUp();
    }
}
I don't use Javascript, but i could write it in this way:

function displayError(display, string, xhr) 
{
    var args = arguments;
    var $error = $("div#error");
    var visible = $error.is(":visible");

    if (!display)
    {
        $error.slideUp();
    }
    else
    {

        if (visible) 
        {
            switch (args.length) 
            {
                case 1:
                case 2:
                    $error.fadeOut(function () 
                    {
                        $(this).html('&lt;b style="color: #ce1919;"&gt;(!)&lt;/b&gt; ' + string).fadeIn();
                    });
                    break;
                case 3:
                    $error.fadeOut(function () 
                    {
                        $(this).html('&lt;b style="color: #ce1919;"&gt;(' + xhr.status + ")&lt;/b&gt; " + string).fadeIn();
                    });
                    break;
            }
        } 
        else 
        {
            $error.html('&lt;b style="color: #ce1919;"&gt;(' + xhr.status + ")&lt;/b&gt; " + string).slideDown();
        }
    }
}


But why you use args.length == 1/2/3 if you don't do anything with it?
You could just write:

function displayError(display, string, xhr) {
    var $error = $("div#error");
    var visible = $error.is(":visible");
    var status = typeof xhr === "object" ? xhr.status : "!";

    if (display) {
        if (visible) {
            $error.fadeOut(function () {
                $(this).html('&lt;b style="color: #ce1919;"&gt;(' + status + ')&lt;/b&gt; ' + string).fadeIn();
            });

        } else {
            $error.html('&lt;b style="color: #ce1919;"&gt;(' + status + ")&lt;/b&gt; " + string).slideDown();
        }
    } else {
        $error.slideUp();
    }
}


I used var status = typeof xhr === "object" ? xhr.status : "!"; so you will get the status if xhr is an object, or ! if not.

I removed all typeof parts since i found it useless, you will always get a string so why you worry about it