Does anyone know why this is happening? Is it a weird thing with scope?
Here is a link to the project for people: http://share.framerjs.com/kgas6ylwbkb7/
I figured out my error. It turns out that layer.subLayers will never return null it will return an empty array. I instead had to check the array's length.
Protip: open the web inspector, go to debugger and enable "Breakpoints > Uncaught Exceptions". Now, if you reach the point of your crash, it will jump into the debugger and you can inspect all the values of your variables.