Hello there! I'm diving into the world of JavaScript and have a query to share on Stackoverflow. If something seems missing in my question, please do point it out for me.
Question 1: Can someone shed light on why the output sequence in the console sometimes deviates from the order in which functions are called (myFunction(); and messageLogger();)?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Test Page</title>
</head>
<body>
<script>
let myFunction = function messageLogger() {
console.log('Message Logged');
}
myFunction(); //Should display message in console
messageLogger(); //Should trigger an error in console
</script>
</body>
</html>
This is a snippet demonstrating a Basic Function Expression as depicted above and also seen in this image Original Code Image.
When executing myFunction();, this code should ideally showcase an error in the console, just like in the illustration found here Console Output Image 1. However, at times, it throws an error even before running myFunction();, resembling what's captured in this image Console Output Image 2.