Home > Software design >  Is there a type of nothing type in javascript?
Is there a type of nothing type in javascript?

Time:11-13

Is there any data types of nothing available in javascript? I mean like not the null or undefined or 'empty string' but pure nothing. For example, i want to print a variable to console; console.log(variable) it should prints nothing. Is there something like that? Because i needed in do operations in array. Like [x === true ? "Script": `!this should be nothing not empty string but nothing!`] if i print that array i want to see just empty array not null or undefined in that array.

CodePudding user response:

No, there is not. The undefined value comes closes to what you want, but an array with a value is still distinguishable from an array without a value.

You'll need to write

x === true ? ["Script"] : []

instead.

CodePudding user response:

Based on your actual problem, you can use the spread operator (assuming you have multiple values):

To answer your question, however -- no, that does not exist. The closest you can get to that is undefined which exists when trying to access "undefined" JSON properties (try doing window.x in inspect element)

function printOutArrayOptionally(flag) {
  return [...(flag ? ["Value"] : [])]
}

console.log(printOutArrayOptionally(true))
console.log(printOutArrayOptionally(false))

  • Related