Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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.

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

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
216,120 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.