From "undefined is not a function" to machine code
Be careful not to reset your computer when calling functions!

The search “undefined is not a function” has 320 million results in Google, don’t feel alone if you ever got this error.
There are even memes about it.
Javascript engine to the rescue
After searching and figuring out what the problem was, you probably learned the reason. You were trying to call something that did not exist.
Try running the following in the browser console:
const seldon = {};
sheldon.shutUp();
We get “undefined is not a function” because the property shutUp
does not exist in the sheldon
object. Luckily, the Javascript engine caught the error. But, did you ever wonder:
What would happen if the engine didn’t catch the error?
On our way to machine code
The Javascript engine is a program that compiles the Javascript code and then executes it. Along the way, the engine performs some improvements and catches errors.
But, what would happen if the engine turned that piece of code sheldon.shutUp()
into machine code and executed it?
sheldon.shutUp()
in machine code looks very similar to just calling shutUp(sheldon)
. In machine code, there are no such things as objects and methods.
And shutUp(...)
means calling a function. Calling a function means moving to another location in memory: jumping to the address where the function is stored.
For example, if we have a function called explain
and this function lives in the memory location 1068, then the machine code for explain()
is similar to JUMP 1068
.
But shutUp
does not exist, therefore has no address in memory. So maybe the machine code of shutUp()
is something like JUMP <void>
or maybe JUMP 0
.
What does it mean to JUMP 0
?
If the CPU executes the instruction JUMP 0
, it would then start reading the instruction in RAM address 0. Whatever it’s there. Maybe there are the booting instructions of your operating system, which means that your computer would reboot.
Think about it next time you want to call a function; make sure you don’t end up rebooting your computer, or even worse, your user’s computer.
PS: No worries, that has never happened 🤣