In this example, you can dispense with the function altogether and just do:
var custom_type = $('#myDiv').attr('custom_type');
However in general you can use an IIFE for more complex "just-in-time" computation of variable assignments - I like to use them if I need to iterate over something, so I can have i
without polluting the current scope.
In your second example, though, the result is completely different - you will need to call the function custom_type()
to get the current value, whereas the first piece of code will get its value once, and the variable will hold that value.