How to use if-else condition in arrow function in JavaScript?

An arrow function can simply be seen as a concise version of a regular function, except that the return is implied (among a few other subtle things you can read about here). One nice way to use an if/else is though a ternary. Take this regular function:

function(a){
    if(a < 10){
        return 'valid';
    }else{
        return 'invalid';
    }
}

The equivalent in an arrow function using a ternary is:

a => (a < 10) ? 'valid' : 'invalid'

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)