SHARE
TWEET

backup

WhiteofNotGrey Sep 6th, 2019 261 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //The three variables : ~~var~~, let and const
  2. // Let allows you to reassign values
  3. // Const cannot be changed/reassigned
  4. // Stick to const, unless you have to reassign values, e.g a score
  5.  
  6. let age = 30;
  7. age = 31;
  8.  
  9. const name = "Hi";
  10. console.log(age);
  11.  
  12. //String, numbers, boolean, null, undefined, symbol
  13.  
  14. const stringName = "This is a string";
  15. const numberName = 9;
  16. const decimalNumber = 9.5;
  17. const thisisBoolean = true;
  18. const x = null;
  19. const y = undefined;
  20. let z //this is undefined
  21.  
  22. console.log (typeof stringName);
  23.  
  24. /*Concatenation or
  25. Template strings (using this)*/
  26.  
  27. const johnName = "John";
  28. const johnAge = 30;
  29. console.log(`My name is ${johnName} and I am of ${johnAge} years old`);
  30. const me = `My name is ${johnName} and I am of ${johnAge} years old right now`;
  31. console.log(me);
  32.  
  33. /* String properties and methods
  34. - Properties (length) do not have parentheses ()
  35. - Methods do have parentheses ()*/
  36.  
  37. const txt = "Hello World!";
  38.  
  39. console.log(txt.length);
  40. console.log(txt.toUpperCase());
  41. console.log(txt.toLowerCase());
  42. console.log(txt.substring(0, 5));
  43. //combining strings
  44. console.log(txt.substring(0, 5).toLowerCase());
  45. //splitting strings to arrays
  46. console.log(txt.split(""));//this splits by letter
  47.  
  48. const tagMe = "item1, item2, item3";
  49. console.log(tagMe.split(", "));
  50.  
  51.  
  52.  
  53. /* Arrays: 23: 54, variables that hold multiple values*/
  54. const fruits = ["orange", "grapes", "banana"];
  55.  
  56. fruits[3] = "apple"; /*(Specifically access, rewrite and add, but not recommended)*/
  57.  
  58. fruits.push("mangoes", "peach");//add at the end (1)
  59.  
  60. fruits.unshift("strawberries");//add at the start (2)
  61.  
  62. fruits.pop(); //remove item at the end of an array (3)
  63.  
  64. fruits.shift(); //remove at the start (4)
  65.  
  66. console.log(fruits);
  67.  
  68. console.log(Array.isArray(fruits));//checks if this is an array
  69.  
  70. console.log(fruits.indexOf("orange"));
  71.  
  72.  
  73.  
  74. //========
  75.  
  76. /*Object literals (30:10) (Key value pairs)*/
  77. const person = {
  78.     firstName: "Jon",
  79.     lastName: "Doe",
  80.     age: 30,
  81.     hobbies: ["Sports", "Swimming", "Reading"],
  82.         address: {
  83.             street: "50 main city",
  84.             city: "Simei",
  85.             state: "MA",
  86.         }
  87. }
  88.  
  89. person.email = "John@gmail.com";//directly adding properties
  90.  
  91. console.log(person);
  92.  
  93. console.log(person.hobbies[2]);//access specific within array, REFER TO THIS
  94.  
  95. console.log(person.firstName, person.lastName) //access specific values
  96.  
  97. console.log(person.address.street) //access values within an object's object
  98.  
  99. //destructuting, using object literals to be actual variables
  100.  
  101. /*const {firstName, lastName} = person;
  102. console.log(firstName);
  103.  
  104. const {address: {city}} = person;
  105. console.log(city); //If within embedded object*/
  106.  
  107. const {firstName, lastName, address: {city}} = person;
  108. console.log(city);
  109.  
  110. /*array of objects (34:22)*/
  111.  
  112. const todos = [
  113.     {
  114.         id: 1,
  115.         text: "Take out trash",
  116.         isCompleted:true,
  117.     },
  118.     {
  119.         id: 2,
  120.         text: "See boss",
  121.         isCompleted:true,
  122.     },
  123.     {
  124.         id: 3,
  125.         text: "Check time",
  126.         isCompleted:false,
  127.     },
  128. ];
  129. console.log(todos[1].text);
  130.  
  131. /*JSON, a data format, in 36:10*/
  132.  
  133. const todoJSON = JSON.stringify(todos);
  134. console.log(todoJSON);
  135.  
  136. /*Loops 37:49*/
  137.  
  138.  
  139. const loopers = [
  140.     {
  141.         id: 1,
  142.         text: "FirstLoop",
  143.         isCompleted:true,
  144.     },
  145.     {
  146.         id: 2,
  147.         text: "SecondLoop",
  148.         isCompleted:true,
  149.     },
  150.     {
  151.         id: 3,
  152.         text: "Thirdloop",
  153.         isCompleted:false,
  154.     },
  155. ];
  156.  
  157. //for
  158. for(let i = 0; i < 10; i++) {//parameters: assignment of iterator/variable (i = 0); condition that needs to be met; increment
  159.     console.log(i)
  160.     console.log(`This is my For Loop Number ${i}`)
  161. }
  162.  
  163. //<= => less than/more than or equal to
  164.  
  165. //While
  166. let i = 0;
  167. while(i < 10) {
  168.     console.log(`while loop number: ${i}`);
  169.     i++;
  170. }
  171.  
  172. const delta = [
  173.     {
  174.         id: 1,
  175.         text: "FirstLoop1",
  176.         isCompleted:true,
  177.     },
  178.     {
  179.         id: 2,
  180.         text: "SecondLoop2",
  181.         isCompleted:true,
  182.     },
  183.     {
  184.         id: 3,
  185.         text: "Thirdloop3",
  186.         isCompleted:false,
  187.     },
  188. ];
  189.  
  190. //looping through arrays (40:33, 41:30)
  191. for(let del of delta) {
  192.     console.log(del);
  193.     console.log(del.text)
  194.     console.log(del.id);
  195. }
  196.  
  197. //high order array methods, suggested for any kind of array iteration (42:27)
  198. /* For Each (loops through them)*/
  199. delta.forEach(function(nameMeAnything) {
  200.     console.log(nameMeAnything.text);
  201. });
  202.  
  203. /*Map (Creates a new array from an existing array, also return the said array (43:35*/
  204. const deltaText = delta.map(function(nameMeAnything) {
  205.     return nameMeAnything.text;
  206. });
  207.  
  208. console.log(deltaText);
  209.    
  210. /*Filter (Creates an array based on a condition) 44:35*/
  211. const deltaText2 = delta.filter(function(nameMeAnything) {
  212.     return nameMeAnything.isCompleted === false;
  213. });
  214. console.log(deltaText2);
  215.  
  216. /*Chaining/stacking on other array methods 45:31*/
  217. const deltaTextStack = delta.filter(function(nameMeAnything) {
  218.     return nameMeAnything.isCompleted === true;
  219. }).map(function(todo) {
  220.     return todo.text;
  221. })
  222. console.log(deltaTextStack);
  223.  
  224.  
  225. /*Conditionals (46:33)*/
  226. const x2 = 20;
  227.  
  228. if(x2 == 4) {
  229.     console.log("X is 10");
  230. } else if(x2 > 10) {
  231.     console.log("x is greater than 10");
  232. } else {
  233.     console.log("X is NOT 10");
  234. }
  235.  
  236. /*Multiple conditions (49:20)*/
  237. const x3 = 4;
  238. const y3 = 11;
  239.  
  240. if(x > 5 || y > 10) { //or
  241.     console.log("X3 is 5 and above while Y3 is 11 and above")
  242. }
  243.  
  244. if(x > 8 && y > 15) { //and
  245.     console.log("X3 is 5 and above while Y3 is 11 and above")
  246. }
  247.  
  248. /*Ternary operator, aka shorthand if statements (51:25)*/
  249.  
  250. const tern = 11;
  251.  
  252. const trueOrFalse =  tern > 10 ? 'Then this is true' : "Else this is false";
  253.  
  254. console.log(trueOrFalse);
  255.  
  256. /*Switches, another way to evaluate conditions (52:20)*/
  257. const switcher = 10;
  258.  
  259. const colors2 = switcher > 10 ? "red":"blue"; //red is for 11+, blue is 10 and lower
  260.  
  261. switch(colors2) {
  262.     case "red":
  263.         console.log("color is red");
  264.     break;
  265.     case "blue":
  266.         console.log("color is blue");
  267.     break;
  268.     default: //doesn't match any of the above
  269.         console.log("color is NOT red or blue");
  270.     break;
  271. }
  272.  
  273. /*Functions (54:26)*/
  274. function addNumstest(num1, num2) { //inside parentheses, put parameters
  275.     console.log(num1 + num2);
  276.  
  277. }
  278.  
  279. addNumstest (5,4); //num1: 5, num2: 4, leave this blank if numbers not assigned
  280.  
  281. //setting default values for parameters
  282. function addNumstest2(num3 = 1, num4 = 4) { //inside parentheses, put parameters
  283.     return num3 + num4;
  284.  
  285. }
  286.  
  287. console.log(addNumstest2());
  288.  
  289. //turning to arrow function (56:39)
  290. const addNumsArrowtest2 = (num5 = 10, num6 = 4) => { //inside parentheses, put parameters
  291.     return num5 + num6;
  292.  
  293. }
  294.  
  295. console.log(addNumsArrowtest2());
  296.  
  297. //object-oriented programming (59:27)
  298.  
  299. //practice
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top