How to prevent adding duplicates to an array in JavaScript

1 Answer

0 votes
const arr = ['javascript', 'typescript', 'nodejs', 'c'];

const s = 'c++';

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

console.log(arr); 

   
   
   
   
/*
run:
   
["javascript", "typescript", "nodejs", "c", "c++"]
   
*/

 



answered Apr 24, 2022 by avibootz
...