www.webdeveloper.com
Results 1 to 9 of 9

Thread: [RESOLVED] What true false means in javascript?

Hybrid View

  1. #1
    Join Date
    Jul 2013
    Location
    tokyo
    Posts
    4

    resolved [RESOLVED] What true false means in javascript?

    Hi, I'm a beginner at javascript, and just start to study with a book "Head First javascript", now studying for Looping.

    <script type="text/javascript">
    var seats = [ false, true, false, true ];
    var selSeat = -1;
    function initSeats() {
    for (var i = 0; i < seats.length; i++) {
    if (seats[i]) {
    document.getElementById("seat" + i).src = "seat_avail.png";
    }
    else {
    document.getElementById("seat" + i).src = "seat_unavail.png";
    }
    }
    }

    ----------------------

    seats[i] = (seats[false]), (seats[true]), (seats[false]), (seats[true]). right?

    At this point, I can not understand meaning of true / false.

    Is "seats[true]" means either 1) seats has a value or 2) seats has a value (the name is true)...?? Which one is correct?

    mightbe this question looks silly, but appriciate get some advice.

    Jkim from Tokyo.

  2. #2
    Join Date
    Jul 2013
    Posts
    66
    The expression "seats[true]" doesn't make any sense. I don't see a clear question, but if you're wondering what seats[i] looks like: It's either true or false. For example, seats[0] is false and seats[1] is true.

  3. #3
    Join Date
    Mar 2011
    Posts
    1,134
    In your code, 'seats' is an array of boolean (true/false) variables. The array is initially set to:
    Code:
     seats[0] = false;
     seats[1] = true;
     seats[2] = false;
     seats[3] = true;
    Array elements are most often referred to by their index number, using ordinal values from 0 to the number of elements in the array (seats.length) - 1, as in seats[0], seats[1], etc. as shown above.

    The function initSeats() checks the value of each element in 'seats' using a 'for' loop. If the element is 'true', then the code sets the 'src' attribute of the corresponding <img> element to 'seat_avail.jpg'. If the element is 'false', then the 'src' is set to 'seat_unavail.jpg'.
    Rick Trethewey
    Rainbo Design

  4. #4
    Join Date
    Jul 2013
    Location
    tokyo
    Posts
    4
    Thanks for kindly advice!
    Understood seats[] is just a holder.

    ----------------------------
    if (seats[i]) {
    document.getElementById("seat" + i).src = "seat_avail.png";
    }
    else {
    document.getElementById("seat" + i).src = "seat_unavail.png";
    }
    --------------------------

    In my understanding, seats [i] has not any meaning, just a holder,
    seats[0] = false;
    seats[1] = true;
    seats[2] = false;
    seats[3] = true;

    are all seats[false ,true ,false, true], set as 'seat_avail.png', isn't it? else if seats[-1 or -1 or 4 or 5 or 6...] then show "seat_unavail.png"...

    How script know if [true], show 'seat_avail.png'?
    Where is "if seats [true] then show 'seat_avail.jpg'" and "if seats [false] then show 'seat_unavail.jpg'" code (meaning)?

    I am not sure this question is clear or not to u..

  5. #5
    Join Date
    Jun 2008
    Posts
    106
    True and False are booleans. You should go back to learning the basics before attempting to read more advanced code.

    You can add me on Skype if you want (thetenfold). Leave me a message if I'm not on, I'll answer it.

  6. #6
    Join Date
    Jul 2013
    Posts
    5
    <script type="text/javascript">
    var seats = [ false, true, false, true ];
    var selSeat = -1;
    function initSeats() {
    for (var i = 0; i < seats.length; i++) {
    if (seats[i]) {
    document.getElementById("seat" + i).src = "seat_avail.png";
    }
    else {
    document.getElementById("seat" + i).src = "seat_unavail.png";
    }
    }
    }

  7. #7
    Join Date
    Jul 2013
    Location
    tokyo
    Posts
    4
    Thanks all...I found what boolean is..,

    0
    -0
    null
    false
    NaN

    these are all the same thing as false!!

  8. #8
    Join Date
    Jun 2008
    Posts
    106
    No. I think you're misinterpreting it.

    ONLY True and False are booleans.

    There are "truthy" and "falsy" values, though (I'd Google more on this if you don't understand). So if you evaluate "truthy" values in an expression, they will return true.
    e.g., if (50) alert('50 is truthy'); // will run the alert. 50 is "truthy"
    e.g., if (0) alert('0 is truthy'); // will not run the alert. 0 is "falsy"

    These are the "falsy" values:
    Code:
    0           // the number zero
    ""          // empty string
    NaN         // (Not A Number)
    false       // a boolean
    null
    undefined
    This can be tricky, due to the loosely typed nature of JavaScript.



    When you looped through your array, you evaluated the value of seats[i] in an if () expression.
    It only checked if seats[i] was truthy, not whether it was "true" or not. It works because "true" is a truthy value and "false" is a falsy value.
    But you could re-write it with more clarity, like this (using the triple equals (===) operator to compare identical values):
    Code:
    if (seats[i] === true) {
        // runs if seats[i] is exactly equal to the true boolean
    } else {
        // runs if seats[i] is anything except true, which in your example, is only false
    }

  9. #9
    Join Date
    Jul 2013
    Location
    tokyo
    Posts
    4
    Very thanks!



    Quote Originally Posted by tenfold View Post
    No. I think you're misinterpreting it.

    ONLY True and False are booleans.

    There are "truthy" and "falsy" values, though (I'd Google more on this if you don't understand). So if you evaluate "truthy" values in an expression, they will return true.
    e.g., if (50) alert('50 is truthy'); // will run the alert. 50 is "truthy"
    e.g., if (0) alert('0 is truthy'); // will not run the alert. 0 is "falsy"

    These are the "falsy" values:
    Code:
    0           // the number zero
    ""          // empty string
    NaN         // (Not A Number)
    false       // a boolean
    null
    undefined
    This can be tricky, due to the loosely typed nature of JavaScript.



    When you looped through your array, you evaluated the value of seats[i] in an if () expression.
    It only checked if seats[i] was truthy, not whether it was "true" or not. It works because "true" is a truthy value and "false" is a falsy value.
    But you could re-write it with more clarity, like this (using the triple equals (===) operator to compare identical values):
    Code:
    if (seats[i] === true) {
        // runs if seats[i] is exactly equal to the true boolean
    } else {
        // runs if seats[i] is anything except true, which in your example, is only false
    }

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
  •  
HTML5 Development Center



Recent Articles