Results 1 to 11 of 11

Thread: Tutorial: If and Else Statements in JS

  1. #1

    Tutorial: If and Else Statements in JS

    Hi everyone,
    This is the happening place to discuss all things related to the If and Else Statements in JS tutorial.

    Cheers,
    Kirupa

    Great, now even Kirupa is { facebooking | twittering }

  2. #2

    Afrostyle

    Please add some notes on triple equal '==='.
    Quote Originally Posted by Crockford
    === and !== Operators.

    It is almost always better to use the === and !== operators. The == and != operators do type coercion. In particular, do not use == to compare against falsy values.
    I want triple equals .

    Does ternary operator includes here? Or that can be another tutorial on conditionals.

  3. #3
    Here's another case where I disagree with Crockford. I know exactly how == and === differ, have known for years, and still find that == is almost always what I want and intend. However, I wouldn't try to convince people to use one over the other like he has (in my opinion, unfortunately) done.

  4. #4
    So you are saying that there is no point in all the javascript conventions written by Crockford? And Ecma-262 5.1 Edition is the holy grail of JavaScript and there is no point in JavaScript The Good Parts by Crockford?

  5. #5
    Nope, I'm just disagreeing with this one piece of his advice. (I said 'another' because there was another of his pieces of advice mentioned recently in a different thread.) I'm not saying anything as sensational as you're implying.

  6. #6
    Maybe it's good to point out that there are ways to write 'if' and 'if/else' statements using shorter syntax.
    Not that I recommend to use them, as they are hard to read, but it's a good thing to know what these statements do...
    A few examples:

    Short IF
    Code:
    if (variable_1 == 80) alert("something");
    Short IF
    Code:
    (variable_1 == 80) && alert("something");
    Short OR
    Code:
    variable_1 = some_property || 0;
    Short if/else
    Code:
    alert( (variable_1==80) ? 'eighty' : 'not eighty' );
    Short if/else
    Code:
    variable_1 = some_property ? 1 : 0;

  7. #7
    TheCanadian's Avatar
    10,250
    posts
    Noo doot aboot it, eh?
    Quote Originally Posted by Krilnon View Post
    Nope, I'm just disagreeing with this one piece of his advice. (I said 'another' because there was another of his pieces of advice mentioned recently in a different thread.) I'm not saying anything as sensational as you're implying.
    But using the strict equality operator is best practice
    Proud Montanadian
    We tolerate living and breathing. And niches.

    Name Brand Watches

    Maybe getTimer() or TweenMax is the answer to your problem . . .

  8. #8
    Quote Originally Posted by Swooter View Post
    Maybe it's good to point out that there are ways to write 'if' and 'if/else' statements using shorter syntax.
    Not that I recommend to use them, as they are hard to read, but it's a good thing to know what these statements do...
    A few examples:

    Short IF
    Code:
    if (variable_1 == 80) alert("something");
    Short IF
    Code:
    (variable_1 == 80) && alert("something");
    Short OR
    Code:
    variable_1 = some_property || 0;
    Short if/else
    Code:
    alert( (variable_1==80) ? 'eighty' : 'not eighty' );
    Short if/else
    Code:
    variable_1 = some_property ? 1 : 0;
    That is a great suggestion! I've added a note to the end of the tutorial with a pointer to your post

    Great, now even Kirupa is { facebooking | twittering }

  9. #9
    TheCanadian's Avatar
    10,250
    posts
    Noo doot aboot it, eh?
    I think it would make sense to say in this tutorial that the condition for an if statement doesn't necessarily need to be a comparison between two operands. It can be any expression that evaluates to a boolean. It would then probably make sense to say what different literal values and complex objects resolve to when coerced.

    Quote Originally Posted by Krilnon View Post
    Nope, I'm just disagreeing with this one piece of his advice. (I said 'another' because there was another of his pieces of advice mentioned recently in a different thread.) I'm not saying anything as sensational as you're implying.
    I just read some stuff by him and I disagree with a lot of it, mostly because I think a lot of it has no reason and is just an attempt to create an arbitrary convention. Not that following a standard is bad per se, he just makes it seem like what he's saying is absolute truth. But I may be misinterpreting the tone of the article.
    Proud Montanadian
    We tolerate living and breathing. And niches.

    Name Brand Watches

    Maybe getTimer() or TweenMax is the answer to your problem . . .

  10. #10
    Quote Originally Posted by TheCanadian
    It can be any expression that evaluates to a boolean.
    It can be any expression. And any expression can be evaluated and converted to be a boolean value. (This might have been exactly what you were saying in your post, but the way that I read your post seemed to imply that there are some expressions that you couldn't use in an if statement.)

    I agree that the additional explanation that you suggested for the tutorial would be useful.

    Quote Originally Posted by TheCanadian
    he just makes it seem like what he's saying is absolute truth. But I may be misinterpreting the tone of the article.
    I interpret his tone in the same way!

  11. #11
    TheCanadian's Avatar
    10,250
    posts
    Noo doot aboot it, eh?
    Quote Originally Posted by Krilnon View Post
    It can be any expression. And any expression can be evaluated and converted to be a boolean value.
    Yeah, that's what I was trying to say
    Proud Montanadian
    We tolerate living and breathing. And niches.

    Name Brand Watches

    Maybe getTimer() or TweenMax is the answer to your problem . . .

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012