10/33 Why does it return: 24 to the console?


#1

When I submit the following code it puts 24 to the console. How come?!

// Our bob object again, but made using a constructor this time
var bob = new Object();
bob.name = "Bob Smith";
bob.age = 30;
// Here is susan1, in literal notation
var susan1 = {
name: "Susan Jordan",
age: 24
};
// Make a new susan2 object, using a constructor instead
var susan2 = new Object();
susan2.name = "Susan Jordan";
susan2.age = 24;


#2

Because we are working in the debugging console, it behaves like a terminal that takes commands, executes them and responds. When we submit our code, it is entered in the command line. What we are seeing here is the response to the final line in the code,

susan2.age = 24;

To the console this is a command (a statement in our program, albeit) and the response is the value that was just assigned, 24.

The IDE we are working in does not use color coding so we cannot tell logging from artifact echoes. Run the code in labs repl.it and you will see the output in green, as,

=> 24

The surest way to prevent an echo is to log something as the last statement, such as,

console.log("Bye");

Also, when logging inside a program that sets a variable, if at all possible, set the variable first, then log whatever it is to be logged. This prevents the assignment from echoing when the code ends.


#3

That explains it all! Thank you for your reply!