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.