In JavaScript, if you assign a value to an identifier (variable) without declaring it with var, let, or const, it will be treated as a global variable. This means that the variable will be created in the global scope and will be accessible from anywhere in your code.
However, if you are running your code in strict mode (by adding "use strict"; at the beginning of your script or function), you will get a reference error when you try to access an undeclared variable. Strict mode is a feature in JavaScript that makes it more strict about certain practices and helps you avoid mistakes in your code.
In the example you provided, if you are running your code in strict mode, you will get a reference error when you try to access MyId because it is not declared. However, if you are not running your code in strict mode, you will not get an error when you try to access MyId. Instead, the value of MyId will be logged to the console.
It is generally a good practice to always declare your variables with var, let, or const to avoid creating unintended global variables and to make your code more predictable and maintainable.
Let me know if you have any further questions or need more information.
Shivam Dixit
Codix Sansaar
What happen if we just assign value of any identifier in any function without declare it for sure it not gives error My Doubt : I think I will get consider as in Global Scope even it is in function but why when I accessing it in vscode it not runs function shiv (){ MyId = 45; var MyLoc = 67; … } Console.log(MyLoc) //Error ✅ Console.log(MyId) // Reference (Error) ❌❓❓