2

In client side javascript, whats the difference b/w

$(function(){
   ....
});

and

function myFunc() {
   ...
}

(Could not find relevant tutorials on Google)

0

3 Answers 3

4

The first is a DOM ready handler function used in jQuery (a JavaScript library). It is executed when the DOM is fully loaded. Whereas, the second is a simply defined function with name myFunc.

You can read more about JavaScript functions in MDN: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Function.

Sign up to request clarification or add additional context in comments.

Comments

2

They are totally different uses of function.
The first on calls function $() with parameter function() {}.
The second defines function myFunc.

Comments

1

$() is a shorthand for jQuery's document.ready syntax: Documentation

Code inside a $() will run when the dom has loaded enough to be accessed/manipulated.

The second example is just a normal function declaration and creates a function named myFunc that can be called with syntax myFunc() later.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.