Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.45 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width">
  6. <title>i. Variables</title>
  7. </head>
  8. <body>
  9.  
  10. <script id="jsbin-javascript">
  11. /*
  12. [Variables]
  13.  
  14. Variables are containers for holding values.
  15. Without variables the values in our program would disappear and we
  16. wouldn't be able to use them again.
  17.  
  18. [Declaring and intializing a variable]
  19. The first step to creating a variable is using the keyword var.
  20. The second step is to give our variable a name that indicates
  21. what the values in the variable consist of.
  22.  
  23.  
  24. //example //
  25.  
  26. */
  27. var color;
  28.  
  29. /* The code above uses the keyword var, which lets the interpreter know that
  30. "color" is a variable.
  31. We then named our variable color which would let us, or someone in the future viewing
  32. our program know that the values in this variable probably have something to do with
  33. colors.
  34. Variables should always begin with a lowercase letter and the first letter of each following
  35. word should be capitalized. This is called camelCase.
  36. // example //
  37. */
  38. var everyFirstLetterOfANewWordShouldBeCapitalized;
  39. /*There are certain words that Javascript has reserved, called keywords, that should never be used for
  40. your variable names. A few of these keywords are var, const, let, function. These words mean other
  41. things to the browser so it's best to just not use them when declaring a variable.
  42. /*
  43.  
  44. After you declare the variable you have to initialize it.
  45. Initializing the variable means you give it a value.
  46. Without initializing the variable by default the value is undefined,
  47. because you didn't define it.
  48. To intialize a value you use the '=' operator and you give the variable a value.
  49.  
  50. //example // */
  51.  
  52. var color = red;
  53.  
  54. /* In the above example we used the assignement operator ( = ) and gave our variable
  55. a value of red.
  56.  
  57. [Variable Reassignment]
  58. Variables can be reassigned to different values.
  59. */
  60. // example //
  61.  
  62. var color = red;
  63. console.log(color);
  64. // the console would print out red
  65.  
  66. var color = blue;
  67. console.log(color);
  68. /* we have reassigned our variable color to have a value of blue so the console
  69. would print out blue
  70. */
  71.  
  72. /* [What type of data variables store]
  73. Variables can store any kind of data.
  74. When a variable stores a primitive (number, string, boolean, undefined, null)
  75. that value is stored directly into that variable.
  76. When a variable stores a complex data type (arrays, objects) the variable
  77. points to a place in memory.
  78.  
  79. The reason that primitives and complex data types are stored differently is because
  80. primitive values have a fixed number in memory whereas complex data types can grow in size.
  81. In other words, primitive data types are stored exactly at the variable
  82. while complex data types reference to a data value.
  83.  
  84. A good visual example I read was that primitive values are like your mom writing on a
  85. sticky note an item or two for you to pick up
  86. at the grocery store. The items are written directly on the sticky
  87. note. This would be the equivalent of storing a primitive type.
  88.  
  89. Whereas if your mom wanted you to go grocery shopping she might have written out
  90. a long list that's too big for the size of the sticky note. So on the sticky note she might reference
  91. you to another place where the grocery list is stored. So on the sticky note she might write
  92. "The grocery list is stored inside of the kitchen drawer". That is similar to how a complex data type is stored.
  93. // code example//
  94. */
  95. var color = blue;
  96. console.log(blue);
  97. // blue is stored directly in the container color
  98.  
  99. var colorObj = {
  100. darkColor: black,
  101. mediumColor: tan,
  102. lightColor: baby blue
  103. }
  104.  
  105. /* Unlike the color array which pointed us directly to our one value of
  106. blue, colorObj points us to
  107. an object that holds 3 different values.
  108. */
  109.  
  110. /* [Variable Scope]
  111. Variables are either local scope (within a function) or global scoped (outside
  112. a function).
  113.  
  114. //example of local scope function//
  115.  
  116. */
  117. function () {
  118. // variables with local scope go inside of here //
  119. //this means that the variable can only be accessed inside of this function//
  120. }
  121.  
  122. //example of global scope //
  123.  
  124. var color = "blue";
  125. //color is accessible outside of the function below
  126.  
  127. function () {
  128. //color is also accesible inside of this function//
  129. }
  130.  
  131. /* [Why we use variables]
  132. Another upside of variables is that they save time.
  133. Instead of retyping what a value is every single time, we can simply reference the variable.
  134. Imagine typing a bunch of never ending objects.
  135. Referencing a value by a name convinient because even if the variables value changes or is extremely long
  136. all we have to do is reference it by its name. This also makes it easy to manipulate our value. Instead of
  137. manually changing all instances of a value, we can just change the variable which would change all of the
  138. values for us.
  139.  
  140. //example//
  141. */
  142. /* now if we wanted to change the above color we could just change the variable color, instead
  143. of changing the values in the colorScheme variable */
  144.  
  145. var color = "blue";
  146.  
  147. var colorScheme = "The color scheme is + ' ' + color"
  148. console.log(colorScheme); // the console would print out "The color scheme is blue.
  149.  
  150. var color = "red";
  151. console.log(colorScheme) // the console would now print out "The color scheme is red"
  152. //we didn't have to manipulate anything in the colorScheme variable.
  153.  
  154.  
  155.  
  156. /* [Constant]
  157.  
  158. ES6, a newer version of Javascript, has a new variable called 'const'.
  159. The const variable is:
  160. - block scoped
  161. - cannot be reassigned new values
  162. - hoisted to the top of the block scope
  163. -must be assigned a value at runtime
  164.  
  165. This is different from a regular variable because regular variables are
  166. function scoped.
  167.  
  168. //example //
  169. */
  170. if (true ) {
  171. const scopeExample = "I am block scoped"
  172. const scopeExample = "I cannot be reassigned a new value"
  173. console.log(const); // the interpreter would say something along the lines of
  174. //'scopeExample has already been declared'//
  175. }
  176.  
  177. /*The above example shows that const is block scoped.
  178. It also shows that you cannot reassign a new value, the interpreter would
  179. tell you that scopeExample has already been declared.
  180.  
  181. /* [let]
  182. Javascript ES6 has another new variable by the name of 'let'.
  183. The let variable
  184. - is scope blocked
  185. - can be reassigned another value
  186. The difference between 'var' and 'let' is there scope.
  187. var's scope is the entire function, while lets scope is block scoped.
  188.  
  189. */
  190.  
  191. function varTest() {
  192. var x = 3;
  193. if (true) {
  194. var x = 2; // vars scope is the WHOLE function, so reassigning var in one part of the function,
  195. // changes the variable in the whole function
  196. console.log(x); // 2
  197. }
  198. console.log(x); // 2
  199. }
  200.  
  201. function letTest() {
  202. let x = 3;
  203. if (true) {
  204. let x = 2 // let is block scoped. let exists as a completely new variable. lets value
  205. // is 2 ONLY inside of this BLOCK. when in block scope let is in its own world.
  206. console.log(x) // 2
  207. }
  208. console.log(x) // let's value is still 3 since it was declared outside of the block.
  209. }
  210. </script>
  211.  
  212.  
  213.  
  214. <script id="jsbin-source-javascript" type="text/javascript">
  215. /*
  216. [Variables]
  217.  
  218. Variables are containers for holding values.
  219. Without variables the values in our program would disappear and we
  220. wouldn't be able to use them again.
  221.  
  222. [Declaring and intializing a variable]
  223. The first step to creating a variable is using the keyword var.
  224. The second step is to give our variable a name that indicates
  225. what the values in the variable consist of.
  226.  
  227.  
  228. //example //
  229.  
  230. */
  231. var color;
  232.  
  233. /* The code above uses the keyword var, which lets the interpreter know that
  234. "color" is a variable.
  235. We then named our variable color which would let us, or someone in the future viewing
  236. our program know that the values in this variable probably have something to do with
  237. colors.
  238. Variables should always begin with a lowercase letter and the first letter of each following
  239. word should be capitalized. This is called camelCase.
  240. // example //
  241. */
  242. var everyFirstLetterOfANewWordShouldBeCapitalized;
  243. /*There are certain words that Javascript has reserved, called keywords, that should never be used for
  244. your variable names. A few of these keywords are var, const, let, function. These words mean other
  245. things to the browser so it's best to just not use them when declaring a variable.
  246. /*
  247.  
  248. After you declare the variable you have to initialize it.
  249. Initializing the variable means you give it a value.
  250. Without initializing the variable by default the value is undefined,
  251. because you didn't define it.
  252. To intialize a value you use the '=' operator and you give the variable a value.
  253.  
  254. //example // */
  255.  
  256. var color = red;
  257.  
  258. /* In the above example we used the assignement operator ( = ) and gave our variable
  259. a value of red.
  260.  
  261. [Variable Reassignment]
  262. Variables can be reassigned to different values.
  263. */
  264. // example //
  265.  
  266. var color = red;
  267. console.log(color);
  268. // the console would print out red
  269.  
  270. var color = blue;
  271. console.log(color);
  272. /* we have reassigned our variable color to have a value of blue so the console
  273. would print out blue
  274. */
  275.  
  276. /* [What type of data variables store]
  277. Variables can store any kind of data.
  278. When a variable stores a primitive (number, string, boolean, undefined, null)
  279. that value is stored directly into that variable.
  280. When a variable stores a complex data type (arrays, objects) the variable
  281. points to a place in memory.
  282.  
  283. The reason that primitives and complex data types are stored differently is because
  284. primitive values have a fixed number in memory whereas complex data types can grow in size.
  285. In other words, primitive data types are stored exactly at the variable
  286. while complex data types reference to a data value.
  287.  
  288. A good visual example I read was that primitive values are like your mom writing on a
  289. sticky note an item or two for you to pick up
  290. at the grocery store. The items are written directly on the sticky
  291. note. This would be the equivalent of storing a primitive type.
  292.  
  293. Whereas if your mom wanted you to go grocery shopping she might have written out
  294. a long list that's too big for the size of the sticky note. So on the sticky note she might reference
  295. you to another place where the grocery list is stored. So on the sticky note she might write
  296. "The grocery list is stored inside of the kitchen drawer". That is similar to how a complex data type is stored.
  297. // code example//
  298. */
  299. var color = blue;
  300. console.log(blue);
  301. // blue is stored directly in the container color
  302.  
  303. var colorObj = {
  304. darkColor: black,
  305. mediumColor: tan,
  306. lightColor: baby blue
  307. }
  308.  
  309. /* Unlike the color array which pointed us directly to our one value of
  310. blue, colorObj points us to
  311. an object that holds 3 different values.
  312. */
  313.  
  314. /* [Variable Scope]
  315. Variables are either local scope (within a function) or global scoped (outside
  316. a function).
  317.  
  318. //example of local scope function//
  319.  
  320. */
  321. function () {
  322. // variables with local scope go inside of here //
  323. //this means that the variable can only be accessed inside of this function//
  324. }
  325.  
  326. //example of global scope //
  327.  
  328. var color = "blue";
  329. //color is accessible outside of the function below
  330.  
  331. function () {
  332. //color is also accesible inside of this function//
  333. }
  334.  
  335. /* [Why we use variables]
  336. Another upside of variables is that they save time.
  337. Instead of retyping what a value is every single time, we can simply reference the variable.
  338. Imagine typing a bunch of never ending objects.
  339. Referencing a value by a name convinient because even if the variables value changes or is extremely long
  340. all we have to do is reference it by its name. This also makes it easy to manipulate our value. Instead of
  341. manually changing all instances of a value, we can just change the variable which would change all of the
  342. values for us.
  343.  
  344. //example//
  345. */
  346. /* now if we wanted to change the above color we could just change the variable color, instead
  347. of changing the values in the colorScheme variable */
  348.  
  349. var color = "blue";
  350.  
  351. var colorScheme = "The color scheme is + ' ' + color"
  352. console.log(colorScheme); // the console would print out "The color scheme is blue.
  353.  
  354. var color = "red";
  355. console.log(colorScheme) // the console would now print out "The color scheme is red"
  356. //we didn't have to manipulate anything in the colorScheme variable.
  357.  
  358.  
  359.  
  360. /* [Constant]
  361.  
  362. ES6, a newer version of Javascript, has a new variable called 'const'.
  363. The const variable is:
  364. - block scoped
  365. - cannot be reassigned new values
  366. - hoisted to the top of the block scope
  367. -must be assigned a value at runtime
  368.  
  369. This is different from a regular variable because regular variables are
  370. function scoped.
  371.  
  372. //example //
  373. */
  374. if (true ) {
  375. const scopeExample = "I am block scoped"
  376. const scopeExample = "I cannot be reassigned a new value"
  377. console.log(const); // the interpreter would say something along the lines of
  378. //'scopeExample has already been declared'//
  379. }
  380.  
  381. /*The above example shows that const is block scoped.
  382. It also shows that you cannot reassign a new value, the interpreter would
  383. tell you that scopeExample has already been declared.
  384.  
  385. /* [let]
  386. Javascript ES6 has another new variable by the name of 'let'.
  387. The let variable
  388. - is scope blocked
  389. - can be reassigned another value
  390. The difference between 'var' and 'let' is there scope.
  391. var's scope is the entire function, while lets scope is block scoped.
  392.  
  393. */
  394.  
  395. function varTest() {
  396. var x = 3;
  397. if (true) {
  398. var x = 2; // vars scope is the WHOLE function, so reassigning var in one part of the function,
  399. // changes the variable in the whole function
  400. console.log(x); // 2
  401. }
  402. console.log(x); // 2
  403. }
  404.  
  405. function letTest() {
  406. let x = 3;
  407. if (true) {
  408. let x = 2 // let is block scoped. let exists as a completely new variable. lets value
  409. // is 2 ONLY inside of this BLOCK. when in block scope let is in its own world.
  410. console.log(x) // 2
  411. }
  412. console.log(x) // let's value is still 3 since it was declared outside of the block.
  413. }
  414.  
  415.  
  416. </script></body>
  417. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement