Brackets in Functions Javascript

前端 未结 3 1908
花落未央
花落未央 2021-01-24 13:10

I am having trouble understanding the usage of brackets in \"for\" loops and \"if\" statements in Javascript. I have seen syntax in Javascript where there is brackets and where

3条回答
  •  终归单人心
    2021-01-24 13:44

    Without brackets only the next statement is affected whereas with brackets everything inside the brackets is affected.

    To add brackets to the loop and have it work exactly the same, just add them around the next statement:

    function range(upto) {
        var result = [];
        for (var i = 0; i <= upto; i++) {
            result[i] = i;
        }
        return result;
    }
    

提交回复
热议问题