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 Foundations Numbers Creating Numbers: Part 2

Tom Studer
Tom Studer
8,098 Points

How cam you assign a variable to a product SKU number that may start with a 0. example: SKU# 012- 6lb bag of sand.

If you have a product catalog and you want 012 to represent a bag of sand.

SKU# 012 - 6lb. bag of sand

How can you associate "012" with "6lb. bag of sand" without JS by default converting "012" to a different number type?

4 Answers

Hi Tom,

I don't know if this will meet your needs but one way you could do it is to store it as a property/value pair in an object.

var products = {"012" : "6lb. bag of sand"};
console.log(products["012"]);  // outputs "6lb. bag of sand"

Here's some more code on how you might interact with this object:

var products = {"012" : "6lb. bag of sand",
                "013" : "12lb. bag of sand"
               };

// This will retrieve all the sku numbers as strings with the leading zero intact
for (sku in products){
  console.log(sku);
}

// In case you have the sku number as a string stored in a variable and you need to retrieve the item description
var skuNum = "012";
console.log(products[skuNum]);

Just to go into further detail about Jason's answer: unless you are using numbers for mathematical equations, you should store the number as a string, instead of a number type just as Jason did in his answer. Strings are safer for numbers that will not have any math applied to them because just as Jim shows in this video, Javascript has some wonky conversion factors and rounding errors for numbers because of the way it maps floating point numbers.

SKU numbers don't really need to have any kind of mathematical operations applied to them, so it is safe to store them as strings. If you were to, for example, have SKU numbers generated that were in numerical order, starting with 0, however, you could store them as strings, convert to integer number, apply mathematical operation, and then convert back to string. Or you can simply use a number type for a variable and start the SKU numbers with 1-9.

For what it's worth, Marcus Parsons's answer above (about storing SKUs as strings even if they're all-numeric) is considered the "best practice" in the real world, and you should do it that way.

Numbers are every bit as sortable/filterable/searchable when they're strings as they are when they're numbers, so there's no loss of use doing so, and then there's no problem with leading-zero-loss, rounding errors, etc. Plus if your company later switches to alphanumeric SKUs (which is not at all uncommon), you have no problems or extra work/headaches in doing so.

So please store SKUs as strings in your application.

Also, if you needed the SKU as an int and not a string, you can use parseInt()

var sku ="0000000001234567890";
var sku_int = parseInt(sku);

You lose the leading zeroes if you do this.