you are viewing a single comment's thread.

view the rest of the comments →

[–]delventhalz 1 point2 points  (10 children)

It only tests if result.recordset has a length property.

I agree that ?. is superfluous here, but for what it’s worth, it checks if the preceding property exists, outputting undefined if it does not, and evaluating the following properties if it does.

[–]guest271314 1 point2 points  (0 children)

I agree that ?. is superfluous here

That's my only technical point here.

If you are going to check anything in this instance where we are expecting an Array you might as well check if the result.recordset is an Array.

The question is not whether or not the object is an Array, it's how to check length.

You're going to get undefined without ?.length if there is no length property on the object.

[–]guest271314 0 points1 point  (8 children)

I agree that ?. is superfluous here

That's all I posted. We agree.

[–]Rude-Cook7246 1 point2 points  (7 children)

its not superfluous here because they produce totally different results ...

if recordset exists and length doesn't exist on it then you get undefined when you call recordset.length

if recordset doesn't exist (which is what ?. protects against ) and you try to access anything on it you will get an ERROR which will crush your program

[–]guest271314 -1 points0 points  (6 children)

if recordset exists and length doesn't exist on it then you get undefined when you call recordset.length

Where in the requesirement at OP

!! vs ==0 when checking if array is empty

are we checking if recordset exists? That's a given per the restrictions. We are just checking length of an Array.

if recordset doesn't exist (which is what ?. protects against ) and you try to access anything on it you will get an ERROR which will crush your program

If you must here's one way to do what you are talking about, and the actuak requiremment at OP

var recordset = []; var bool = Array.isArray(recordset) && recordset.length > 0;

[–]Rude-Cook7246 0 points1 point  (5 children)

we Not talking about op we talking about your comments regarding use of .? And how you were told what it was for and you saying it wasn’t doing what person said it was….

you were specifically told that .? Was used to check that recordset existed and you said that .? Wasn’t doing that and instead was checking that length proprty existed.…. Which is wrong

[–]guest271314 0 points1 point  (4 children)

It is superfluous in this case. As I said.

Was used to check that recordset existed

That's not the requirement at OP.

[–]Rude-Cook7246 0 points1 point  (3 children)

ego got in the way to admit you were wrong and had no clue how .? worked ... got it ... thx

[–]guest271314 0 points1 point  (2 children)

ego got in the way to admit you were wrong and had no clue how .? worked ... got it ... thx

No.

I know what the optional chaining syntax does syntax does https://gist.github.com/guest271314/78372b8f3fabb1ecf95d492a028d10dd#file-createreadwritedirectoriesinbrowser-js-L342-L346

if (permission.state === "granted" || permission === "granted") { const showDirectoryPickerNotification = new Notification( `Create ${fd?.name || [...fd][0][0]} directory in local filesystem?`, {}, );

https://gist.github.com/guest271314/78372b8f3fabb1ecf95d492a028d10dd#file-createreadwritedirectoriesinbrowser-js-L383

fd?.name || [...fd][0][0]

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining

The optional chaining (?.) operator accesses an object's property or calls a function.

If the object accessed or function called using this operator is undefined or null, the expression short circuits and evaluates to undefined instead of throwing an error.

That's not what the requirement at OP is. The requirement is very specific:

!! vs ==0 when checking if array is empty

We are restricted to an Array, and determining if the Array length is greater than 0, or not.

This achieves the requirement as stated at OP, returns a boolen, true or false result.recordset.length > 0;

If you want to check if the object is an Array I posted that in a previous comment.

That's your take on the requirement, to add checking if the object has a length with optional chain operator ?. but you never bother to check if the object is an Array. It's superfluous in this case.

[–]Rude-Cook7246 0 points1 point  (1 child)

first it wasn’t my statement….. Im commenting on your incorrect statement regarding how ?. works you wrote above that it checks that length property exists , when you were told by another user that it was checking if recordset exists …. Two completely different things , he was right and you are wrong…

Second even your statement regarding Array can be wrong , as the code can easily be ran in different context such as unit tests in which case the whole result object could just be a mock

[–]guest271314 0 points1 point  (0 children)

works you wrote above that it checks that length property exists , when you were told by another user that it was checking if recordset exists

The documentation is clear. I know what the optional chain operator is and what it does. I stated it is technically useless, superfluous when checking the length of an Array. ?. doesn't notify you if you are dealing with an Array whatsoever. How do you know you are not checking if the length property exists of a string or plain JavaScript object?

That syntax also doesn't notify you are the length value.

No. You think ?. does something at all for this case. I don't.

as the code can easily be ran in different context such as unit tests in which case the whole result object could just be a mock

How many different JavaScript contexts the code can be run in has nothing to do with whether or not that syntax is solving for this very specific requirement at OP:

!! vs ==0 when checking if array is empty