13. Not: please explain the curly braces


#1

I’m just curious why both sets of code run. I thought you always need curly braces after a condition. If you don’t need them why do we use them at all here? Thanks!

```

//Curly braces around return true;
var programming=false;
var happy = function() {
if(!programming){
return true;
}
else{
return false;
}
};

//Without curly braces around return true;
var programming = false;
var happy = function() {
if (!programming)
return true;
else return false;

};

<do not remove the three backticks above>

#2

Hi,
For one line statements curly braces are not required, but they recommended in case you decide to add more statements.

Cheers


#3

@dani182ford,

Using your code

var programming = false;
var happy = function() {
    if (!programming)
    return true;
    else return false;
  
};

i am asked to do a console.log(“This false”);
before the return false statement
in the ELSE clause…
How would you write the code…??

Have a read through the Javascript code-convention
http://javascript.crockford.com/code.html

.


#4

Got it, thanks for the help!