A function for checking the values of JS. Is there a standard or how to write?

I have Fieldname_1 - Fieldname_5
Need to check if the field is filled, if not, return 1 if Yes, then return the value entered.
In the end, option1 must be either 1, or the entered value.
var option1 = checkValue1(fieldname1);

 function checkValue1(checkFieldname){
 if (!checkFieldname) {
 var opt1 = 1;
 } else {
 return checkFieldname;
}
 }


While working the solution to "bad code "
if (!fieldname22) {
 var opt1 = 1;
 } else {
 var opt1 = fieldname22;
}
 if (!fieldname45) {
 var opt3 = 0;
 } else {
 var opt3 = fieldname45*ed;
}
 if (!fieldname47) {
 var opt4 = 0;
 } else {
 var opt4 = fieldname47*ed;
 }

Some data should be 0, any 1.. In fact wanted to create two functions for these purposes.
March 23rd 20 at 18:53
2 answers
March 23rd 20 at 18:55
Solution
If we are undefined not satisfied with null, 0 and "" (which is implicitly cast to false), you usually use such pattern:
const value = to currentvalue || defaultValue;
For example:
const productCategory = categoryInput.value || "N/A";
c [] won't work because the arrays in a Boolean context to true - evert commented on March 23rd 20 at 18:58
[] is not a falsy value, they only 6: ", 0, null, undefined, NaN, false - Paris commented on March 23rd 20 at 19:01
@evert, @Paris , Yes, you're right, corrected. - Kelly_Veum commented on March 23rd 20 at 19:04
I have a sense that the more these values some be combined (them 0 do), and some are multiplied, their 1. :) - cheyanne.Renner commented on March 23rd 20 at 19:07
@cheyanne.Renner, not clear to your question) This will go? (based on your example above)
var opt1 = fieldname22 || 1;
var opt3 = fieldname45 * ed || 0;


If fieldname45 is undefined, then the multiplication will give NaN -> false and will be our default - 0. Of course, you can wrap in function setValue(value, defaultValue), but does it make sense?... - Kelly_Veum commented on March 23rd 20 at 19:10
@Kelly_Veum, o may gat ))))))))) , how trite and simple
Thank you friend! Now my code is more beautiful :) - cheyanne.Renner commented on March 23rd 20 at 19:13
@cheyanne.Renner, please (= But please note that if you have a valid value that can be casted to false (as in the answer wrote, for example 0), a default different from "empty" value, then this method will not work properly. - Kelly_Veum commented on March 23rd 20 at 19:16
@Kelly_Veum, what you need. It works! Function is not needed. - cheyanne.Renner commented on March 23rd 20 at 19:19
March 23rd 20 at 18:57
A lot of options on the Internet is.
// Simple version
function isEmpty(str) {
 return (!str || 0 === str.length);
}

// More fun option
function isBlank(str) {
 return (!str || /^\s*$/.test(str));
}

Find more questions by tags JavaScript