-7

I tried to initialize an empty array in JavaScript but it return something in it. then I tried with typescript it allow to initialize empty array. but I want it in JavaScript so how can I do?

Can anyone help?

Thanks in advance.

javascript code:

var emptyArr = [];
var emptyString = "";
var emptyNumber;
var numberWithZeroValue = 0;
var emptyBoolean = false;
console.log("emptyArr: " + !!emptyArr);
console.log("emptyString: " + !!emptyString);
console.log("emptyNumber: " + !!emptyNumber);
console.log("numberWithZeroValue: " + !!numberWithZeroValue);
console.log("emptyBoolean: " + !!emptyBoolean);

output:

emptyArr: true        //it returning somthing in it.
emptyString: false
emptyNumber: false
numberWithZeroValue: false
emptyBoolean: false

javascript does't allow to declare type of variable so I tried with typescript.

typescript code:

var emptyArr: Array;
var emptyString: String = "";
var emptyNumber: Number;
var numberWithZeroValue: Number = 0;
var emptyBoolean: Boolean = false;

console.log("emptyArr: " + !!emptyArr);
console.log("emptyString: " + !!emptyString);
console.log("emptyNumber: " + !!emptyNumber);
console.log("numberWithZeroValue: " + !!numberWithZeroValue);
console.log("emptyBoolean: " + !!emptyBoolean);

output:

emptyArr: false     //it returning empty array.
emptyString: false
emptyNumber: false
numberWithZeroValue: false
emptyBoolean: false

Note: typescript return an array is empty but javascript return something in it.

Update: I don't want to check with array.length because my variable is any of type like String, Number, Array, Boolean, etc. I want to check it like return !!myVariable

Vipul Solanki
  • 313
  • 1
  • 2
  • 19

4 Answers4

4

You did create an empty Array, no problems with your creation code.

An Array in Javascript is considered empty if it holds no elements.

You check for an empty array in Javascript like this:

console.log("emptyArr: " + !!emptyArr.length);

let emptyArr = []; // or new Array();
console.log("emptyArr: " + !!emptyArr.length);
if (emptyArr.length) {
  console.log("Array is not empty");
} else {
  console.log("Array is empty");
}

Since the following values evaluate to false in a Boolean expression in Javascript

0, "", undefined, null, false

instead of if (!!emptyArr.length) you simply go if (emptyArr.length) in Javascript. These values (except false) are called falsy in Javascript.

connexo
  • 53,704
  • 14
  • 91
  • 128
1

This the proper way to check for an empty array in javascript

var emptyArr = [];
var emptyString = "";
var emptyNumber;
var numberWithZeroValue = 0;
var emptyBoolean = false;
if(emptyArr.length == 0){
console.log(" its an emptyArr");
}

console.log("emptyString: " + !!emptyString);
console.log("emptyNumber: " + !!emptyNumber);
console.log("numberWithZeroValue: " + !!numberWithZeroValue);
console.log("emptyBoolean: " + !!emptyBoolean);
Abslen Char
  • 3,071
  • 3
  • 15
  • 29
0

you can declare array in typescript like this.

var alphas:any[];
Sunil
  • 138
  • 2
  • 15
0

Maybe you want to know why vanillaJS array initialization equals true and typescript false.

This is because of the typescript transpiler. It transpiles var emptyArr: Array; into var emptyArr; and now you've the same result as your third vanillaJS line (var emptyNumber;).

Philip
  • 3,486
  • 1
  • 24
  • 37