How do i render variables to show on the document DOM

for functions, you can just do:
define the function :

if statement ... etc.
then after write:
let result = testLogic(34);
document.write(result); 

and it will print the result from the if statement

how do i print like a list from a variable?
so if i have :

var list = {
name: "Henry",
occupation: "Builder",
height: "6ft" };
let displayList = list /// this wouldn't work
document.write(displayList); ///not working.

all that is rendered is [object object] it says

This is kind of similar to a problem i ran into. If i fully understand what you are saying, the problem is that you are trying to write an object instead of a property in the object, hence the coersion. JavaScript converts it to a string.

Well if you call the things in the list its properties, i want to print the whole list onto the browser page, not the console. Isn’t that the point of JavaScript, it needs to communicate to the user?.

You can’t print it like that. in fact, using multiple lines of document.write will only rewrite the whole page with the last one.
For instance, to access one of the properties you can use the dot notation:

    var list = {
  name: "Henry",
  occupation: "Builder",
  height: "6ft" };
  let displayList = list 
  document.write(displayList.name) //Henry
  /* using just document.write(displayList) will make javascript force the object to become a string*/

If you are wanting to write everything, you should use another method, like dynamically inserting them into the DOM:

var list = {
  name: 'Henry',
  occupation: 'Builder',
  height: '6ft'
};

let displayList = list;

for (const key in displayList) { // goes through all the enumerable keys the object
  const myNode = document.createElement('li'); 
  myNode.textContent = displayList[key];
  document.body.append(myNode);
}

1 Like

thank you, the first bit works and Henry is outputted on the browser webpage. However the second bit of code, there’s an error and nothing is outputted.

That should probably be due to your browser version - i just ran the code. Try upgrade your.

1 Like

Its all upgraded, it says

document.body.append(myNode);

Uncaught typeerror: cannot read property append of null , for ^this line