0
 </!DOCTYPE html>
</html>
</body>
<p>Given that y=5, calculate x=++y, and display the result.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction()
{
var y=5;
var x=++y;
var demoP=document.getElementById("demo")
demoP.innerHTML="x=" + x + ", y=" + y;
}
</script>
<p><strong>Note:</strong> Both variables, x and y, are affected.</p>
</body>
</html>*/

Clearly, I'm a beginner but I have no one else to ask. It would be helpful if someone could explain the reason behind demop.innerHTML="x=" + x + ", y=" +y; in this code.

mjk
2,4514 gold badges34 silver badges33 bronze badges
asked Feb 3, 2013 at 15:36
3
  • the reason is on the <p> tag. Commented Feb 3, 2013 at 15:38
  • Not the answer, but </!DOCTYPE html>, and your opening html and body tags should not have a forward slash. There is an extraneous */ at the end of the document as well? Commented Feb 3, 2013 at 15:47
  • the answer is i neglected to look over what is there. the poor writing came into me putting it up bad but had nothing to do with my problem Commented Feb 3, 2013 at 16:36

3 Answers 3

4

it would be helpful if someone could explain the reason behind demop.innerHTML="x=" + x + ", y=" +y; in this code

That line of code doesn't increment anything, in that line the + is a string concatenation operator, not a number addition operator. It's used for building up a string (it doesn't change x or y), which is then assigned to demoP.innerHTML, which replaces the content of the DOM element with that string's contents.

The line that's a bit harder to understand for a beginner is this one:

var x=++y;

That does three things:

  1. It declares a variable called x in the current scope (var).

  2. It increments the value of y.

  3. It assigns the incremented value to x.

So both x and y end up with 6, since the increment happens before the value of y is used to initialize x. This is called a "prefix increment". "Pre" because it happens before we use the value for something.

Like most languages that derive their main syntax from B (so, C, C++, Java, C#, JavaScript, and many others), there's also a "postfix" increment:

var x = y++;

"Post" because we increment after using the value. If the line were as above, x would get 5 (y's old value) and y would get 6.

answered Feb 3, 2013 at 15:38
Sign up to request clarification or add additional context in comments.

1 Comment

@JordanBeidatsch: No worries! Part of being a beginner. Glad this helped, anyway.
1
demoP.innerHTML = "x = " + x + ", y = " + y;

This sets the HTML of the element identified by demoP. + will concatenate the strings and the numbers into one string which will be the HTML of demoP.

answered Feb 3, 2013 at 15:38

Comments

0

When you click the button "Try it" the function myFunction() is executed and the HTML inside the element with id demo is changed to: x=6, y=6

answered Feb 3, 2013 at 15:39

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.