I have five digit US zip codes coming out of the database. When JavaScript see them, it treats them as a number and lops off the leading zeros.
How do I get JavaScript to treat the numbers as a string so that they display correctly?
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.
Quote them.
In both JavaScript and JSON
05123is an octal number equal to2643(thanks @Marc B) and"05123"is a string containing only numeric characters.Don’t quote them in your database, of course. You’ll want to quote them in the JavaScript or JSON you are generating in the server-side code that’s reading the information from the database and passing it to your client-side code. Ordinarily, that’s as simple as casting the zip code to a string (or, as is the more likely case, not casting your numeric zips to a number.)