I'm curious about, why some programers compares before the value than the variable ?
For example, if I want to create a simple if, I'll do something like this:
const foo = 'success';
if(foo === 'success') {
console.log('works fine!');
} else {
console.log('don\'t work');
}
BUT I've seen some programers that do that:
const foo = 'success';
// Note the position of the success value!
if('success' === foo) {
console.log('works fine!');
} else {
console.log('don\'t work');
}
I want to know which way is better and WHY ?
Thanks.