I saw there is an answered question about whether there is a difference between using $(document).ready(function(){}) and $(function(){}) (there isn’t), but my question is which is the preferred syntax and why.
I’ve been using jQuery for about a year and have always used the $(document).ready() syntax; but lately on SO and in some other places, I’ve seen the $(function()) syntax used more and more.
Is there a preferred syntax that you use and why do you use it? Do you use the shorter syntax solely to save a few characters?
Just a little more background, I’m currently starting a new app from the ground up and want to put some generally accepted best practices and standards in place.
Thanks in advance!
I use
$(document).ready(function(){});despite the additional typing. I just prefer having that in my code to remind me exactly when the code is going to run, and for the ability to search for ‘ready’ to find those sections. It’s the same functionality, and I normally take shortcuts, but in this case I don’t.