How to join strings with a comma only if strings are not null or empty in Node.js

1 Answer

0 votes
const s1 = '';
const s2 = 'node.js';
const s3 = '';
const s4 = '';
const s5 = null;
const s6 = 'java';
const s7 = 'c#';

const s = [s1, s2, s3, s4, s5, s6, s7].filter(Boolean).join(',');

console.log(s); 

  
  
  
  
/*
run:
  
node.js,java,c#
  
*/

 



answered Apr 13, 2022 by avibootz
...