Casting Null Values to String

Posted on Updated on by Gergő D. Nagy

Sometimes we need to perform string actions, so we cast them to string with the toString() method. But you can’t cast null values to string with this, so we need a little trick to avoid errors and issues.

// Cast null to string
// Then you can perform any "string" action
(value || '').toString(); // Chain the methods after this

Need a web developer? Maybe we can help, get in touch!