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 trial

JavaScript

If strings are JS objects, how do they differ from Capitalized objects as Math.random or Date?

If strings are JS objects, how do they differ from Capitalized objects as Math.random or Date?

Ben, what Leslie is talking about is awesome, I haven't heard any discussion on that at all, until now. But that has me looking at your question again. Are you asking if strings are objects... or if Strings are objects...

Leslie, yes I'm familiar with Andrews Prototype course (though I was largely overwhelmed throughout it) And am aware that everything is built on something. Even strings. Just had never heard mention of the String Prototype. Although now that I'm aware of it, it makes perfect sense. I still think it's possible that Ben was asking about string being an object, not String. Cause I had the same question. (as I say until Andrew pointed out that everything is a method and an object)

2 Answers

String (with a capital S) is a built-in global object in JavaScript just like Number, Array, Math, Date, etc. It has a several methods and properties that you can call, such as String.slice() and String.length. Global objects like these in JavaScript are like constructors, which (in best practice) are the only type of entities that are capitalized. Let's say you define a new string:

var newString = "foo";

The newly defined variable inherits all of the methods and properties of the String global object, so from here, you can call all of the methods and properties from String on newString directly.

You can read more about String here.

Hopes this helps!

Leslie, is String part of the new ecma script? I I don't recall any discussion on it in any of the courses I have taken yet.

Hi John,

String is a standard built-in object that has been included in JavaScript since its beginning. Without it, you would not be able to call any of its handy methods or properties directly on strings.

Prototypal Inheritance is the foundational element of JavaScript that allows this to happen. You can read more about it here.

Andrew Chalkley discusses the prototype chain in his Object-Oriented JavaScript course, as well (albeit without specific mention of the standard built-in global objects like String). Watch it here.

Happy coding,
Leslie

Ben, hi. If you are asking why is Math.random not math.random... I can't find anything on it and I wondered the same thing. I suspect however that smart people:

  • a: hold math in high regard and possibly wanted to grant it special significance
  • b: wanted to differentiate it from other types of functions
  • c: they got really high, giggled a lot and said "This'l freak them out"

Added the term "Date" after math to emphasis I ask on Captial-letter objects in general...