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