JQuery return a function to a variable

javascript, jquery

Solution

If I'm understanding you correctly, you're trying to set a variable to the result of a function. This should do what you're hoping for;

var ztype = function(){ //can be set to Full / percentage / Image / left empty for native image size
    var type = $('.carousel').attr('data-zoom');
    if(!type){
        type = $(that).attr('data-zoom');
    }

    return type;
}();

Problem

Is there any way to set a variable equal to a function, but instead of the variable being equal to the function it is equal to the functions return value? Searched for a bit on this without any success, it is most likely because I don't know how to phrase the question, but any help is appreciated, this is what I have : Instead of having to set `var ztype = zoom()` after the function, is there anyway for me to assign the return value to `zoom` without an extra line of code? ``` var zoom = function(){ //can be set to Full / percentage / Image / left empty for native image size var type = $('.carousel').attr('data-zoom'); if(!type){ type = $(that).attr('data-zoom'); } return type; } var ztype = zoom(); ```

Original source