-6

I have a litlle bit of trouble with the if statement in javascript when checking for an empty array or an array that have the value of 0 for example

array1=[0]
        
if(array1 === 0 || array1.length <= 0){
    console.log(true)
} else {
    console.log(false)
}
        
if(array1 === 0){
    console.log(true)
} else {
    console.log(false)
}

the problem is , I am always having false as a result

Matus Dubrava
  • 13,637
  • 2
  • 38
  • 54
arkahn jihu
  • 109
  • 4
  • [Possible duplicate](https://stackoverflow.com/questions/1181575/determine-whether-an-array-contains-a-value). – user202729 Apr 01 '18 at 11:29
  • 3
    You're getting downvotes because it's not clear what you would like to achieve, would you like to check the existence of element within an array or array consisting of single zero element, and of course, because it's a very basic question, answer to which you could have just found in a couple of seconds with any search engine you like – nicael Apr 01 '18 at 11:33
  • 1
    I am just a beginner trying to learn , i feel like you're chasing me away from this forum – arkahn jihu Apr 01 '18 at 11:38
  • 2
    No, we're trying to do you a favor by pushing you towards using Google and reading documentation. You won't get very far before you learn to use those resources. – JJJ Apr 01 '18 at 11:40
  • @JJJ i did use google and spent few hours trying to figure out why , if it wasnt the case i wouldn't post here and get all the negative attitude , if you put yourself in the choose of a beginner you will probably understund , that been said , have a good day – arkahn jihu Apr 01 '18 at 11:48
  • Sorry, but if you spent *hours* googling for how to check array contents in JS and didn't find a solution, I can only suggest that you find a tutor or enroll in a course for how to use Google efficiently. They are often provided for free or very cheap by local libraries and community centers. – JJJ Apr 01 '18 at 11:54
  • 1
    @JJJ if you cant help someone at the least dont put them down ,valuing a human communication skills is a key – arkahn jihu Apr 01 '18 at 12:18

2 Answers2

2

Since an array never equals 0 when you use strict equality (===), the 1st condition array === 0 fails. Since the array is not empty, the check array1.length <= 0 (btw - an array's length can never be less than 0) fails as well, and the result is false.

Check if array in the 1st index (0) is equal to 0:

const arr1 = [0];
const arr2 = [];
const arr3 = [5];

const isZeroArray = (arr) => arr.length === 0 || arr[0] === 0;

console.log(isZeroArray(arr1)); // true
console.log(isZeroArray(arr2)); // true
console.log(isZeroArray(arr3)); // false

In addition, if you want to check if all items in the array are 0, you can use Array.every():

const arr1 = [0, 0, 0, 0];
const arr2 = [];
const arr3 = [5];

const isZeroArray = (arr) => arr.length === 0 || arr.every(e => e === 0);

console.log(isZeroArray(arr1)); // true
console.log(isZeroArray(arr2)); // true
console.log(isZeroArray(arr3)); // false
Ori Drori
  • 183,571
  • 29
  • 224
  • 209
-2

why === it should be ==

var array1 = [0]

if (array1 == 0 || array1.length <= 0) {
  console.log(true)
} else {
  console.log(false)
}

if (array1 === 0) {
  console.log(true)
} else {
  console.log(false)
}
Ori Drori
  • 183,571
  • 29
  • 224
  • 209
Rehan Azher
  • 1,340
  • 1
  • 9
  • 17