'Get empty string instead of null

I have null in a row if there no number. I need to put empty string in a row instead of null.

<td>${number}</td>. // null, if there no number,

so, Am I thinking in the right way? :

!number ? number : "" 


Solution 1:[1]

This will check if the variable's type is number and if not it won't render anything:

typeof number === 'number' ? number : ''

Solution 2:[2]

try

`<td>${!number ? '' : number}</td>`

if there is no number then display '' else display number

Solution 3:[3]

You can use Nullish coalescing operator (??) that returns its right-hand side operand when its left-hand side operand is null or undefined, and otherwise returns its left-hand side operand.

Working Demo :

let number = null;

document.getElementById('numberText').innerHTML = number ?? ''
<p id="numberText"></p>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 kappaprajd
Solution 2 Advait_Nair
Solution 3 Rohìt Jíndal