Guest User

cheatsheet

a guest
Nov 21st, 2020
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.33 KB | None | 0 0
  1. //variable
  2.  
  3. //VALID
  4. var man;
  5. var blackDog; // This is the best way to name variables with several words
  6.  
  7. // INVALID
  8. var 1girl;
  9. var -girl;
  10.  
  11. //let shares a lot of similarities with var but unlike var has scope constraints.
  12.  
  13. let x, y, z;
  14. let x = 50, y = 20, z = 3;
  15.  
  16. //Unlike var, variables cannot be re-declared using let, trying to do that will throw a Syntax error: Identifier has already been declared.
  17.  
  18. let x = 20;
  19. let x = 50;
  20.  
  21. console.log(x); // SyntaxError: identifier "x" has already been declared.
  22.  
  23. //Use const when you are sure a variable will not be redeclared.
  24. //A variable declared with **const** MUST be initialized.
  25.  
  26. const x; // SyntaxError: missing initializer
  27.  
  28. //You are not allowed to change the value of a variable declared with const.
  29. //However, if you declare an object as const, you are able to change the properties.
  30.  
  31. // String
  32. const name = "Sarai"; // This is a string using double quotes
  33. const name2 = 'Sarai'; // This is also a string using single quotes
  34. const name3 = `Sarai`; // This is another string using computed string `` it accepts mutation using ${whatever}
  35. // Number
  36. const number = 0; // Simple
  37. // Boolean
  38. const boolean = false; // Can only be true or false
  39. // Object
  40. const object = {
  41. key: value,
  42. }; // This is an object that has a key and a value
  43. // Array or List
  44. const array = []; // This is an empty list
  45. const array2 = ['Sarai'] // This is a list with a string
  46. const array3 = [0]; // This is a list with a number
  47. const array4 = [true] // This is a list with a boolean
  48. const array5 = [{key: 'Sarai'}] // This is a list with a object with a key called key and value of a string
  49. // Null
  50. const nullVar = null;
  51. // Undefined
  52. const undef; // This is an undefined variable
  53. const undef2 = undefined; // This is an undefined variable (explicit)
  54.  
  55. //functions
  56.  
  57. function validFunctionName(parameter) {
  58. return statement;
  59. }
  60.  
  61. bark();
  62.  
  63. //A function can have multiple parameters or no parameters at all
  64.  
  65. //Function Expression defines a named or anonymous function
  66.  
  67. var fullName = function(firstName, lastName) {
  68. return `${firstName} ${lastName}`;
  69. }
  70. fullName("Jamal", "Uddin"); // Jamal Uddin
  71.  
  72. //Arrow Function: An Arrow Function Expression is a shorter syntax for writing function expressions. Arrow functions do not create their own this value.
  73.  
  74. const double = (value) => {
  75. return value * 2
  76. }
  77. double(10); // 20
  78.  
  79. const double2 = value => value * 2;
  80. double2(10); // 20
  81.  
  82. const noise = () => console.log("Pling");
  83. noise(); // Pling
  84.  
  85. const noise2 = _ => console.log("Pling");
  86. noise2(); // Pling
  87.  
  88. const multiply = (a = 2, b = 3, c = 1) => a * b * c;
  89. multiply(2, 2, 2); // 8
  90. multiply(2, 2); // 4
  91. multiply(3); // 9
  92. multiply(); // 6
  93.  
  94. // the return keyword can ONLY be used inside of a function.
  95.  
  96. function returnOnlyOnce(){
  97. return "Hello";
  98. return "Goodbye";
  99. }
  100. returnOnlyOnce(); // "Hello"
  101.  
  102. //Shorthand method definition can be used in a method declaration on object literals and ES6 classes. We can define them using a function name, followed by a list of parameters in a pair of parenthesis (para1, ..., paramN) and a pair of curly braces { ... } that delimits the body statements.
  103.  
  104. const fruits = {
  105. items: [],
  106. add(...items) {
  107. this.items.push(...items);
  108. },
  109. get(index) {
  110. return this.items[index];
  111. }
  112. };
  113. fruits.add('mango', 'banana', 'guava');
  114. fruits.get(1); // banana
  115.  
  116. //A generator is a function that can stop midway and then continue from where it stopped. In short, a generator appears to be a function but it behaves like an iterator.
  117.  
  118. function * generatorFunction() {
  119. yield 'Hello, ';
  120. console.log('I will be printed after the pause');
  121. yield 'World!';
  122. }
  123. const generatorObject = generatorFunction();
  124. console.log(generatorObject.next().value);
  125. console.log(generatorObject.next().value);
  126. console.log(generatorObject.next().value);
  127. // output should be following below.
  128. // Hello,
  129. // I will be printed after the pause
  130. // World!
  131. // undefined
  132.  
  133. //The Function constructor creates a new Function object.
  134.  
  135. var sum = new Function('a', 'b', 'return a + b');
  136. console.log(sum(2, 6)); // 8
  137.  
  138. //conditionals
  139.  
  140. if (condition a) {
  141. // code that will execute if condition a is true
  142. } else if (condition b) {
  143. // code that will execute if condition b is true
  144. } else if (condition c) {
  145. // code that will execute if condition c is true
  146. } else {
  147. // code that will execute if all above conditions are false
  148. }
  149.  
  150. //The ternary operator, also known as the conditional operator, is used as shorthand for an if...else statement.
  151. //A ternary operator is written with the syntax of a question mark (?) followed by a colon (:), as demonstrated below.
  152.  
  153. //// Set age of user
  154. let age = 20;
  155.  
  156. // Place result of ternary operation in a variable
  157. const oldEnough = (age >= 21) ? "You may enter." : "You may not enter.";
  158.  
  159. // Print output
  160. oldEnough;
  161.  
  162. //loop
  163.  
  164. while (n < 3) {
  165. n++;
  166. }
  167.  
  168.  
  169. //classes have properties and methods
  170.  
  171. class Car {
  172. constructor(brand) {
  173. this.carname = brand;
  174. }
  175. present() {
  176. return "I have a " + this.carname;
  177. }
  178. }
  179.  
  180. mycar = new Car("Ford");
  181. document.getElementById("demo").innerHTML = mycar.present();
  182.  
  183.  
  184. //The constructor method is special, it is where you initialize properties, it is called automatically when a class is initiated, and it has to have the exact name "constructor"
  185.  
  186. class Car {
  187. constructor(brand) {
  188. this.carname = brand;
  189. }
  190. present() {
  191. return "I have a " + this.carname;
  192. }
  193. }
  194.  
  195. mycar = new Car("Ford");
  196. document.getElementById("demo").innerHTML = mycar.present();
  197.  
  198.  
  199. object.name = "Blah"
  200. //^this is a setter
  201.  
  202. object.name
  203. //without the equal is a getter
  204.  
  205. const myObj = { a: 1, b: 2, c: 3};
  206. console.log(a); // undefined
  207. const { a } = myObj;
  208. const a = myObj.a;
  209. console.log(a); // 1
  210.  
  211.  
  212. //Access Object Properties in JavaScript
  213.  
  214. //Dot property accessor -> expression.identifier
  215.  
  216. const hero = {
  217. name: 'Batman'
  218. };
  219.  
  220. // Dot property accessor
  221. hero.name; // => 'Batman'
  222.  
  223.  
  224. //Square brackets property accessor
  225.  
  226. expression[expression]
  227.  
  228. const property = 'name';
  229. const hero = {
  230. name: 'Batman'
  231. };
  232.  
  233. // Square brackets property accessor:
  234.  
  235. hero['name']; // => 'Batman'
  236. hero[property]; // => 'Batman'
  237.  
  238.  
  239. //Object destructuring
  240.  
  241. const { identifier } = expression;
  242.  
  243. const hero = {
  244. name: 'Batman'
  245. };
  246.  
  247. // Object destructuring:
  248. const { name } = hero;
  249. name; // => 'Batman'
  250.  
  251. //The AND (&&) and OR (||) logical operators both require two operands, and are used to perform Boolean operations on their operands.
  252. //&& operation returns true only when both operands are true, otherwise it returns false.
  253. //|| operation returns false only when both operands are false, otherwise it returns true.
  254.  
  255. false && true || true; // true
  256. false && (true || true); // false
  257.  
  258.  
  259. // number literal
  260. 0xFF
  261.  
  262. // array literal
  263. []
  264.  
  265. // object literal
  266. {}
  267.  
  268. // regexp literal
  269. /^\d+$/
  270.  
  271. // logical AND operation
  272. (x && y)
  273.  
  274. // bitwise XOR operation
  275. (x ^ y)
  276.  
  277. // ternary operation
  278. (x ? y : z)
  279.  
  280. // arithmetic operation
  281. (x + y) / z
  282.  
  283. // assignment
  284. x = 'string'
  285.  
  286. // function expression
  287. (function x(y) {})
  288.  
  289. // function invocation
  290. x(100)
  291.  
  292. // object property access
  293. obj.students[0].name
  294.  
  295. //If you want to access any element in an HTML page, you always start with accessing the document object.
  296.  
  297. document.getElementById(id) //Find an element by element id
  298. document.getElementsByTagName(name) //Find elements by tag name
  299. document.getElementsByClassName(name) //Find elements by class name
  300.  
  301. //Changing HTML Elements
  302.  
  303. element.innerHTML = new html content //Change the inner HTML of an element
  304. element.attribute = new value //Change the attribute value of an HTML element
  305. element.style.property = new style //Change the style of an HTML element
  306.  
  307. element.setAttribute(attribute, value) //Change the attribute value of an HTML element
  308.  
  309. //Adding and Deleting Elements
  310.  
  311. document.createElement(element) //Create an HTML element
  312. document.removeChild(element) //Remove an HTML element
  313. document.appendChild(element) //Add an HTML element
  314. document.replaceChild(new, old) //Replace an HTML element
  315. document.write(text) //Write into the HTML output stream
  316.  
  317. //The getElementsByClassName method of Document interface returns an array-like object of all child elements which have all of the given class nam
  318.  
  319. var elements = document.getElementsByClassName(names); // or:
  320. var elements = rootElement.getElementsByClassName(names);
  321.  
  322. //HTML EVENT HANDLERS
  323.  
  324. onblur onclick onerror onfocus
  325. onkeydown onkeypress onkeyup onmouseover
  326. onload onmouseup onmousedown onsubmit
  327.  
  328. //Add an event listener that fires when a user clicks a button:
  329.  
  330. document.getElementById("myBtn").addEventListener("click", displayDate);
  331.  
  332. element.addEventListener(event, function, useCapture);
  333.  
  334. //callback promises and async
  335.  
  336. // v set timeout takes in a call back function adn it takes in a certain amount of time that you want to delay whatever you put in there, in this case 1second
  337.  
  338. function getPosts () {
  339. setTimeout(function(); {
  340.  
  341. }, 1000)
  342. }
  343.  
  344. //you can also do it as an arrow function
  345.  
  346. function getPosts () {
  347. setTimeout (() => {
  348.  
  349. }, 1000)
  350. }
  351.  
  352.  
  353.  
  354. git init
  355.  
  356. - initialises folder as git repository
  357.  
  358. git log -oneline
  359.  
  360. - see a brief log of commits
  361.  
  362. git add .
  363.  
  364. - adds files to the staging area
  365.  
  366. git checkout [comit log number] [file name]
  367.  
  368. e.g. git checkout 900cfcf index.html
  369.  
  370. - will checkout that file into my current working directory
  371.  
  372. - this has already been staged (need_help - understand staged?) , so this git checkout will pull out an older version of the file and then will replace what is on the current directory and also check it into the staging area
  373.  
  374. - if I do that and realise that this is what I want then I can just do annother commit
  375.  
  376. git reset HEAD index.html
  377.  
  378. - but if I dont like the above and want to revert I do ^
  379.  
  380. - the modified version I had checkedout is still there but has been unstaged
  381.  
  382. git checkout -- index.html
  383.  
  384. - here my file has been restored to what it was before I reverted stuff (need_help - why double minus sometimes?)
  385.  
  386. npm init
  387.  
  388. - initialises a package.json file for your project
  389.  
  390. npm install lite-server --save-dev (need_help)
  391.  
  392. - "--save-dev" specifies that this lite-server is used for development dependency for our project
  393. - in installing a module on which project is directly dependent on then I would use "--save"-option"
  394.  
  395. (need_help) I had to add lite-servermanually in lines 7 and 9 of my json package. I have no idea why I did dis, I understand that dis means going forward dis will start the lite server but I don't understand why? npm start would start a server already before
  396.  
  397. theory - a live-server equivalent already installed as a global dependancy? that is why "npm start" already does something but because in dis case it is just a git repository that I made I had to do it manually. when I create a react project this package? node module? is already installed
  398.  
  399. .gitignore
  400.  
  401. -You normally dont want to upload the node modules folder as dis can be recreated by typing "npm install", to avoid certain folders getting commited you need to create a gitignore file
  402.  
  403. create-react-app --help
  404.  
  405. -Brings out a set of instructions on how to use the create react appears
  406.  
  407.  
  408.  
  409.  
Add Comment
Please, Sign In to add comment