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 Numbers and Strings

Cuong Hoang
Cuong Hoang
1,769 Points

A question about prompt(); ...

So whatever the user or we input in prompt(); , it will always return the input or value as a string?

2 Answers

Sam Baines
Sam Baines
4,315 Points

Yes correct - but you can easily convert the string from a prompt command into an integer providing the string starts with a number, using the code below:

var width = prompt('Enter the number 190px');

parseInt(width);  //This will convert the string "190px" from the width variable into the integer 190.

And of course you could just use the function parseInt straight onto the prompt() function too:

var width = parseInt(prompt('Enter a number'));

Hope this helps.

You always want to explicitly set that you are parsing the number using the base 10 system, however. In certain cases, certain strings will register as hexadecimal numbers unless explicitly set otherwise. Here is how you should write parseInt (99% of the time unless you're using a different system of numbers):

var width = parseInt(prompt('Enter a width:'), 10);
Sam Baines
Sam Baines
4,315 Points

Thanks thats good to know marcus - I was just going by what I learnt in the javascript basics course already - I will need to do some looking into this base 10 system.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt

Read under the heading Description which lists possible ways that parseInt can get the wrong number. Here's something to try in your console:

//Use a value of "0x34" in each of these prompts
var width = parseInt(prompt('Enter a number'), 10);
console.log(width); //Returns 0
var width = parseInt(prompt('Enter a number'));
console.log(width); //Returns 52
Sam Baines
Sam Baines
4,315 Points

Ah glad you have made me read this Marcus, I fully understand it now - so the base 10 system is the main one to use for just generic numbers based around 0-9 and base 16 if you were trying to parse integer values for example for a RGB value from hexadecimal color codes.

Absolutely! You could break apart a hexadecimal color value gathered from CSS (or something) and convert it into rgb:

var hexval = "#ffffff";
hexval = hexval.replace("#","");
var r = parseInt(hexval.slice(0,2), 16);
var g = parseInt(hexval.slice(2,4), 16);
var b = parseInt(hexval.slice(4,6), 16);
var rgb = r + ", " + g + ", " + b;
console.log(rgb);

Or:

var hexval = "#ffffff";
hexval = hexval.replace("#","");
var r = hexval.slice(0,2);
var g = hexval.slice(2,4);
var b = hexval.slice(4,6);
r = parseInt("0x"+r);
g = parseInt("0x"+g);
b = parseInt("0x"+b);
var rgb = r + ", " + g + ", " + b;
console.log(rgb);
Sam Baines
Sam Baines
4,315 Points

Awesome Marcus - thats a great little tutorial code right there for converting hexadecimal codes into rgb (glad I was right about using the base 16 for this, otherwise I would have look like a right newbie) - I'm off to have a play with this!

ps. you also just taught me how to use the the .replace method.

Awesome man! Replace is a very powerful command and once you get the hang of regular expressions, it can be used to do some really cool things. Play around with it and see what you come up with :)

Cuong Hoang
Cuong Hoang
1,769 Points

Thanks for the replies guy, but as a beginner it's really hard to understand the base 10 system.. do you mind explaining?

The base 10 system is the standard number system from 0-9 (10 numbers). It is the standard system in use with mathematics.

But there are also other number systems such as Octal (8 number system) or Hexadecimal (16 number system). Octal isn't used that much, but Hexadecimal is very frequently used to express colors as it has a fast, efficient way of representing blocks of numbers that can convert into the RGB color scale.

In JavaScript, as of ECMAScript 5, in order to get an octal number, the radix must be explicitly set when parsing integers. They can't be parsed from a string representation.

//the first argument is the string to parse
//the second argument is known as the radix
//which sets which number system to use
var anOctal = parseInt("17", 8);
//Puts 15 into console
console.log(anOctal); 

I don't want to confuse you too much with all of this. If you want to hear more, though, I will gladly go on.