Get Programming with JavaScript - Listing 3.09
Listing 3.09 - Concatenating string properties
JS Bin
var book1;
var book2;
book1 = {
title: "The Hobbit",
author: "J. R. R. Tolkien"
};
book2 = {
title: "Northern Lights",
author: "Philip Pullman"
};
console.log(book1.title + " by " + book1.author);
console.log(book2.title + " by " + book2.author);
Further Adventures
Listing 3.09 - Concatenating string properties - Tasks 1 and 2
- Add a third book.
- Log its details to the console.
var book1;
var book2;
var book3; // declare a third variable
book1 = {
title: "The Hobbit",
author: "J. R. R. Tolkien"
};
book2 = {
title: "Northern Lights",
author: "Philip Pullman"
};
book3 = { // create a third
title: "The Adventures of Tom Sawyer", // book and assign
author: "Mark Twain" // it to the
}; // book3 variable
console.log(book1.title + " by " + book1.author);
console.log(book2.title + " by " + book2.author);
console.log(book3.title + " by " + book3.author); // log the book
Listing 3.09 - Concatenating string properties - Tasks 3 and 4
- Add a third property.
- Update the messages logged to include the new property.
var book1;
var book2;
var book3;
book1 = {
title: "The Hobbit",
author: "J. R. R. Tolkien",
published: 1937 // add a third property
};
book2 = {
title: "Northern Lights",
author: "Philip Pullman",
published: 1995 // add a third property
};
book3 = {
title: "The Adventures of Tom Sawyer",
author: "Mark Twain",
published: 1876 // add a third property
};
// Update the messages
console.log(book1.title + " by " + book1.author + " was published in " + book1.published);
console.log(book2.title + " by " + book2.author + " was published in " + book2.published);
console.log(book3.title + " by " + book3.author + " was published in " + book3.published);