Advertisement
Guest User

Untitled

a guest
Mar 26th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.84 KB | None | 0 0
  1. /* DATATYPES:
  2.  
  3. Different types of values that Javascript recognizes. There are two types
  4. of datatypes; simple and complex.
  5.  
  6. Simple datatypes are values that DO NOT aggregate or collect other values;
  7. they are just one value. This means they're immutable. When they are are
  8. assigned or passed they are copied from one variable to the next by their
  9. values.
  10.  
  11. Examples of Simple Datatypes:
  12. 1.String
  13. 2.Number
  14. 3.Boolean
  15. 4.Undefined
  16. 5.Null
  17. 6.NaN (Not a Number)
  18.  
  19. */
  20.  
  21. //Strings are text values wrapped in quotation marks
  22. 'use strict';
  23.  
  24. var myString = 'Hi There!';
  25.  
  26. //Numbers are numberic values
  27. var num = 10;
  28.  
  29. //Booleans are values that represent true or false
  30. var myBoolean = true;
  31. console.log(myBoolean); //prints true value
  32. myBoolean = false;
  33. console.log(myBoolean); //prints false value
  34.  
  35. //Undefined is a datatype that represents the non-existence of a value
  36. //This is typically assigned by Javascript
  37.  
  38. var notDeclaredVar;
  39. console.log(notDeclaredVar); //prints undefined
  40.  
  41. /* Null is another datatype that represents the non-existence of a value
  42. but it is intentionally set by the programmer to be 'nothing'.
  43. */
  44.  
  45. var nullValue = null;
  46. console.log(nullValue);
  47.  
  48. //NaN (Not a Number) is value the cannot be evaluated to be a numberic value.
  49. //It gets returned when mathmatical functions fail.
  50.  
  51. /* Infinity is a datatype that represents a value that is greater
  52. than any other number; anyhting multiplied by Infinity is Infinity
  53. and anything divided by Infinity is 0.
  54.  
  55. -Infinity is a datatype that represents an infinite value but acts
  56. differently from Posiitve Infinity.
  57.  
  58. Any positive number including Infinity multiplied by -Infinity results in
  59. -Infinity; any negative number including -Infinity results in Infinity.
  60. 1.Zero multiplied by -Infinity is NaN.
  61. 2.NaN multiplied by -Infinity is NaN.
  62. 3.-Infinity, divided by any negative value except -Infinity, is Infinity.
  63. 4.-Infinity, divided by any positive value except Inifinity, is -Infinity.
  64. 5.-Infinity, divided by either -Infinity or Infinity, is NaN.
  65. 6.Any number divided by -Infinity is zero.
  66.  
  67. */
  68.  
  69. /*Complex datatypes include objects, arrays, and functions. They're complex
  70. because they are mutable which means they DO collect other values; therefore
  71. have an indefinite size.
  72.  
  73. When they are assigned to variables or passed through function
  74. they are copied by reference because they can be so large in size.
  75.  
  76. */
  77.  
  78. //Example:
  79. var myObject = {
  80. name: 'value',
  81. key: 'value'
  82. };
  83. console.log(myObject); //prints the object with two key value pairs
  84.  
  85. var myArray = [1, 2, 3, 4, 5];
  86. console.log(myArray); // prints the array that contains the numbers 1-5
  87.  
  88. function doSomething(a) {
  89. a += 1;
  90. }
  91.  
  92. var z = 1;
  93. doSomething(z);
  94.  
  95. console.log(z); //prints 1 not 2, the value of variable z is copied through
  96. // calling the function doSomething().
  97.  
  98. /* Many datatypes have different properties and methods attached to them.
  99. properties (things they're made of) / methods (things they can do).
  100.  
  101. For example an array has a length property that tells how many values
  102. are stored in an array we can use the .length method access this information.
  103. */
  104.  
  105. //Example:
  106. console.log(myArray.length); //prints the number 5 because there 5 values
  107. //held in that array.
  108. /* Functions are blocks of code that hold 'instructions' telling the
  109. computer what to do and how to get it done. They are declared with
  110. the keyword function followed by a set of parentheses and curly braces.
  111. The instructions for what to do go inside the curly braces.
  112.  
  113. Sometimes functions take parameters/arguments; these are values or
  114. 'information' that the function needs in order to get the job done.
  115.  
  116. Parameters are basically named variables while arguements are the actual
  117. values used.
  118.  
  119. */
  120. //Example:
  121. /*This function passes two numbers as arguments, adds them together
  122. and then returns the sum.
  123. */
  124. function add(numOne, numTwo) {
  125. var sum = numOne + numTwo;
  126. return sum;
  127. }
  128.  
  129. console.log(add(1, 2)); //prints the number 3
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement