js push into array if item exist 1

js push into array if item exist

if(this.items.indexOf(item) === -1) {
    this.items.push(item);
    console.log(this.items);
}

Here is what the above code is Doing:
1. We’re creating a new array called items.
2. We’re creating a new function called addItem.
3. We’re creating a new variable called item.
4. We’re creating a new if statement that checks if the item is already in the array.
5. If the item is not in the array, we’re pushing the item into the array.
6. We’re logging the array to the console.

Similar Posts