Sometimes developer’s get confused between these two topics (I am saying about the starters like me). Basically both function declaration and function expression are same. Only difference depends on how you handle or treat these two. We will see a basic demo here to ensure the difference between function declaration and expression. I hope you will like this.
Here I am using jQuery 2.0.2 version, you can use whichever version you like.
Background
In my project I am using both function declarations and function expressions. So I thought to highlight some basic difference between them, so it may help someone.
The basic difference between function declaration and function definition:
- Function declarations are loaded first, that is before any codes.
- Function expressions are loaded when the interpreter reaches that particular line of code.
- Basically we can call any code only after the declarations are loaded.
We will try to find out this difference with a demo.
Using the code
First thing you need to do is create a page.
- <!DOCTYPE html>
- <html>
-
- <head>
- <title>Function Declaration VS Expression - Sibeesh Passion</title>
- </head>
-
- <body> Function Declaration VS Expression - Sibeesh Passion </body>
-
- </html>
Then you need to include the script needed.
- <script src="http://sibeeshpassion.com/content/scripts/jquery-2.0.2.min.js"></script>
Now we will create a function declaration as follows.
- <script>
- $(document).ready(function()
- {
- alert(myFunctionDeclaration());
-
- function myFunctionDeclaration()
- {
- return 'myFunctionDeclaration is called';
- }
- });
- </script>
What will be the output of this?
Function Declaration And Function Expression.
Now we will change our script as follows.
- <script>
- $(document).ready(function()
- {
- function myFunctionDeclaration()
- {
- return 'myFunctionDeclaration is called';
- }
- alert(myFunctionDeclaration());
- });
- </script>
This will also return the same output.
Now we will create a function expression as follows.
- <script>
- $(document).ready(function()
- {
- alert(myFunctionExpression());
- var myFunctionExpression = function()
- {
- return 'myFunctionExpression is called';
- }
- });
- </script>
What may be the output of this? We will check it now.
Function Declaration And Function Expression
As you can see, instead of giving an alert it is throwing an error
Uncaught TypeError: myFunctionExpression is not a function in the console. It is just because the function expression is not yet loaded. You are trying to call that expression before it loads.
So what we can do to make it work? Simple, just change the script as follows.
- <script>
- $(document).ready(function()
- {
- var myFunctionExpression = function()
- {
- return 'myFunctionExpression is called';
- }
- alert(myFunctionExpression());
- });
- </script>
Now you will get an alert as follows.
That's all. I hope you know the difference between function declaration and function expression now.
Complete Code
- <!DOCTYPE html>
- <html>
-
- <head>
- <title>Function Declaration VS Expression - Sibeesh Passion</title>
- <script src="http://sibeeshpassion.com/content/scripts/jquery-2.0.2.min.js"></script>
- <script>
- $(document).ready(function()
- {
- var myFunctionExpression = function()
- {
- return 'myFunctionExpression is called';
- }
- alert(myFunctionExpression());
- alert(myFunctionDeclaration());
-
- function myFunctionDeclaration()
- {
- return 'myFunctionDeclaration is called';
- }
- });
- </script>
- </head>
-
- <body> Function Declaration VS Expression - Sibeesh Passion
- <div class="first"></div>
- </body>
-
- </html>
Conclusion
Did I miss anything that you may think which is needed? Could you find this post as useful? I hope you liked this article. Please share me your valuable suggestions and feedback.
Your turn. What do you think?
If you have any questions, then please mention in the comments section.
Read this article in my blog here.