Returns a new array with null and undefined values removed.
null
undefined
The element type is narrowed via NonNullable<T>. Other falsy values (0, '', false, NaN) are preserved — use array.filter(Boolean) or a custom predicate if you want full falsy removal.
NonNullable<T>
0
''
false
NaN
array.filter(Boolean)
compact([1, null, 2, undefined, 3])// [1, 2, 3] Copy
compact([1, null, 2, undefined, 3])// [1, 2, 3]
Returns a new array with
nullandundefinedvalues removed.The element type is narrowed via
NonNullable<T>. Other falsy values (0,'',false,NaN) are preserved — usearray.filter(Boolean)or a custom predicate if you want full falsy removal.