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.



Sibeesh VenuPosted Oct 26, 2015, 12:50 AM
Santhakumar Munuswamy Thank you
Sibeesh VenuPosted Oct 26, 2015, 12:50 AM
Priyaranjan K S Thanks buddy
Sibeesh VenuPosted Oct 26, 2015, 12:49 AM
Richardson dos Santos Neves Yes you are right.
Santhakumar MunuswamyPosted Oct 25, 2015, 11:45 AM
Good one
Priyaranjan K SPosted Oct 21, 2015, 1:07 PM
Good One Sibi ..
Richardson dos Santos NevesPosted Oct 21, 2015, 9:05 AM
Declarative functions are as variable assignments, are defined and made available at the time they occur.
Sibeesh VenuPosted Oct 21, 2015, 6:45 AM
Nilesh Jadav Thanks buddy
Sibeesh VenuPosted Oct 21, 2015, 6:44 AM
Aqib Shehzad Thanks
Nilesh JadavPosted Oct 21, 2015, 6:39 AM
Good one sir !!
Muhammad Aqib ShehzadPosted Oct 21, 2015, 6:14 AM
Good one