What is better conditional expressions or a if statement? [duplicate]
This question already has an answer here:
Is <boolean expression> && statement() the same as if(<boolean expression>) statement()?
5 answers
I was reading an article that was talking about conditional javascript but they didn't explain which was better to use. I made a fiddle with the 2 examples.
console.log('start');
const VALUE = true;
const TEST = false;
//test 1
VALUE && !TEST && (() => {
console.log('hello there!');
})();
//test 2
if(VALUE && !TEST) {
console.log('bye');
}
https://jsfiddle.net/xvdLq6to
The most important thing with this example is that I don't want to call a new function I want to execute the code in the function its inside just like an if statement. If you know something better then I can't wait to hear that!
Here is the article (keep in mind that the example is not directly from the article)
https://hackernoon.com/conditional-javascript-for-experts-d2aa456ef67c
javascript if-statement ecmascript-6 conditional
marked as duplicate by Bergi
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 20 '18 at 8:53
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
|
show 5 more comments
This question already has an answer here:
Is <boolean expression> && statement() the same as if(<boolean expression>) statement()?
5 answers
I was reading an article that was talking about conditional javascript but they didn't explain which was better to use. I made a fiddle with the 2 examples.
console.log('start');
const VALUE = true;
const TEST = false;
//test 1
VALUE && !TEST && (() => {
console.log('hello there!');
})();
//test 2
if(VALUE && !TEST) {
console.log('bye');
}
https://jsfiddle.net/xvdLq6to
The most important thing with this example is that I don't want to call a new function I want to execute the code in the function its inside just like an if statement. If you know something better then I can't wait to hear that!
Here is the article (keep in mind that the example is not directly from the article)
https://hackernoon.com/conditional-javascript-for-experts-d2aa456ef67c
javascript if-statement ecmascript-6 conditional
marked as duplicate by Bergi
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 20 '18 at 8:53
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
6
Theif
looks far better IMO, using a construct that results in an expression (including the conditional operator) are probably best used when you need the result of the expression. If you're just using boolean logic, useif
/else
– CertainPerformance
Nov 20 '18 at 8:31
Agreed with @CertainPerformance. I've never seen the "test 1" kind used anywhere, and don't know what its benefit would be.
– RobertAKARobin
Nov 20 '18 at 8:34
3
Personally I wouldn't bother with the syntax shown in test 1. Readability of your code should be higher priority here. Also a tool like an uglifier or minifier can probably generate code pretty similar to test 1 for you automatically while not sacrificing readability.
– Mathyn
Nov 20 '18 at 8:34
1
If you're worried about the function, don't be, just leave it off entirelyVALUE && !TEST && console.log('hello there!');
– CertainPerformance
Nov 20 '18 at 8:38
1
You say they weren't explaining which was better to use, but I can find "you will see how some conditional statements can be converted to simple expressions. You will also see how such conversions can make your code look more compact and cleaner." Of course, many would disagree -if
is much more readable for a statement with a side effect. Yes, they can shorten it, but the article is called "conditionals for experts" not "for minifiers". It only makes sense when you want to produce a value (typically with a ternary).
– Bergi
Nov 20 '18 at 9:10
|
show 5 more comments
This question already has an answer here:
Is <boolean expression> && statement() the same as if(<boolean expression>) statement()?
5 answers
I was reading an article that was talking about conditional javascript but they didn't explain which was better to use. I made a fiddle with the 2 examples.
console.log('start');
const VALUE = true;
const TEST = false;
//test 1
VALUE && !TEST && (() => {
console.log('hello there!');
})();
//test 2
if(VALUE && !TEST) {
console.log('bye');
}
https://jsfiddle.net/xvdLq6to
The most important thing with this example is that I don't want to call a new function I want to execute the code in the function its inside just like an if statement. If you know something better then I can't wait to hear that!
Here is the article (keep in mind that the example is not directly from the article)
https://hackernoon.com/conditional-javascript-for-experts-d2aa456ef67c
javascript if-statement ecmascript-6 conditional
This question already has an answer here:
Is <boolean expression> && statement() the same as if(<boolean expression>) statement()?
5 answers
I was reading an article that was talking about conditional javascript but they didn't explain which was better to use. I made a fiddle with the 2 examples.
console.log('start');
const VALUE = true;
const TEST = false;
//test 1
VALUE && !TEST && (() => {
console.log('hello there!');
})();
//test 2
if(VALUE && !TEST) {
console.log('bye');
}
https://jsfiddle.net/xvdLq6to
The most important thing with this example is that I don't want to call a new function I want to execute the code in the function its inside just like an if statement. If you know something better then I can't wait to hear that!
Here is the article (keep in mind that the example is not directly from the article)
https://hackernoon.com/conditional-javascript-for-experts-d2aa456ef67c
This question already has an answer here:
Is <boolean expression> && statement() the same as if(<boolean expression>) statement()?
5 answers
javascript if-statement ecmascript-6 conditional
javascript if-statement ecmascript-6 conditional
edited Nov 20 '18 at 9:01
asked Nov 20 '18 at 8:30
Rick Grendel
50119
50119
marked as duplicate by Bergi
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 20 '18 at 8:53
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Bergi
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 20 '18 at 8:53
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
6
Theif
looks far better IMO, using a construct that results in an expression (including the conditional operator) are probably best used when you need the result of the expression. If you're just using boolean logic, useif
/else
– CertainPerformance
Nov 20 '18 at 8:31
Agreed with @CertainPerformance. I've never seen the "test 1" kind used anywhere, and don't know what its benefit would be.
– RobertAKARobin
Nov 20 '18 at 8:34
3
Personally I wouldn't bother with the syntax shown in test 1. Readability of your code should be higher priority here. Also a tool like an uglifier or minifier can probably generate code pretty similar to test 1 for you automatically while not sacrificing readability.
– Mathyn
Nov 20 '18 at 8:34
1
If you're worried about the function, don't be, just leave it off entirelyVALUE && !TEST && console.log('hello there!');
– CertainPerformance
Nov 20 '18 at 8:38
1
You say they weren't explaining which was better to use, but I can find "you will see how some conditional statements can be converted to simple expressions. You will also see how such conversions can make your code look more compact and cleaner." Of course, many would disagree -if
is much more readable for a statement with a side effect. Yes, they can shorten it, but the article is called "conditionals for experts" not "for minifiers". It only makes sense when you want to produce a value (typically with a ternary).
– Bergi
Nov 20 '18 at 9:10
|
show 5 more comments
6
Theif
looks far better IMO, using a construct that results in an expression (including the conditional operator) are probably best used when you need the result of the expression. If you're just using boolean logic, useif
/else
– CertainPerformance
Nov 20 '18 at 8:31
Agreed with @CertainPerformance. I've never seen the "test 1" kind used anywhere, and don't know what its benefit would be.
– RobertAKARobin
Nov 20 '18 at 8:34
3
Personally I wouldn't bother with the syntax shown in test 1. Readability of your code should be higher priority here. Also a tool like an uglifier or minifier can probably generate code pretty similar to test 1 for you automatically while not sacrificing readability.
– Mathyn
Nov 20 '18 at 8:34
1
If you're worried about the function, don't be, just leave it off entirelyVALUE && !TEST && console.log('hello there!');
– CertainPerformance
Nov 20 '18 at 8:38
1
You say they weren't explaining which was better to use, but I can find "you will see how some conditional statements can be converted to simple expressions. You will also see how such conversions can make your code look more compact and cleaner." Of course, many would disagree -if
is much more readable for a statement with a side effect. Yes, they can shorten it, but the article is called "conditionals for experts" not "for minifiers". It only makes sense when you want to produce a value (typically with a ternary).
– Bergi
Nov 20 '18 at 9:10
6
6
The
if
looks far better IMO, using a construct that results in an expression (including the conditional operator) are probably best used when you need the result of the expression. If you're just using boolean logic, use if
/ else
– CertainPerformance
Nov 20 '18 at 8:31
The
if
looks far better IMO, using a construct that results in an expression (including the conditional operator) are probably best used when you need the result of the expression. If you're just using boolean logic, use if
/ else
– CertainPerformance
Nov 20 '18 at 8:31
Agreed with @CertainPerformance. I've never seen the "test 1" kind used anywhere, and don't know what its benefit would be.
– RobertAKARobin
Nov 20 '18 at 8:34
Agreed with @CertainPerformance. I've never seen the "test 1" kind used anywhere, and don't know what its benefit would be.
– RobertAKARobin
Nov 20 '18 at 8:34
3
3
Personally I wouldn't bother with the syntax shown in test 1. Readability of your code should be higher priority here. Also a tool like an uglifier or minifier can probably generate code pretty similar to test 1 for you automatically while not sacrificing readability.
– Mathyn
Nov 20 '18 at 8:34
Personally I wouldn't bother with the syntax shown in test 1. Readability of your code should be higher priority here. Also a tool like an uglifier or minifier can probably generate code pretty similar to test 1 for you automatically while not sacrificing readability.
– Mathyn
Nov 20 '18 at 8:34
1
1
If you're worried about the function, don't be, just leave it off entirely
VALUE && !TEST && console.log('hello there!');
– CertainPerformance
Nov 20 '18 at 8:38
If you're worried about the function, don't be, just leave it off entirely
VALUE && !TEST && console.log('hello there!');
– CertainPerformance
Nov 20 '18 at 8:38
1
1
You say they weren't explaining which was better to use, but I can find "you will see how some conditional statements can be converted to simple expressions. You will also see how such conversions can make your code look more compact and cleaner." Of course, many would disagree -
if
is much more readable for a statement with a side effect. Yes, they can shorten it, but the article is called "conditionals for experts" not "for minifiers". It only makes sense when you want to produce a value (typically with a ternary).– Bergi
Nov 20 '18 at 9:10
You say they weren't explaining which was better to use, but I can find "you will see how some conditional statements can be converted to simple expressions. You will also see how such conversions can make your code look more compact and cleaner." Of course, many would disagree -
if
is much more readable for a statement with a side effect. Yes, they can shorten it, but the article is called "conditionals for experts" not "for minifiers". It only makes sense when you want to produce a value (typically with a ternary).– Bergi
Nov 20 '18 at 9:10
|
show 5 more comments
1 Answer
1
active
oldest
votes
The second example should be the preferred syntax, by far. Readability is incredibly important and the second example is considerably more readable, it's easier to understand the intention of the code.
The first example, while technically possible, is simply not good code, in my opinion.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The second example should be the preferred syntax, by far. Readability is incredibly important and the second example is considerably more readable, it's easier to understand the intention of the code.
The first example, while technically possible, is simply not good code, in my opinion.
add a comment |
The second example should be the preferred syntax, by far. Readability is incredibly important and the second example is considerably more readable, it's easier to understand the intention of the code.
The first example, while technically possible, is simply not good code, in my opinion.
add a comment |
The second example should be the preferred syntax, by far. Readability is incredibly important and the second example is considerably more readable, it's easier to understand the intention of the code.
The first example, while technically possible, is simply not good code, in my opinion.
The second example should be the preferred syntax, by far. Readability is incredibly important and the second example is considerably more readable, it's easier to understand the intention of the code.
The first example, while technically possible, is simply not good code, in my opinion.
answered Nov 20 '18 at 8:36
Flater
7,14732443
7,14732443
add a comment |
add a comment |
6
The
if
looks far better IMO, using a construct that results in an expression (including the conditional operator) are probably best used when you need the result of the expression. If you're just using boolean logic, useif
/else
– CertainPerformance
Nov 20 '18 at 8:31
Agreed with @CertainPerformance. I've never seen the "test 1" kind used anywhere, and don't know what its benefit would be.
– RobertAKARobin
Nov 20 '18 at 8:34
3
Personally I wouldn't bother with the syntax shown in test 1. Readability of your code should be higher priority here. Also a tool like an uglifier or minifier can probably generate code pretty similar to test 1 for you automatically while not sacrificing readability.
– Mathyn
Nov 20 '18 at 8:34
1
If you're worried about the function, don't be, just leave it off entirely
VALUE && !TEST && console.log('hello there!');
– CertainPerformance
Nov 20 '18 at 8:38
1
You say they weren't explaining which was better to use, but I can find "you will see how some conditional statements can be converted to simple expressions. You will also see how such conversions can make your code look more compact and cleaner." Of course, many would disagree -
if
is much more readable for a statement with a side effect. Yes, they can shorten it, but the article is called "conditionals for experts" not "for minifiers". It only makes sense when you want to produce a value (typically with a ternary).– Bergi
Nov 20 '18 at 9:10