Ad
  • Default User Avatar

    from javascript.info

    "
    But if there is a return statement, then the rule is simple:

    If return is called with an object, then the object is returned instead of this.
    If return is called with a primitive, it’s ignored.
    In other words, return with an object returns that object, in all other cases this is returned.
    "

  • Custom User Avatar

    Not sure, but I think beceause as soon as you call the NameMe function you will get the object back that is returned inside NameMe. So it will only get you the name. But if you return nothing you will get "this", so the NameMe object itself.

  • Custom User Avatar

    You have a point and I agree with you about the one-liner problem (even if it's going against what I wrote three years ago).

    Nowadays I don't use bracket with one-liners that can be written on the same line of the if, often for early exits:

      if (name === 'Johnny') return 'Hello, my love!';
    

    Otherwise, I like to use brackets mostly to make all if-statements look the same. Consistency makes things easier to read in my opinion. (And clear rules are easier for a team to follow and for tools to enforce.)

    Refactoring out a function whenever there are a few lines, Uncle Bob-style, is another discussion. :-)

  • Default User Avatar

    I would not say, that it is best practice, it is coding conventions. And in the majority of coding conventions the brackets are demanded.
    I had to work without brackets and I must say since then the code without brackets is much easier to read.
    The main argument is allways, that you may add a line outside the conditional block. In my expierence this never happens. And if it happens your test should fail. And if you have to add lines to a conditional block, it maybe time to refactor a methode which a catchy name.

  • Custom User Avatar

    You misspelled Coffee.

  • Custom User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Custom User Avatar

    Yeah, it's mainly for brevity. Have no idea about performance, but as @Balkoth said, I'm guessing it makes very little difference.

  • Default User Avatar

    It is an alternate - and shorter - way to write an if statement, so it's not faster, it's the same.

  • Custom User Avatar

    Is this way of doing a conditional faster than an if statement?

  • Custom User Avatar

    And it hurts my brain more. That counts for somethingggg right ;-)?

  • Custom User Avatar

    I would even go so far as to say that using the brackets are best practice. It's easy to start out with a one-liner and forget to put in the brackets when you add another line to the conditionaly block.

  • Custom User Avatar

    Brackets are optional for single line if statements (but some people prefer to add them for clarity).

  • Custom User Avatar

    How does this function work without the opening and closing {}'s after the conditional if statement?

  • Custom User Avatar

    This comment is hidden because it contains spoiler information about the solution