First I linked two JavaScript files to my HTML
<script src="script.js"></script>
<script src="main.js"></script>
On the first file ( the "script.js" file) I wrote this code
function testing() {
console.log("does it show in the console");
}
testing()
setTimeout(()=> testing(), 3000)
on the second file (the "main.js" file) I wrote this code
function testing() {
console.log("this will override it");
}
testing()
The result I got on the console
does it show in the console script.js:20
this will override it main.js:2
this will override it main.js:2
Based on the knowledge I have I expected the function declaration (testing()) in the second file to override the function testing() in the first file because that would have been the result if I wrote both in the same file.
that was my reason for adding the setTimeout function
function testing() {
console.log("does it show in the console");
}
testing()
setTimeout(()=> testing(), 3000)
function testing() {
console.log("this will override it");
}
testing()
The result on the console
this will override it script.js:27
this will override it script.js:27
this will override it script.js:27
It wasn't really confused I'd rather say I learnt something new because I tried the same thing with one external JavaScript file and the inline JavaScript script tag
the external JavaScript script.js file
function testing() {
console.log("does it show in the console");
}
testing()
setTimeout(()=> testing(), 3000)
the inline JavaScript script tag, as you can see the external script tag was also placed at the top here
<script src="script.js"></script>
<script>
function testing() {
console.log("this will override it");
}
testing()
</script>
I got the same result on the console
does it show in the console script.js:20
this will override it index.html:27
this will override it index.html:27
I concluded that's probably how the JavaScript engine runs code, if they are in different file.
Here's what deepseek AI said
Why Your Original Code Worked "Weirdly"
Browsers parse scripts sequentially but execute them immediately when encountered.Your external script's testing() was called before the inline script had a chance to overwrite it.
The console shows historical logs, even if functions are later modified.
This behavior is why experienced developers avoid duplicate function names! Let me know if you'd like to dive deeper into execution contexts. 🚀
I was happy I learnt something new until I decided to play around with the code again
I added both code to the same file again but did something different this time. I placed the first function in a block (curly brackets)
{
function testing() {
console.log("does it show in the console");
}
}
testing()
setTimeout(()=> testing(), 3000)
function testing() {
console.log("this will override it");
}
testing()
Here the result I got on the console
does it show in the console script.js:22
does it show in the console script.js:22
does it show in the console script.js:22
Now that really got me confused (completely). At this point AI didn't give any reasonable explanation, please I need someone to explain what happened in the last code.
Why did the first function in the curly braces print to the console despite declaring (overriding) the same function in the same code?