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 Loops, Arrays and Objects Tracking Data Using Objects Add Properties to an Object

what's wrong with this code?

I do not know what code to go with. I am aware that the property values in an object are to be surrounded by quotation marks if not numbers and that they do not need them with numbers. But what happens when you have a property value with both letters and numbers. I have tried using no quotes, using quotes, and as shown in my attached code, using quotes only around the letter in a value without quotes for the letters in it.

var paris = {
population: 2.211 + "e" +6,
latitude:48.8567 + " N",
longitude; 2.3508+ "E",
<!DOCTYPE html>
  <meta charset="utf-8">
  <title>JavaScript Objects</title>
<script src="script.js"></script>

var paris = { population: 2.211 + "e" +6, latitude:48.8567 + " N", longitude; 2.3508+ "E", };

You're have a semi-colon instead of a colon after longitude. Check you're syntax.

1 Answer

Steven Parker
Steven Parker
228,991 Points

What happens here is known as "type coercion".

When you perform an operation on unmatched types, JavaScript will attempt to convert one of them to a type appropriate to the operation. In this case the presence of the string causes the plus sign operator to be interpreted as a concatenation, so the number is converted into a string to complete the operation. The final result will therefore also be a string.

So, for example, instead of: 48.8567 + " N", you could just write: "48.8567 N", since that is what it will become.

And as Phouthalang pointed out, you have a semicolon instead of a colon in your object initialization.