Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I was wondering which of the pieces of code below are generally more accepted or maybe even better:
function doStuff(check) {
if(!check) {
return "blablabla";
} else if (check >= 5) {
return "blablabla";
} else {
return "blablabla";
}
}
or
function doStuff(check) {
if(!check) {
return "blablabla";
} else if (check >= 5) {
return "blablabla";
}
return "blablabla";
}
Since the return statement makes the JavaScript interpreter leave the current function it is in I don't see a reason to put an else statement at the end of the second example because the only way to get there is if all previously mentioned comparisons resulted false anyways.
Is this just a case of pick whatever the hell you like but be consistent at it or is there an actual rule for this?
I would even go one step further:
function doStuff(check) {
if(!check)
return "blablabla";
if (check >= 5)
return "blablabla";
return "blablabla";
}
But this is a matter of taste. While I find it better to omit unnecessary else statements, I have also heart colleagues say that you should always put this else there to make the control structure clearer... But in such simple cases I would not bother putting them in.
For numeric values, though, I would prefer check == 0 to !check.
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 months ago.
Improve this question
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.
It's Yoda conditions. Programmers do for null safe.
I try compare strings. If my variable null and I try call method of includes() I will get exception, but if I call includes() for literal it's save from exception.
const someString = null;
// I will get exception
if (someString.includes("literal")) {
}
// Null safe
if ("literal".includes(someString)) {
}
BUT: if you matter know about something variable that it's null, then not need use yoda condition. Try handle this exception.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
I have a Map = {'key - string', value as object} and a List[] and a String 'TEST',
Here I need to check two checking based on the String in the map and the Array,
For Example,
if(Map.has('TEST')) {
Need to do some logic
} else {
if(Array.includes('TEST') {
Need to do some manipulation
} else {
Need to do some manipulation
}
}
I have tried like above to achieve my need. Is it a best practice? Advice me.
if(Map.has('TEST')) {
Need to do some logic
} else if(Array.includes('TEST')) {
Need to do some manipulation
} else {
Need to do some manipulation
}
Use Else If
More Examples
EDIT1: Since you said you Need to check 2 Things Maybe this suits even more but im not sure your Question is very vague.
// || = OR
if(Map.has('TEST') || Array.includes('TEST')) {
Need to do some logic
} else {
Need to do some manipulation
}
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
I have a collection in Javascript/Typescript
[
{"order":1,"step":"abc:","status":true},
{"order":2,"step":"xyz","status":true},
{"order":3,"step":"dec","status":false},
{"order":4,"step":"pqr","status":false},
{"order":5,"step":"tuv","status":false}
....
];
I need to write a function that every time its called it identifies the first occurrence of a false (Order:3 in the above example) and updates it to true. If the above method is called again now the next element (order:4 ) would have been updated. The steps that are false will always be below steps that are completed i.e true.
What's the best way (simplest, less code and elegant) to write this function? I can manually loop through using for each of the items in the collection, check for the first occurrence of false and then update it.
In ES6 you can use this:
yourArray.find((element) => !element.status).status = true;
See find() and its compatibility table.
And note that this will fail if there is no entry with status: false. A quick and dirty fix could for example look like the one below. But that entirely depends on your use case.
(yourArray.find((element) => !element.status) || {}).status = true;
Pretty much what you described is how you would do it:
for (let order of orders) {
if (!order.status) {
order.status = true;
break;
}
}
When you look for the matching occurance you require, then insert a 'break' statement to stop the search.
Example:
for( var i=0; i<recs.length; i++ ) {
if ( recs[i]['status'] == false ) {
recs[i]['status'] = true;
break;
}
}
You can use Lodash find method to find the first occurence of false status.
Sample Code
_.find(users, function(object) { return object.status === false });
lodash find documentation link
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have multiple if statements like this:
if (variable == something && lock01 == false)
{
do your thing
lock01=false; lock02=true; lock03=true; ....
}
else
{
lock01=true; lock02=false; lock03=true;
}
This locks all other if statements so only one is active at a time. The problem with this is if I add more if statements, I have to add more locks by hand. It also only works from top to bottom one after another. Is there a better way to do this? If it's easier in Jquery, JQ code is also ok.
edit: I forgot to write that I loop through the if statements and only one can be unlocked at a time.
One option would be to use a variable that identifies which lock is unlocked:
unlocked = "lock01";
// or
unlocked = 1;
Then your code could look something like this:
if (variable == something && unlocked === 1) {
// do your thing
} else {
unlocked = 2;
}
Probably the best thing to do is to use switch statements instead of "n" if/else statements.
So you code will looks like:
switch(variable) {
case 1:
do your things here;
break;
case 2:
do something else;
break;
case 3:
case 4:
when variable is 3 or 4 do the same code
break;
default:
do something else;
lock = false;
}
and so on.
Switch statements works with strings or numbers (as far as I know), just make sure to use quote with strings (e.g.: case 'something').
If you don't want to use switch statements, then use else/if:
if(variable == something) {
do something;
}
else if(variable == something_else) {
do something_else;
}
else {
lock = false
}
This way only the first matched "if" will be executed.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
If I write this:
$('#SomeDiv').click(DoSomething);
function DoSomething() {
if (SomeCondition === true) {
return false;
}
// more code here
}
regardless of whether I put return; or return false; or return true; the code doesn't throw exceptions and the function execution stops.
Which is the best option?
If those are the only options, use return; in this case. (read below)
If the function normally returns something (calculates something, gets some value, etc) then you definitely don't want to return anything, because you might confuse the caller.
If your function doesn't normally return anything, then it might not hurt to return anything you like, but it might still confuse callers.
I would personally rather just put an else after the if, and not use the return;. And if the function gets too large, just retractor it a bit.
If you just want to stop the function on some condition and don't care what it returns, then it doesn't matter which of the three you choose. If you're not using the output of the function, I'd just use a simple return; statement to stop it executing further.