Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.12 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta name="description" content="javascript variables">
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width">
  7. <title>Variables</title>
  8. </head>
  9. <body>
  10.  
  11. <script id="jsbin-javascript">
  12. /*
  13. * Variables:
  14. *
  15. * Variables are used to hold things in memory.
  16. * They are named at declaration and assigned a value at initialization/assignment
  17. */
  18.  
  19. // To declare a variable:
  20. var myVar;
  21.  
  22. // To assign a variable:
  23. myVar = 'some value';
  24.  
  25. // note: can be combined where on the left side of the equal sign the variable is declared and on the right side the variable is assigned:
  26. var myOtherVar = 'some other value';
  27.  
  28.  
  29. // Trying to access a variable that has been declared, but not assigned will reture undefined:
  30. var theVar;
  31. console.log(theVar); // > undefined
  32. theVar = 2;
  33. console.log(theVar); // > 2
  34.  
  35. // Variables can be re-assigned to another value:
  36. console.log(theVar);
  37. theVar = 5.5; // theVar is now set to 5.5 not 2
  38. console.log(theVar);
  39.  
  40.  
  41. // When variables are assigned, the are given a type, and at re-assignment the variable can be re-assigned to any type
  42. theVar = "a string";
  43. theVar = [];
  44. theVar = {};
  45. </script>
  46.  
  47.  
  48.  
  49. <script id="jsbin-source-javascript" type="text/javascript">/*
  50. * Variables:
  51. *
  52. * Variables are used to hold things in memory.
  53. * They are named at declaration and assigned a value at initialization/assignment
  54. */
  55.  
  56. // To declare a variable:
  57. var myVar;
  58.  
  59. // To assign a variable:
  60. myVar = 'some value';
  61.  
  62. // note: can be combined where on the left side of the equal sign the variable is declared and on the right side the variable is assigned:
  63. var myOtherVar = 'some other value';
  64.  
  65.  
  66. // Trying to access a variable that has been declared, but not assigned will reture undefined:
  67. var theVar;
  68. console.log(theVar); // > undefined
  69. theVar = 2;
  70. console.log(theVar); // > 2
  71.  
  72. // Variables can be re-assigned to another value:
  73. console.log(theVar);
  74. theVar = 5.5; // theVar is now set to 5.5 not 2
  75. console.log(theVar);
  76.  
  77.  
  78. // When variables are assigned, the are given a type, and at re-assignment the variable can be re-assigned to any type
  79. theVar = "a string";
  80. theVar = [];
  81. theVar = {};
  82. </script></body>
  83. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement