Test yout skills: Object basics 3

let cat = {
name : ‘Bertie’,
breed : ‘Cymric’,
color : ‘white’,
greeting: function() {
console.log("Hello, said " + this.name + " " + this.breed + “.” );

let cat2 = {
name : ‘Vico’,
breed : ‘Ja’,
color : ‘black’,
greeting: function() {
console.log("Hello, said " + this.name + " " + this.breed + “.” );

Hallo, i would like to know if my code ist correct, i saw a lot of answers from others that they write like this but i dont understand the difference: "
console.log(Hello, said ${ this.name } the ${ this.breed }.);
My question is if just writing “this.name” is enough because i am already inside the object where i wanna access, and when should i use the other method?.
Thank you

Hello @Victoria_Petrone

you doing great

just tiny little thing there a missing semicolon after the closing bracket of the cat and cat2 object so it }; at the end

now for the ${this.name}
let us use your code "Hello, said " + this.name + " " + this.breed + “.” here you used concatenation for multi strings the ${this.name} make the browser interpret the the thing inside the {} to be obejct or function not as string
so it allow you to make one string like following
Hello, said ${this.name} ${this.breed}. and it will replace anything inside the {} as inyour code so here we used only single quotation and did not need the concatenations +
notice i used backword quote to make the $ work this sympol ``

you can use both as you like it’s better to use the $ way cause it make your code shorter

hope that help and have a nice day :slight_smile:


thank you! helped a lot! :blush:

you welcome and glad it helped :slight_smile:

I just wanted to opine in with a comment the OP made about when to use String Litterals or use String Templates: the differecnt being the first one is using Backticks and the other uses "". I would say that this is prefference, however I have found personally that using the Backticks method and using${this.variable}` is better for doing string manipulation and writting HTML in JS.