Because using .toString() for null vars doesn’t work, and I can’t be checking each and every one of these in my particular application.
I know this is a stupidly simple problem with an answer that literally must be staring me in the face right now.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The non-concatenation route is to use the String() constructor:
Of course,
var str = "" + myVar;is shorter and easier. Be aware that all the methods here will transform a variable with a value of null into"null". If you want to get around that, you can use||to set a default when a variable’s value is “falsey” like null:Just so long as you know that 0 and false would also result in
""here.https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/String