How to add element to array if it does not exist in the array with JavaScript

1 Answer

0 votes
const arr = ['javascript', 'c', 'c++', 'java'];
const value = 'c#';

if (!arr.includes(value)) {
  	arr.push(value);
}


console.log(arr);

  
  
  
  
/*
run:
  
["javascript", "c", "c++", "java", "c#"]
  
*/

 



answered Apr 13, 2022 by avibootz
...