Why do we need to put quotations (" ") on strings but leave the quotations (" ") out for numbers?

Lesson 36. Store Multiple Values in one Variable using JavaScript Arrays

var myArray = [“array”, 23];

Why do we need to put quotations (" “) on strings but leave the quotations (” ") out for numbers?

Can anybody explain this to me, please?

Thank you

Welcome, francescowang.

I suggest you do some testing with code like this:

let myNum = 22;
let myString = "francescowang";
let unknown = "22";

console.log("Is myNum a string: ", typeof myNum === 'string');
console.log("Is myString a string: ", typeof myString === 'string');
console.log("Is unknown a string: ", typeof unknown === 'string');
console.log("Is unknown exactly the same as myNum: ", myNum === unknown);

For future posts, if you have a question about a specific challenge as it relates to your written code for that challenge, just click the Ask for Help button located on the challenge. It will create a new topic with all code you have written and include a link to the challenge also. You will still be able to ask any questions in the post before submitting it to the forum.

Hope this helps.

1 Like

The simple answer is that “array” in your example is a string and “23” is a number. They are different data types, used by the computer in different ways, and the quotation marks are a way of signalling that.

The main thing is that 23 can be used to add, subtract, divide, etc., whereas “array” is just a word.

If you add two strings “23” + “23”, the answer is “2323”. Because in this case the “add” is actually a special operation called “concatenation.”

if you add two numbers, 23 + 23, the answer is 46, because in this case, the “+” is calling for the math operation we all know and love.

You will often see a word without quotes. In that case, it is usually a variable (or some other keyword that’s part of the language). So when you see Array, like that, no quotes, it’s part of JavaScript and it has its own special function. But “Array” or “array” are just strings and you can do whatever you want with them.

Or if you see something like myName, that’s probably a variable that was declared earlier, like var myName = “Mike.” In that case, it’s a variable representing a string (kind of a nickname). It could also be a number. var myAge = 20 would mean that whereever you wrote myAge, it would act the same as 20 (by the way, myAge = 20 is a lie).

If you are interested, there is a way to “force” a string to be a number. If you had a string, “23,” and wrote parseInt(“23”), it will push out 23, the number, which can then be added and subtracted and used in other math-y ways.

TL;DR, the quotes around array signal that it is a “string.” The lack of quotes around 23 signal it’s a “number.”

1 Like

Thank you so much! :heart_eyes: :grinning:

I’m saving your answer.

1 Like

Thank you so much, Shaun. :grin:

I’m saving your answers for future reference. :grinning: