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 Foundations Objects Methods

Object syntax

Why doesn't the object need a closing semicolon to end the statement before the closing bracket? Also, with the challenge, why does the method work without calling the function as opposed to referencing it(since the return value is the string)? For example, I tried the following as an answer:

      var genericGreet = function() {
        return "Hello, my name is " + this.name;
      }

      var andrew = {
        name: "Andrew",
        greet: genericGreet();
      }

      var ryan = {
        name: "Ryan",
        greet: genericGreet();
      }

After some thinking I believe it is because the objects are technically variable definitions, and the semicolon should come after the closing bracket(since it is a single statement). I'm still unsure of the reason why genericGreet does not need a pair of call parenthesis.

2 Answers

Earl Varona
Earl Varona
13,889 Points

If the code is inside the bracket (or block) and you're at the end before the closing bracket then it's technically the end of that line of code so the semicolon is not necessary (but I still think it's a good habit to apply it).

I'm not sure if I'm understanding your question correctly, but in your code:

greet: genericGreet();

you are calling the function genericGreet instead of assigning it because you are using the () with the function. The correct way is:

greet: genericGreet;

But if you're using it the way you did, the function should only return the string "Hello, my name is " but should not return this.name because it is not a string but an object.method. 'This' refers to its parent object (which is window, or so) and it will look for the method called name of that parent object.

I hope I made sense. Good luck.

If you want to be strict about it, the absolute correct way of doing it is like this: var someFunction = function(){...}; (note the semicolon at the end of the line). This is because you are writing a statement (consisting of a variable assignment), and all statements in JavaScript should end with semicolons. This is in contrast with a pure function definition (which is not a statement) and for which you do not need a semicolon at the end. Example below:

function someFunction() {
  ...
} // here you do not need semicolon because this is not a statement.

However, many times the JS interpreter is pretty loose (it allows for some deviations) as long as there is no possibility for confusion. In your case, the presence of the closing bracket symbol and the new line is enough to keep the JS interpreter happy.

Note however, that it is always good practice to end your statements with semicolons. Usually in production, JS code gets minified before being deployed on the server, which means that everything gets squished on a single line. What used to be very clear for the JS compiler before minification, suddenly becomes gibberish when code is minified unless all semicolons are consistently placed at the end of each statement.

When dealing with the exercises in this course I assume you are not applying any minification and you can afford to be more lax about these issues.