style bug example

why use braces in javascript

by Terrance Smith

HTML

<input type="button" id="tester" value="Fire!!!" onclick="test();" />

JavaScript

var test=function(){
    if (1 === 1) {
        alert("Case 1");
    }
    /*This is why you use braces in all if statements */
    if (1 === 2)
        alert("Case 2a");
        //this will fire regardless
        alert("Case 2b");
    
    /*javascript sees this as what follows*/
    if(1===2){
        alert("Case 2a");
    }
    alert("Case 2b");
    
    if(1===1){
       alert("!1");
       alert("!2");
    }
       
    

};