When to use the " " quation marks around input


#1


https://www.codecademy.com/en/courses/javascript-beginner-en-6LzGd/1/2?curriculum_id=506324b3a7dffd00020bf661#


When I type this for the above exercise it gives me an error (that I didn't print out the value of newNumber).

// Parameter is a number, and we do math with that parameter
var timesTwo = function(number) {
return number * 2;
};

// Call timesTwo here!
var newNumber = timesTwo("8");
console.log(newNumber);

The result is correct though, it prints out 16. When I change the code by deleting the quotation marks:

// Parameter is a number, and we do math with that parameter
var timesTwo = function(number) {
return number * 2;
};

// Call timesTwo here!
var newNumber = timesTwo(8);
console.log(newNumber);

Then it also prints 16 but it says the exercise is correct.

When do I use the " " and why is it wrong in this exercise?

Thanks in advance.


Replace this line with your code.


#2

Anything wrapped with "" becomes the data type called strings. Therefore "8" is a string and NOT a number. Your first function outputs a string, "16". It's not obvious in the console because they don't display the quotation marks.

The second one deals with the number 8 and therefore outputs the number 16.
Reason why you are getting an error message for the first code is because you were giving them a string and not a number.


"Strings are useful for holding data that can be represented in text form."
~MDN

Such as "Hello world!".
You can read more about data types here. This is also where the quote came from.


#3

Thanks! Now I understand!


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.