Menyingkat if else

You may recall from Comparison with the Equality Operator that all comparison operators return a boolean trueor falsevalue.

Sometimes people use an if/else statement to do a comparison, like this:

function isEqual(a,b) {
if (a === b) {
return true;
} else {
return false;
}
}

But there’s a better way to do this. Since ===returns trueor false, we can return the result of the comparison:

function isEqual(a,b) {
return a === b;
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s