Search code examples
javascriptoopexecution

Execution order of JS


I have following code:

<!DOCTYPE html>  
<html data-ng-app="">  
 <head>
     <script type="text/javascript" src="myJs.js"></script>  
     <script type="text/javascript">  
         function myFunction(){  
             alert("Hello.. internal JS");  
         }  
     </script>  

    <!--<script type="text/javascript" src="myJs.js"></script>-->  
    <!--<script type="text/javascript" src="angular.min.js"></script>-->  
</head>  
<body>  

<button onclick="myFunction()">Click me</button>  

</body>  
</html> 

and myJS.js :

function myFunction(){ 
    alert("Hello.. External JS");  
}   

and output is: Hello.. internal JS.

I would like to know why the last JS gets executed every time? If I shift the lines to alternate position, the one at the end gets executed.

Thanks


Solution

  • You can check the below fiddle to understand it better. If you define the same function twice, the bottom most will be called.

    http://jsfiddle.net/5dALP/