join Turns null And undefined Into Empty Strings
join coerces null and undefined to empty strings while other values go through String(); missing data becomes invisible separators unless mapped first.
Code
// join 把 null 和 undefined 转成空字符串,其他值走 String()
const parts = ['a', null, 'b', undefined, 'c'];
console.log(parts.join('-'));
console.log('length kept:', parts.join('-').split('-').length);
// 数字、布尔、数组都按 String() 规则串接
console.log([1, true, false, [2, 3]].join('|'));
// 嵌套数组先 toString 成逗号串,join 只展平一层
console.log([[1, 2], [3]].join('+'));
// 缺省分隔符是逗号
console.log(['x', 'y'].join());
// 想区分"缺值"与"空串":先映射成显式占位
const safe = ['a', null, 'b'].map((v) => v ?? '(missing)');
console.log(safe.join('-'));Output
Captured from a sandboxed run of this snippet in the build pipeline.
a--b--c
length kept: 5
1|true|false|2,3
1,2+3
x,y
a-(missing)-bHow it works
The file’s state is a handful of top-level bindings: parts, safe.
Using it
The file’s steps show up in the output as length kept:.
Output is labeled for readability — expect lines prefixed with length kept:.
Key statements
const safe = ['a', null, 'b'].map((v) => v ?? '(missing)');
Interactive Tools for This Topic
FAQ
- Do I need to install anything before running this?
- No — every API used here ships with the language runtime, so there is nothing to install. Its state lives in module-level bindings: `parts`, `safe`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.