To expand a little further on @Codeinventor’s explanation, when you used the var keyword inside the function, you created a new variable also called aNumber, but this one limited in scope to just that function.
The new aNumber variable is not acessible outside of the function, and despite having the same name as the first one, they are stored differently. so changing one doesn’t affect the other.
And since JS is not whitespace dependant, even though the console.log is at the same level as the function, it is outside the braces, so it can only access the first aNumber, which is 5. Hope that helps.