0

This is not a problem, just personal curiosity.

I've found a weird edge-case and I don't know if my brain is messing up or if JavaScript simply lacks this. From an SQL-Query that returns columns of Strings that can also be null, I want to put them into an array. However, I want those arrays to be empty when the strings are null.

If I do the following, the arrays will contain null as entry instead of staying empty:

// Column A and B can be either a String or null
var sqlColumnA = sqlResults[i][1];
var sqlColumnB = sqlResults[i][2];

// Store them within arrays of an objects ONLY IF they are a String
var obj = {
    a : [sqLColumnA],
    b : [sqlColumnB]
};

To bypass this, I'd have to use a ternary operator like this:

sqlColumnA === null ? [sqlColumnA] : []

Obviously it works, but it got me thinking. Is there something like Nothing in JavaScript? Where a variable isn't just null, but literally empty? Or will I not be able to bypass the ternary operator solution?

Selbi
  • 813
  • 7
  • 23
  • check out undefined – StateLess Aug 10 '17 at 11:08
  • 1
    you're going to end up with a single element array which has the value `undefined`, or `null` - there is no way to avoid the ternary (or some variation thereof) – Jamiec Aug 10 '17 at 11:09
  • 1
    _“Where a variable isn't just null, but literally empty?”_ - null is the explicit, intentional absence of any value. What “literally empty” would be supposed to mean, you would have to define first. – CBroe Aug 10 '17 at 11:09
  • do you get the value `null` or a string with the value `'null'`? – Nina Scholz Aug 10 '17 at 11:11
  • No, JS doesn't have a `Maybe` data type like in Haskell which can take only two values like `Just data` or `Nothing`. So what you do is what you can do as far as JS can get. – Redu Aug 10 '17 at 16:58
  • @CBroe My definition of "literally empty" would be that such a variable would return absolutely nothing and thus produce an array of length 0 when put into one: `[literallyEmptyVariable] == []` – Selbi Aug 11 '17 at 17:43

5 Answers5

1

If you want an actually empty (as in zero length) array you will need the ternary operator (or similar syntax) as per your question. There is no way to avoid it.


In actual fact javascript has at least 2 way to define "not there" - Both null and undefined.

Jamiec
  • 133,658
  • 13
  • 134
  • 193
  • You don't need the ternary operator. You could use the shortcut with `||` as pointed out here: https://stackoverflow.com/a/4576915/457268 – k0pernikus Aug 10 '17 at 11:13
  • 3
    @k0pernikus the problem with that example is it wont work here as `[null]` is truthy (has a length of 1) so the ternary is the shorter syntax in this rare case – Jamiec Aug 10 '17 at 11:16
1

A different approach could be to use .filter(Boolean) on the array to get rid of all falsey values (null, undefined, '', NaN, 0 and blanks)

var arr = [null, 1, 2, 3, '', undefined, 'a', NaN,,, 0, 'b', {}];

console.log(arr.filter(Boolean));
Christopher Moore
  • 3,071
  • 4
  • 30
  • 46
0

Variables can be undefined.

void 0 example.

Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
Emil S. Jørgensen
  • 6,216
  • 1
  • 15
  • 28
  • From the linked question's answer: `The void operator is often used merely to obtain the undefined primitive value, usually using “void(0)” (which is equivalent to “void 0”). In these cases, the global variable undefined can be used instead (assuming it has not been assigned to a non-default value).` – Jamiec Aug 10 '17 at 11:13
0

Yes, there is a 'nothing' and it's called undefined.

var empty;
console.log(empty);

Output: undefined.

https://jsfiddle.net/96h7vn2z/

Musa
  • 406
  • 3
  • 11
0

You could just use values, which are not null or not 'null' as string.

console.log([null, 'null', '', 'value'].map(v => v !== null && v !== 'null' ? [v] : []));
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392