let intrestrate = 0.3;
intrestrate = 1;
console.log(intrestrate);
let age = 20 ;
console.log(age);
document.getElementById(“demo”).innerHTML = “The total is” + intrestrate + age;
var z = intrestrate + age;
document.getElementById(“Hello”).innerHTML = "The total is " + z;
alert( "The total is " + z);
Basically all above is my coding. The first one in which I have displayed the total in the div id “demo”, the answer is 120 while in the second one in which the total is displayed in the div id “Hello”, the answer is 21 which is the correct answer. Can anyone please explain why is the first one answer not coming correct ??
Note: I have not shown my html coding. I have only made the div ids in it and thats all. Nothing else is written in my html coding.
I’m sorry, I don’t really understand your explanation. I’m also confused - Are you saying that you haven’t written any HTML code? If you have, we need to see that. If you haven’t, then that’s your problem.
You don’t really say what the error with document.getElementById(“demo”).innerHTML is.
I’m assuming that it is some kind of null pointer exception. If document.getElementById(“demo”) returns undefined (in other words, it can’t find that id in the document) then the .innerHTML will throw an error because you cannot access properties on a null or undefined. (Technically you can’t on any primitive, but the others have object wrappers that prevent NPEs.)