Jump to content

Javascript Arithmetic Example Doubt


shaijuelayidath

Recommended Posts

I was refering w3schools Javascript Tutorials,in 'Javascript Arithmetic' section i found a different type of 'var' delcarationactually i did't understand, because it is not refered in the tutorial, the code I given below............................................................................................... <script type="text/javascript">function myFunction(){var y=5;var x=y+2;var demoP=document.getElementById("demo")demoP.innerHTML="x=" + x;}</script>...............................................................................................In the above example i did't understand the below lines, var demoP=document.getElementById("demo")demoP.innerHTML="x=" + x; anyone can define it Thank You

  • Like 1
Link to comment
Share on other sites

This probably would of been a better idea to post in the JavaScript forum.. What the above is doing is... var y is set to 5.var x is set to y, which is 5 then added 2 to it resulting in x being set to 7.var demoP is set to the element with the id of "demo".demoP.innerHTML = "x = " + x; sets the 'content' of that element to be/display "x = 7". "x = " is a string, then the plus sign/operator concatenates the value of variable x which is 7 resulting in saying "x = 7" So if the element is a div container with an id of "demo", it will look like this basically: <div id="demo">x = 7</div>

Link to comment
Share on other sites

first line is calling method getElementByID() od document object which returns a node. which is stored in demoP.innerHTML is attribute of demoP. attributes are accessed by dot operator.

Link to comment
Share on other sites

Thank you for the replies (DonE, Birbal)after the first line there should be a semi colon ";" is'nt it ?in w3c there is not semi colon after the first 'var' delcarion line, but second line is having, is it required or not for the first line ? var demoP=document.getElementById("demo")demoP.innerHTML="x=" + x; wats your comment...

Link to comment
Share on other sites

in Js lines are terminated automatically on new line. so it is not must to terminate expressions explicitly.but it is best practice to terminate expression and should do it.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...