How to add element to array if it does not exist in the array with Node.js

1 Answer

0 votes
const arr = ['node.js', 'c', 'c++', 'java'];
const value = 'c#';
 
if (!arr.includes(value)) {
    arr.push(value);
}
 
 
console.log(arr);
 
   
   
   
   
/*
run:
   
[ 'node.js', 'c', 'c++', 'java', 'c#' ]
   
*/

 



answered Apr 13, 2022 by avibootz
...