Casting Null Values to String

JavaScriptPosted on

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!

Looking for web dev job?
You can check them on Jooble.

To see and write comments, you must allow the related (Disqus) cookies. For more information, please visit our privacy policy page.

Similar Posts

More content in JavaScript category