Forum Moderators: open

Message Too Old, No Replies

Easy way to convert undefined to false?

         

csdude55

6:41 pm on Aug 1, 2023 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Still playing with GAM here, and I have some fields that I need to specifically be FALSE, not just falsey.

Slots are defined in a loop, and I'm explicitly (and conditionally) defining 10 fields for 10 slots. 18 of those slots are defined as "false".

Is there an easy way to say, "if the variable is undefined, make it false"? Other than explicitly doing 10 of these, I mean?

var[counter] ?= false;

My goal here is to minimize the code to shave off a few microseconds :-)

brotherhood of LAN

7:34 pm on Aug 1, 2023 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



My intuition says if you don't know the inputs then it's undefined behaviour.

That said, typeof x === 'undefined'

So (y = typeof(x === 'undefined' ? false : true)

Fotiman

9:34 pm on Aug 1, 2023 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



EDIT: I'm really struggling with the code editor on Webmaster World. It's removing one of the exclamation points, so where you see DBL! in the code below, that's meant to represent two !.

[mods note: fixed the double ! problem]

There are a couple of ways, but my favorite is with a double NOT operator. The NOT operator (!) will implicitly convert values to a boolean, with the value being the negated version. So a double NOT will give you the original value as an actual boolean. For example:

var isThisFun = 'this is a truthy value';
isThisFun = !!isThisFun; // true

Your syntax looks like you're using an array to hold the values, so you could do something like this:

var[counter] = !!var[counter];

And that would result in only true of false values being assigned.
Another alternative is explicitly using the Boolean function (not as a constructor, but as a regular function):

var[counter] = Boolean(var[counter]);

This is a little more explicit, but I prefer the terseness of the double NOT.

[edited by: phranque at 10:53 pm (utc) on Aug 1, 2023]
[edit reason] fix display of double ! [/edit]

csdude55

3:53 am on Aug 2, 2023 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I'm really struggling with the code editor on Webmaster World. It's removing one of the exclamation points, so where you see DBL! in the code below, that's meant to represent two !.

Haha, same in mine! It was supposed to have 2 question marks.

I forget who it was, but someone told me a trick to get around it. Add a blank tag in between and the system won't realize that it's the same character.

So in my case,

var[counter] ??= false;

Anyway.

Using the double ! is pretty interesting, I hadn't seen that one before! In my case, though, it doesn't work the way I'd need it, because it would then overwrite the variables that have real values.

Meaning, I have something like this now:

variable[0] = 'foo';
variable[2] = 'bar';

for (var i=0; i < variable.length; i++) {
var[i] ??= false;
console.log(variable[i]);
}

This returns "foo", false, "bar". If I use your suggestion I end up with true, false, true. And it doesn't really make the code any shorter, either. I was hoping for a magic trick that would change ALL undefined variables in the function to false, but I guess that's too much to wish for :-/

Fotiman

5:36 pm on Aug 2, 2023 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



In that case, you could use the OR operator:

for (var i=0; i < variable.length; i++) {
variable[i] = variable[i] || false;
console.log(variable[i]);
}

Though personally, I'd prefer the built in array methods for iterating instead of a for loop. In this case, I'd use .map().

variable.map(item => item || false);

You might also look at where you do the original assignment in the array, and just perform this conversion there instead. Note, empty strings are falsy as well, so this wouldn't work if an empty string is a valid condition that you don't want to replace with false.

lucy24

7:12 pm on Aug 2, 2023 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



if the variable is undefined, make it false
Did you at some point say whether any variables can legitimately have values of, say, zero or (empty string)? I'm wondering if it would be simpler to say “if the variable has any value other than {thing it is allowed to have, such as positive number or non-empty string}, set it to false"--including the ones that happen to be "false" already.

csdude55

12:25 am on Aug 3, 2023 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



What I have looks like this:

for (m of [5, 6, 2, 7, 3, 4, 1, 0, 9, 8]) {
switch (m) {
case 5:
unitArr.slot.block[m] = 'foo';
unitArr.size.block[m] = 'bar;
unitArr.channel.block[m] = 12345;
unitArr.collapse.block[m] = true;
unitArr.collapseB.block[m] = false;

unitArr.slot.backup[m] = 'lorem';
unitArr.size.backup[m] = 'ipsum';
unitArr.channel.backup[m] = 98765;
unitArr.collapse.backup[m] = false;
unitArr.collapseB.backup[m] = false;

break;

case 6:
unitArr.slot.block[m] = 'this';
unitArr.size.block[m] = 'that;
unitArr.channel.block[m] = false;
unitArr.collapse.block[m] = false;
unitArr.collapseB.block[m] = false;

unitArr.slot.backup[m] = false;
unitArr.size.backup[m] = false;
unitArr.channel.backup[m] = false;
unitArr.collapse.backup[m] = false;
unitArr.collapseB.backup[m] = false;

// and so on
}
}


The only fields that HAVE to be there are unitArr.slot.block[m] and unitArr.size.block[m], the others are all optional.

The way they're used in GAM is:

slotArr[unit] =
googletag.defineSlot('/12345/6789/' + unitArr.slot.block[unit],
unitArr.size.block[unit],
'block_' + unit)
.setCollapseEmptyDiv(unitArr.collapse.block[unit], unitArr.collapse.block_2[unit])
.addService(googletag.pubads());


My goal here would be to minimize the code by eliminating all of the unnecessary false declarations; eg,

for (m of [5, 6, 2, 7, 3, 4, 1, 0, 9, 8]) {
switch (m) {
case 5:
unitArr.slot.block[m] = 'foo';
unitArr.size.block[m] = 'bar;
unitArr.channel.block[m] = 12345;
unitArr.collapse.block[m] = true;

unitArr.slot.backup[m] = 'lorem';
unitArr.size.backup[m] = 'ipsum';
unitArr.channel.backup[m] = 98765;

break;

case 6:
unitArr.slot.block[m] = 'this';
unitArr.size.block[m] = 'that;

// and so on
}
}


Doing that sends "undefined", though, which throws an error. So I guess that, really, the value will always either be a string or undefined, unless the browser decides to send null? I'm not sure on that one.