Guest User

Untitled

a guest
Nov 19th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 KB | None | 0 0
  1. ```javascript
  2. // JavaScript CS
  3.  
  4. // convert to string
  5. // --------------------------------------------------------------------
  6.  
  7. let myNumber = 9;
  8.  
  9. let myString = String(score);
  10.  
  11. // convert to number
  12. // --------------------------------------------------------------------
  13.  
  14. let myString = '44';
  15.  
  16. let myNumber = Number(myString);
  17.  
  18. // convert to boolean
  19. // --------------------------------------------------------------------
  20.  
  21. let age = 0;
  22.  
  23. // good
  24. let hasAge = Boolean(age);
  25.  
  26. // best
  27. let hasAge = !!age;
  28.  
  29. // array map - Array.prototype.map()
  30. // --------------------------------------------------------------------
  31.  
  32. let numbers = [1, 5, 10, 15];
  33. let doubles = numbers.map(function(x) {
  34. return x * 2;
  35. });
  36. // doubles is now [2, 10, 20, 30]
  37. // numbers is still [1, 5, 10, 15]
  38.  
  39. // es6 example
  40. let numbers = [2, 4, 8, 10];
  41. let halves = numbers.map(x => x / 2);
  42. // halves is now [1, 2, 4, 5]
  43. // numbers is still [2, 4, 8, 10]
  44.  
  45. let numbers = [1, 4, 9];
  46. let roots = numbers.map(Math.sqrt);
  47. // roots is now [1, 2, 3]
  48. // numbers is still [1, 4, 9]
  49.  
  50.  
  51. // array filter - Array.prototype.filter()
  52. // --------------------------------------------------------------------
  53.  
  54. let words = ["spray", "limit", "elite", "exuberant", "destruction", "present", "happy"];
  55.  
  56. let longWords = words.filter(function(word){
  57. return word.length > 6;
  58. });
  59.  
  60. // filtered array longWords is ["exuberant", "destruction", "present"]
  61.  
  62. // array find - Array.prototype.find()
  63. // --------------------------------------------------------------------
  64.  
  65. function isBigEnough(element) {
  66. return element >= 15;
  67. }
  68.  
  69. [12, 5, 8, 130, 44].find(isBigEnough); // 130
  70.  
  71. // array slice - Array.prototype.slice()
  72. // --------------------------------------------------------------------
  73.  
  74. let a = ['zero', 'one', 'two', 'three'];
  75. let sliced = a.slice(1, 3);
  76.  
  77. console.log(a); // ['zero', 'one', 'two', 'three']
  78. console.log(sliced); // ['one', 'two']
  79.  
  80.  
  81. // array splice - Array.prototype.splice()
  82. // --------------------------------------------------------------------
  83.  
  84. let myFish = ['angel', 'clown', 'mandarin', 'sturgeon'];
  85.  
  86. myFish.splice(2, 0, 'drum'); // insert 'drum' at 2-index position
  87. // myFish is ["angel", "clown", "drum", "mandarin", "sturgeon"]
  88.  
  89. myFish.splice(2, 1); // remove 1 item at 2-index position (that is, "drum")
  90. // myFish is ["angel", "clown", "mandarin", "sturgeon"]
  91. ```
Add Comment
Please, Sign In to add comment