I’m trying to create a web page that contains 3 JavaScript objects

Try

let book1 = { title: "A Song of Ice and Fire", author: "George RR Martin", type: "Paperback", price: 19.99, image: "songoficeandfire.jpg" }
let book2 = { title: "The Woman in the Window", author: "A.J. Finn", type: "Paperback", price: 6.29, image: "womaninwindow.jpg"}
let book3 = { title: "The Silkworm", author: "Robert Galbraith", type: "Hardback", price: 14.99, image: "silkworm.jpg" }

let books = [book1, book2, book3];

function show(index) {
  msg.innerText = JSON.stringify(books[index], null, 4);
}
<button onclick="show(0)">Show book1</button>
<button onclick="show(1)">Show book2</button>
<button onclick="show(2)">Show book3</button>

<pre id="msg"></pre>

Leave a Comment