Different ways to write Jquery function (Part 2)


So many time in interview, we will get this question. But maximum people give the answer of one or two approach of writing function in Jquery.

we can write the the function in Jquery 4 ways. I will include all the 4 ways in given below example.

jqueryfunction

Complete source code as given below


<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="../Scripts/jquery.min.js"></script>
    <script>
        //1 Standard ways
        jQuery(document).ready(function () {
            alert("Hello from Jquery");
        });
        
        //2 Standard ways using $ alias

        $(document).ready(function () {
            alert("Hello from Jquery using $");
        });

        //3 using shortcut 
        $(function () {
            alert("Hello from Jquery using shortcut");
        });

        //4 Shortcut with failsafe usage of $

        jQuery(function ($) {
            alert("Hello from Jquery with Failsafe");
        });
    </script>
</head>
<body>
</body>
</html>

Advertisement

One thought on “Different ways to write Jquery function (Part 2)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.