Ad
  • Default User Avatar
  • Default User Avatar

    i dont know why peoples are complaining but they forget this part if it works doesnt matter what it is! it works

  • Custom User Avatar

    @Johan it will not generate a syntax error unless there's an else after it...
    else it will do weird stuff, and definitely not what you want...

  • Custom User Avatar

    Nice Easter egg! :]

  • Custom User Avatar

    if () {} else if () {} .. is shortcircuited. If age<14, its whole else branch will be skipped.

    Thank you for your point of view, but you're wrong.

    Now, using the undeclared variable drink is ++UnGood - it will become global. Also, the whole variable is unnecessary; just return $somethingAppropriate;.

    This is not a Best Practice solution.

  • Custom User Avatar

    They're only necessary if there's more than one statement.

    Of course, if you don't have them and add another statement to do more cool stuff, you should not forget to enclose them with curly brackets. Or interestingness happens ( it may not even generate a syntax error ).

  • Custom User Avatar

    thats what i did cause i i had trouble doing a switch case.

  • Default User Avatar

    Hey, I can't understand why you people use
    if(condition)
    do cool stuff;
    else if(condition)

    instead of
    if(condition){
    do cool stuff
    }else if(condition){
    do cool stuf
    }

    I had to unlock the answer, but I can't seem to find anything that explains this. Why don't you use curly brackets {}?

  • Default User Avatar

    Hi! I just want to say this practice is quite bad. Why? Because no matter what all possible cases will be compared. Function is like a crossing of roads - you have to leave it as soon it is possible. Ofcourse with results you want.
    Anyway thanks for your point of view!

  • Custom User Avatar

    As far as I know, not using var will put any declaration in the global scope. So while you wouldn't want to do that on a real production app. Using it for these little solutions isn't necessarily wrong. As you probably don't need to worry about your namespace

    here's an example:
    drink = 'beer'
    function drunk () {
    console.log(drink)
    }
    drunk()
    //'beer'
    function wino () {
    var drink = 'wine';
    console.log(drink)
    }
    wino()
    // 'wine'
    drink
    // 'beer'

  • Custom User Avatar

    why var is not used in this solution?

  • Custom User Avatar

    Dunno if it was a typo or intended, but else if(age => 21) actually means "else if function(age) returning 21 exists" which will always evaluate to true.