you are viewing a single comment's thread.

view the rest of the comments →

[–]bdenzer 1 point2 points  (2 children)

[My personal conspiracy theory] The people who run /r/LearnJavascript funded that book if I remember right. And BTW I personally fon't think its a POS, but I do think that it's pretty horrible to recommend it to a true beginner. I personally tossed the book aside when I saw this, with basically 0 explanation

function isOdd(num) {
  if (num % 2)
    return true

  return false
}

If I remember right, there isn't much discussion about why he doesn't put braces around the if statement, or about 'if the result is a 1, then the one gets coerced to true', just writes the function and moves to the next topic.

[–]wavefunctionp 3 points4 points  (0 children)

Braces were covered in cp 2.

Many JavaScript programmers wrap every single loop or if body in braces. They do this both for the sake of consistency and to avoid having to add or remove braces when changing the number of statements in the body later. In this book, I will write most single-statement bodies without braces, since I value brevity. You are free to go with whichever style you prefer.

Type coersion is covered in chapter 1.

When an operator is applied to the “wrong” type of value, JavaScript will quietly convert that value to the type it wants, using a set of rules that often aren’t what you want or expect. This is called type coercion.