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 JavaScript Basics (Retired) Working With Numbers The Math Object

Strings and numbers are objects? This video says "yes". Other sources say "no".

Dave says "JavaScript is made up of different types of objects. Numbers are one type of object and strings are another type of object."

My understanding (from a different course, I believe) is that numbers, strings and booleans are not objects. They are primitive values. As I'm working to keep these terms defined, this causes some confusion. What's the story?

2 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

You are correct! And so is he! :smiley: Those are primitive data types in JavaScript. However, JavaScript also has corresponding object wrappers for each of these. So wherever you're seeing this, they must be referring to the object "wrapper" for that data type. Here's some information from MDN.

https://developer.mozilla.org/en-US/docs/Glossary/Primitive

Thanks for the link. This is to the point and previously unknown to me โ€” Except for null and undefined, all primitive values have object equivalents that wrap around the primitive values.

When strings are referred to as objects, it is here that the object wrapper is in play. Without the object wrapper it's a primitive data type. Correct?

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Lee Zamastil That's my understanding. But I'm not sure that in JavaScript they can really be separated. However you can use valueOf() to determine the primitive datatype of the ... whatever... you're looking at :smiley:

I'm reading Nicholas Zakas's The Principles of Object-Oriented JavaScript. In it he includes a very illustrative example of what a primitive wrapper is doing behind the scenes.

This is the code we write which allows us to ignore the fact that a primitive value is not an object and therefore does not have methods to call:

var name = "Nicholas";
var firstChar = name.charAt(0);
console.log(firstChar);                 // "N"

This is what happens behind the scenes:

// what the JavaScript engine does
var name = "Nicholas";
var temp = new String(name);
var firstChar = temp.charAt(0);
temp = null;
console.log(firstChar);                 // "N"

Zakas writes "The String object exists only for one statement before itโ€™s destroyed (a process called autoboxing)."