Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

123
Views
What is the difference between two coding methods below?
elem = document.getElementById("demo"); 
elem.innerHTML = x;           

WHY I CAN NOY WRITE THE CODE ABOVE LIKE THIS:

elem = document.getElementById("demo").innerHTML; 
elem = x;           
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Understand that variable names are just pointers to data in memory

If you understand this you'll be a long way to understanding that in the top, where elem is a label for an HTMLElement, it will set the innerHTML of the element. In the bottom elem is just a string. But more than that, when you set elem to x, you are just setting a new value in the label, not changing the element.

about 4 years ago · Juan Pablo Isaza Report

0

They are not the same thing. the first one:

elem = document.getElementById("demo"); 
elem.innerHTML = x;

elem is now an object, an object that is referencing the Element with the demo ID. the next line you are swapping the current value of innerHTML to whatever is the value of x;

The second one:

elem = document.getElementById("demo").innerHTML; 
elem = x;

The elem is a string, the HTML string that is inside the demo element ID is now saved on elem. The second line replaces the string inside elem with the value of x, however the innerHTML of the demo element remains unchanged.

about 4 years ago · Juan Pablo Isaza Report

0

// stores reference to DOM element #demo
elem = document.getElementById("demo");

// changes #demo innerHTML to "x" -- affects the DOM
elem.innerHTML = x;




// stores #demo content as a string
elem = document.getElementById("demo").innerHTML;

// changes the string to "x" -- does not affect the DOM
elem = x;
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!