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
200,589 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.