I want to shorten the conditions of a javascript if but I don’t know how I can achieve it
code:
JavaScript
x
3
1
if ((!emailValidation() || (!nameValidation()) || (!surnameValidation()) || (!addressValidation()) || (!cityValidation()) || (!postalCodeValidation()))) {
2
}
3
I have the conditions defined in this way:
JavaScript
1
20
20
1
let surnameValidation = () => {
2
if (apellidoUsuario.value.length == 0) {
3
surnameError();
4
return false;
5
}
6
7
else if (apellidoUsuario.value.length == 1) {
8
surnameError();
9
return false;
10
}
11
12
else {
13
apellidoUsuario.focus;
14
apellidoUsuario.style.border = '0';
15
apellidoUsuario.style.backgroundColor = 'transparent';
16
apellidoUsuario.style.outline = '1px solid #00ffb1'
17
apellidoUsuario.style.transitionDuration = '0.4s'
18
return true;
19
}
20
I appreciate any help! 🙂
Advertisement
Answer
You can remove all unnecessary parenthesis in your if
condition:
JavaScript
1
10
10
1
if (
2
!emailValidation() ||
3
!nameValidation() ||
4
!surnameValidation() ||
5
!addressValidation() ||
6
!cityValidation() ||
7
!postalCodeValidation()
8
) {
9
}
10
Other than that, there’s not really a clean, readable way to shorten your code.