sharepoint online – Check null value in spfx webpart

In Javascript a single equals operator (=) is used for assignment, that is, to assign a value to a variable.

So when you do post.Content = null, you are assigning post.Content to be null.

In order to check if it is null, you have to use either a double equals (==) or a triple equals (===). The difference is that with the double equals (==), Javascript will try to coerce the values to be of the same type before actually comparing them, so for instance:

'1' == 1    // true
1 == 1      // true
'1' == '1'  // true
1 == '1'    // true

however the triple equals does a strict comparison, and does not convert the type when making the comparison:

'1' === 1    // false
1 === 1      // true
'1' === '1'  // true
1 === '1'    // false

It’s generally considered a better practice to use the strict comparison (===) because then you won’t run into any unexpected results due to the type coercion.

So to check it post.Content is actually null, you want to do

if (post.Content === null)

But – that will only check if it is exactly null. What about if it’s undefined, or an empty string? A better way to check if post.Content is just blank, because this will work whether is is null or undefined or an empty string, is to just check if it is not (!) there:

if (!post.Content) {
    // message if post.Content is blank for any reason
}