Advertisement
Guest User

Untitled

a guest
Jan 16th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.72 KB | None | 0 0
  1. /*
  2. * DATATYPES:
  3. *
  4. * 0. Javascript can accomodate different types of data in primitive and complex forms. When declaring
  5. * a variable, it is not necessary to declare a datatype. The type of variable will be automatically
  6. * determined during program execution.
  7. *
  8. * 1. Primitive types utilize physical memory in that, each value is stored within the 8 bytes allocated
  9. * for each variable.
  10. *
  11. * 2. Complex types are not stored in memory directly. Since they do not have a fixed size, only the
  12. * reference to the value is stored in each variable.
  13. */
  14.  
  15. /*
  16. * PRIMITIVE DATATYPES
  17. */
  18. // Number
  19. var a = 25.4; // can include decimals
  20. var b = 123e-5; // scientific notation used for larger numbers
  21.  
  22. // String
  23. var c = "Sample string of characters!"; // a sequence of characters enclosed in quotations
  24.  
  25. // Boolean
  26. var d = true; // can be one of two states: true or false
  27. var e = false;
  28.  
  29. // Undefined
  30. var f; // When a variable is declared without a value, the default value is 'undefined'.
  31. console.log(f);
  32.  
  33. // Null
  34. var g = null; // Null means "nothing". The Value is null, but the type is considered an object.
  35.  
  36. // NaN
  37. var h = NaN; // "Not a number" is still considered a 'number' type despite its name. NaN is returned
  38. console.log("pete"/5); // when something cannot be converted in to a number type.
  39.  
  40. // Infinity
  41. // As in mathematics, any division by zero will result in an "infinity" type.
  42. // If the dividend is a negative number, the operation will result in "-infinity"
  43. var i = -5/0;
  44. console.log(i);
  45.  
  46. /*
  47. * COMPLEX DATATYPES
  48. */
  49. // Array
  50. // A sequence of values enclosed in square brackets and separated by commas. Each value has an index
  51. // which is '0' for the first value in the array.
  52. //
  53. var j = [4, 6, 30, 9, 80]; // the variable 'k' stores the reference to the memory location for the array
  54. var k = ["Frank", "Jerry", "Kim"]; // each value can be accessed using l[index]
  55. console.log(j[3]); // prints the 4TH value in array 'k'.
  56.  
  57. // Object
  58. // Similar to array, but instead of a sequence of single values, an object is a sequence of
  59. // 'name':'value' pairs. Instead of an index, the values are accessd by their corresponding 'name'.
  60. //
  61. var l = {
  62. id: 42,
  63. color: "gray",
  64. type: "laminated",
  65. };
  66. console.log(l.color); // prints "gray"
  67.  
  68. // Function
  69. // A section of code that can be re-used and called upon if needed. Using functions is a 2 step
  70. // process: Declaration and Execution. Values can be 'passed' to the function using parameters in the
  71. // funciton declaration. We can call a funciton many times without having to write the same code
  72. // over and over
  73. //
  74. function test(number) { // Declaration
  75. console.log("Testing " + number);
  76. }
  77. test(5); // Execution - Prints "Testing 5"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement