Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Nov 2007
    Posts
    71
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Small script - bugging me?

    <body>
    <script type="text/javascript">
    var name = "john";
    var a = (name=="jOhn") ? "true" : "false";

    if (a) {
    alert(a);
    }

    </script>

    </body>

    for both true or false conditions, I get an alert message. Variable a is "false". What is wrong? I want to process only when condition is true. How can I achieve this different way? Thanks.
    Edit/Delete Message

  • #2
    New Coder
    Join Date
    Aug 2006
    Posts
    54
    Thanks
    3
    Thanked 0 Times in 0 Posts
    In your condition:
    var a = (name=="jOhn") ? "true" : "false";

    You have used "true" : "false". Both of these are string and not boolean variables which i think you intended.. Thats why javascript is showing the alert.

    Replace the strings with booleans and its done.
    var a = (name=="jOhn") ? true : false;


  •  

    Posting Permissions

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