Let us create a function called cool and call it immediately and then we shall create the same function with small change in console message as follows.
function cool(){ console.log('first function'); } cool(); function cool(){ console.log('second function'); } cool();
When we execute the above JavaScript code, we will get the output as
second function second function
In our code, the hoisting is happening. The JavaScript will move all the declaration to top. So when we create the second function it has overwritten the values.
To avoid this we can declare our function with variable like follows
var a = function cool(){ console.log('first function'); } a(); var a = function cool(){ console.log('second function'); } a();
Output
first function second function
This time function is writing messages as expected.
Happy Coding 🙂