Why does “break;” not allow me to exit my loop like it should?

后端 未结 3 1785
死守一世寂寞
死守一世寂寞 2021-01-29 16:33

I\'m trying to learn javascript and am creating a rudimentary hangman game. However, I keep getting an error when trying to break out of my loop. I can\'t figure it out for the

3条回答
  •  抹茶落季
    2021-01-29 16:42

    Without seeing the rest of the code to understand the scope I would assume that you are using break incorrectly. Instead you should be using return.

    Break is used inside loops (for, while, for in, switch etc). Where as return is used to return from functions, if blocks and can either return a value or not.

    if (guess === null) {
        return;

提交回复
热议问题