Do numbers need quotes in Arrays?

Tell us what’s happening:
I understand the lesson. I’ve passed the test. I just want to know if numbers ever need a quote. In this lesson, I was struggling at first because I kept putting my string in quotes (which is correct) and my number in quotes (which was not correct) which would prohibit me from moving on to the next lesson. Do numbers need quotes in any other situtation? Do you ever need quotes for numbers?
Your code so far


// Only change code below this line
var myArray = ["James", 26];

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36.

Challenge: Store Multiple Values in one Variable using JavaScript Arrays

Link to the challenge:

You put quotes around a number only when you want it to be part of a string, like "123 Maple Lane". Otherwise, you probably don’t want your number in quotes.

1 Like

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures
You may find this documentation useful

1 Like

Oh ok. So if I had a array like this → var carTypes = ["Toyota 1994", "Ford"];

Then that is when those numbers would be in quotes?

versus if I have a array like this → var myAge = ["Jimmy", 26];

Then the number that is seperate from a string would NOT need to be in quotes ever?

Just being super ocd, i’m sorry. I know I shouldn’t be this literal and observant over something pretty self explanatory. I just am trying to instill it into my brain lol.

1 Like

An important thing to remember is that a string is a string and number is a number, but a number in quotes is a string, and sometimes you want this, and other times you will realize JavaScript took the liberty of coercing your variables into data types you were not expecting and this can caused unwanted side effects . As an example:

let int = 1 + '2';
console.log(int) //12
console.log(typeof int) //string
1 Like

In general, yeah. There will occasionally be exceptions, but as a general rule you don’t want numbers in quotes.

1 Like

so the '2' acts as the number 2 that is added to 1 in let int = 1 + '2'; ?

Like this result will be 12 because they are literally put side by side in this instance and not literally looked at as 1+ 2 = 3?

Ok, I got ya. So only numbers in quotes if it’s a string and only numbers alone if it is not in a string. A number just stays a number.

1 Like

The plus operator for two number such as 1 + 2 works as you would normally expect, but if use the plus operator with a string its going to concatenate the values so in the pervious example you had the number 1 and the string "2" so when you used the plus operator it concatenated them rather than adding them so you end up with the string "12"

1 Like

Ok thank you so much. I understand that now.

Remember, quotes = display or string and no quotes = value

hope this helps :grinning: :+1:

1 Like

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