Implicitly coercing an array to a string calls the .join method, and:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join
If an element is undefined or null, it is converted to the empty string.
So, you can't use implicit coercion or .join. One option would be to use reduce to manually join every element (so as not to skip nulls):
var arr = [];
arr.push('x');
arr.push(null);
arr.push('z');
const joined = arr.reduce((a, elm) => a + (a ? ',' : '') + elm);
var samp = 'Array elements are: ' + joined;
console.log(samp);
If you also want the quotes around each string as with your
Array elements are: 'x', null,'z'
then check the typeof each element before concatenating:
var arr = [];
arr.push('x');
arr.push(null);
arr.push('z');
const joined = arr.reduce((a, elm) => {
const quote = typeof elm === 'string' ? "'" : '';
return a + quote + (a ? ',' : '') + elm + quote;
}, '');
var samp = 'Array elements are: ' + joined;
console.log(samp);
(if the elements contain any single-quotes themselves, you may want to replace them with \' as well)
nullif it is null, create null as string "null" and then concatenate. null is a typeof object, so you can do JSON.stringify(null) which will be converted to string