We have already seen, how to print Hello World on the screen using JQuery.
$(document).ready(function () { document.write("Hello World") });
Now , let us make the above code a little short.
$(function () { document.write("Hello World") });
So , we have omitted document and ready(),
$(document).ready(function () {
And reduced the line to,
$(function () {
Well ! $(document).ready() is not a mandatory one. If you want, you can minimise the line to $(). And it just works as expected.
<html> <body> <script src = "https://cdnjs.cloudflare.com/ajax/libs/JQUERY/3.3.1/jquery.min.js"> </script> <script> $(function () { document.write("Hello World") }); </script> </body> </html>
The $ sign in JQuery can be considered as a main gate for JQuery. Any code you want to write in JQuery, that should begin with $ sign.
$(function () { document.write("Hello World") });
Any code that you write inside the parenthesis () of $ sign, would be treated as JQuery code.
So what is function () inside the $(...)?
It is something called as Anonymous Function.
An Anonymous Function is a function with no name.
If you are not aware of Functions. Just remember, a function is something that does some work for us.
Say for example, the below function, function ()(That we have inside $(...)),
function () { document.write("Hello World") }
Does the work of printing Hello World on the screen.
And since the function has no name, it is called as Anonymous Function.
And when the Anonymous Function is put inside $(...),
$(function () { document.write("Hello World") });
The Anonymous Function is treated as a JQuery code and the output Hello World is printed on the screen.
We can do a lot of cool stuff using JQuery. But a basic understanding of DOM is needed.
Let us look at DOM next.