Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialA X
12,842 PointsWhy do all values default initially to string in JavaScript?
I'm going through Dave McFarland's JavaScript track, and we're discussing the section on Numbers, but I'm curious if anyone understands the logic or the history behind why items like a number or a special character in JavaScript are all initially treated as a String vs. in the case of numbers, an Integer (Int) or a Floating Point Value. I could see having a number contained within a sentence as being understood by JavaScript as a part of the string, but I don't understand the point of having numbers alone be deemed as strings... Like a user inputs their age, and JavaScript interprets that value as a string. Why was this logic originally done in JavaScript? I don't understand the point/purpose that this serves.
1 Answer
Jennifer Nordell
Treehouse TeacherI believe what he's talking about is numbers input by a user. Everything read in by the user is considered a string. It's just the way input works. And I'm guessing the reason is that most things input by a user would be impossible to interpret as a number. Ie email, name, address, social security number etc etc. Even though some of these contain numbers, there are no instances where you will be doing mathematical operations on an adress for example: 123 Main St. Keep in mind that this is not just done this way in Javascript but multiple programming languages.