Now, what happens if you want to evaluate more than one true and false condition in a conditional; for example, suppose you want to ask if a person is of legal age and is a man or a woman:
edad = 21
mujer = false
hombreAdulto = false
De momento nosotros tenemos lo siguiente:
if(edad >= 18) {
hombreAdulto = true
} else {
hombreAdulto = false
}
But how can we say that we also need the person to be a man, for example; for that we can use the logical and operator in JavaScript and it is also supported in any programming language that we are going to see in this course but in some I change its definition a little; That is, for example, to represent the and operator in JavaScript would be to place the sign known as et twice; that is && and we have something like the following:
if(edad >= 18 && mujer == false) {
hombreAdulto = true
} else {
hombreAdulto = false
}
hombreAdulto
And here we are asking that the age be between 18 and 18 and that the person is a man, therefore it is enough for one of them to be false if the previous condition is not met.
On the other hand, if we want to make a structure a little more flexible and we are simply interested in him being a man or of legal age; as you can infer from the context, if only one of them is true, it is enough for the condition to be met; for that we have the or operator in JavaScript or in any programming language; It is represented by the character ||:
if(edad >= 18 || mujer == false) {
hombreOAdulto = true
} else {
hombreOAdulto = false
}
hombreOAdulto
Complex conditions
Now, what happens if you need to make more complex conditions; you can perfectly define as many logical operators as you need; for example:
edad = 21
mujer = false
hombreOAdulto = false
terceraEdad = false
if(edad >= 18 || mujer == false && terceraEdad) {
hombreOAdulto = true
} else {
hombreOAdulto = false
}
hombreOAdulto
Develop with Laravel, Django, Flask, CodeIgniter, HTML5, CSS3, MySQL, JavaScript, Vue, Android, iOS, Flutter