JavaScript override If else with boolean

0

I have a question in a JavaScript code. I am trying to make a comparison code, however I want to remove the words "true" and "false" and replace with other words that I can put. I'm using If else but does it help?

    JavaScript Comparison

<p> x = 5; y =5 => x == y 
    <script type="text/javascript">
        var x = 5;
        var y = 5;
        document.write(x == y);

        if (x == y) {
            document.write("palavra aleatória");
        } else {
            document.write("palavra aleatória2");
        }
    </script>
</p>

Thank you guys!

    
asked by anonymous 16.06.2017 / 10:51

1 answer

0

Whether it will help or not depends on the context in which this javascript is applied because conditional statements are used to perform different actions based on different conditions.

The if statement is used to specify a block of JavaScript code to run if the condition is true.

The else statement is used to specify a block of JavaScript code to run if the condition is false.

Use the else if statement to specify a new condition if the first condition is false.

Example if else

if(x==y) {
    document.write("palavra aleatória");;
} else {
    document.write("palavra aleatória2");
}

or using ternary notation

    var resultado = (x == y) ? document.write('palavra aleatória') : document.write('palavra aleatória2');

Example if - else if - else

 if(x< y) {
    document.write("palavra aleatória");;
} else if(x == y) {
    document.write("palavra aleatória2");
} else {
    document.write("palavra aleatória3");
}

Often in programming, you will need a data type that can only have one of two values, such as

  • YES NO
  • ON OFF
  • TRUE FALSE (true false)

In this case, you can use the Boolean function to find out whether an expression (or a variable) is true:

document.write(5 == 5);

Note: Uppercase letters in the statements (If, IF, Else etccc) generate a JavaScript error.

    
16.06.2017 / 13:41