<!DOCTYPE html> <html> <body> <p>Sum of <span id="one">10</span> and <span id="two">20</span> is <span id="res"></span></p> <script> var numOne = parseInt(document.getElementById("one").innerHTML); var numTwo = parseInt(document.getElementById("two").innerHTML); var result = numOne + numTwo; document.getElementById("res").innerHTML = result; </script> </body> </html>
Sum of 10 and 20 is
In above example, after initializing the value to variable say numOne, writing numOne anywhere in the program, means writing the value inside it or represents the value stored in it.
- var Variable
- let Variable
- const Variable
Note - Use var to declare a global variable, that can be updated and re-declared.
Note - Use let to declare a block-scoped variable that can be updated, but can not be re-declared.
Note - Use const to declare a block-level variable that can neither be updated nor be re-declared. A const variable must be initialized with a value at the time of its declaration.
- Variable name must starts with a letter (A-Z or a-z), a dollar sign ($), or an underscore (_)
- Use letters, digits (0-9), underscores, and dollar signs to name a variable
- Do not use any keyword to name a variable
« Previous Tutorial Next Tutorial »