There’s deeper subtlety here. There are declared variables, uninitialised variables and variables that will at some point be declared, uninitialised or not, that don’t exist in the logic of the program flow yet. Compilers / interpreters may or may not make distinctions between these, especially if memory is (perhaps only considered to be) in short supply.
What specific languages do in these cases is wide and varied. Some automatically give all variables a safe, but identifiably undefined value the moment they’re declared, which may or may not be equivalent to the language’s interpretation of null. Or is equivalent in some senses but not others. Looking at you, Javascript.
In the toilet paper example, the holder might actually still exist but the middle bar might be missing for “identifiably undefined”, and the blank wall is that variable you have literally no idea about.
Maybe in trying to access it you amaze yourself by finding that you’ve punched through the wall and managed to grab some toilet paper from a neighbouring bathroom cubicle.
Maybe you instead discover your hand goes through a mincing machine and the pain, oh god why the pain nothing will ever be the same, is that my hard drive churning? No way to tell. Now that’s undefined behaviour.
There’s deeper subtlety here. There are declared variables, uninitialised variables and variables that will at some point be declared, uninitialised or not, that don’t exist in the logic of the program flow yet. Compilers / interpreters may or may not make distinctions between these, especially if memory is (perhaps only considered to be) in short supply.
What specific languages do in these cases is wide and varied. Some automatically give all variables a safe, but identifiably
undefined
value the moment they’re declared, which may or may not be equivalent to the language’s interpretation ofnull
. Or is equivalent in some senses but not others. Looking at you, Javascript.In the toilet paper example, the holder might actually still exist but the middle bar might be missing for “identifiably
undefined
”, and the blank wall is that variable you have literally no idea about.Maybe in trying to access it you amaze yourself by finding that you’ve punched through the wall and managed to grab some toilet paper from a neighbouring bathroom cubicle.
Maybe you instead discover your hand goes through a mincing machine and the pain, oh god why the pain nothing will ever be the same, is that my hard drive churning? No way to tell. Now that’s undefined behaviour.