+ 1
How can I get firstName in this code
let grammarcorrah = { firstName: 'Grammar', lastName: 'Corrah' } function checkFunc() { for (let element in grammarcorrah) { document.getElementById("demo1").innerHTML = element; } } https://code.sololearn.com/WduGP0uCQux1/?ref=app
3 Answers
+ 3
You are replacing firstName with lastName by loop. Just use like this:
let grammarcorrah = {
firstName: 'Grammar',
lastName: 'Corrah'
}
function checkFunc() {
document.getElementById("demo1").innerHTML = grammarcorrah.firstName;
document.getElementById("demo2").innerHTML = grammarcorrah.lastName;
}
//document.getElementById("demo1").innerHTML = grammarcorrah['firstName']; // index method
+ 2
grammercorrah.firstName
grammercorrah['firstName']
+ 1
Thank you my _black belt coders_ for your help