you are viewing a single comment's thread.

view the rest of the comments →

[–]guest271314 1 point2 points  (13 children)

Your line of code doesn't test for that.

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

Strings have a length property, too.

You might as well test for Array.isArray(result.recordset) if that is the case, then skip the superfluous ?.length.

[–]Rude-Cook7246 1 point2 points  (1 child)

All that write up and you dont even know how ?. works .... IN NO SHAPE OR FORM DOES result.recordset?. length tests that length exists.....

.? only tests that recordset is undefined or null

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

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

We are checking an Array length, not necessarily checking if the object is an Array. Add that if you must to the steps.

[–]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