Console.log & document.getElementById()


#1

what is deference between console.log & document.getElementByid:
if I ues document.getElementByid ill gave me undefined
Code:

<body>
  <p id="demo"></p>
 
  <script>
    var friends = {};
    friends.bill = {
      firstName: "Bill",
      lastName: "Gates",
      number: "(206) 555-5555",
      address: ['Microsoft Way']
      }
    friends.steve = {
      firstName: "Steve",
      lastName: "Jobes",
      number: "(444) 111 000",
      address: ["Apple way"]
    }
    function list(obj) {
      for( var key in obj){
        console.log(key);
      }
    }
    document.getElementById("demo").innerHTML = list(friends);

    function search(name) {
      for(var key in friends){
        if(name === friends[key].firstName){
          console.log(friends[key]);
        }
      }
    }
    search("Steve");
    
  </script>

</body>

#3

what is deference between console.log & document.getElementByid:
if I ues document.getElementByid ill gave me undefined
Code:


#4

list is a function, you can't directly add it to a html document with innerHTML, you would need to add the document....innerHTML inside your list function:

function list(obj){
  for (var key in obj){
    document.....innerHTML = key;
  }
}

you might need to use some form of append instead, but i will leave that for you to figure out.

Console.log will log things to the console (mostly used for debugging) and innerHTML will actually add something to the webpage


#5

I do it like that he only gave me steve answer:
function list(obj) {
for( var key in obj){
document.getElementById("demo").innerHTML = key + "
";
}
}
list(friends);