Guest User

jscheatsheet 1-11

a guest
Aug 21st, 2018
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. www.plusfeeds.com
  2. https://www.plusfeeds.com/javascript-for-beginners-cheat-sheet/
  3.  
  4. Javascript Cheat Sheet
  5.  
  6. Basics
  7.  
  8. -On page script
  9.  
  10.  <script type="text/javascript">
  11.  </script>
  12.  
  13.    Include external JS file
  14.  
  15.  <script src="filename.js"></script>
  16.  
  17. -Functions
  18.  
  19.  function addNumbers(a, b) {
  20.    return a + b; ;
  21.  }
  22.  
  23.  x = addNumbers(1, 2);
  24.  
  25. -Edit DOM element
  26.  
  27.  document.getElementByld("elementID").innerHTML = "Hello World!";
  28.  
  29. -Output
  30.  
  31.  console.log(a);              // write to the browser console
  32.  document.write(a);           // write to the HTML
  33.  alert(a);                    // output in an alert box
  34.  confirm("Really?");          // yes/no dialog, returns true/false depending on user click
  35.  prompt("Your age?","0");     // input dialog. Second argument is the initial value
  36.  
  37. -Comments
  38.  
  39.  /* Multi line
  40.   comment */
  41.  // One line
  42.  
  43.  
  44. -If - Else                                    www.plusfeeds.com
  45.  
  46.   if ((age >= 14) && (age < 19)) {  // logical condition
  47.     status = "Eligible.";           // executed if condition is true
  48.   } else {                          // else block is optional
  49.     status = "Not eligible.";       // executed if condition is false
  50.   }
  51.  
  52. -Switch Statement
  53.  
  54.   switch (new Date().getDay()) {    // input is current day
  55.     case 6:                         // if (day == 6)
  56.      text = "Saturday";
  57.      break;
  58.     case 0:                         // if (day == 0)
  59.      text = "Sunday";
  60.      break;
  61.     default:                        // else...
  62.      text = "Whatever";
  63.   }
  64.  
  65.  
  66. -Data Types
  67.  
  68. var age = 18;                             // number
  69. var name = "Jane";                        // string
  70. var name = {first:"Jane", last:"Doe"};    // object
  71. var truth = false;                        // boolean
  72. var sheets = ("HTML","CSS","JS");         // array
  73. var a; typeof a;                          // undefined
  74. var a = null;                             // value null
  75.  
  76. -Objects
  77.  
  78. var student =                           // object name
  79.  firstName:"Jane",                      // list of properties and values
  80.  lastName:"Doe",
  81.  age:18,
  82.  height:170,
  83.  fullName : function() {                // object function
  84.   return this.firstName + " " + this.lastName;
  85.  }
  86.  
  87.  
  88. student age = 19;                       // setting value
  89. student[age]++;                         // incrementing
  90. name = student.fullName();              // call object function
  91.  
  92. -Loops
  93.  
  94. -For Loop
  95. for (var i = 0; i < 10; i++){
  96.   document.write(i + ": " + i*3 + "<br />");
  97. }
  98. var sum = 0;
  99. for (var i = 0; i < a.length; i++) {
  100.   sum + = a[i];
  101. }                 // parsing an array
  102. html = "";
  103. for (var i of custOrder) {
  104.   html += "<li>" + i + "</li>";
  105. }
  106.  
  107. -While Loop
  108.  
  109. var i = 1;                 // initialize
  110. while (i < 100) {          // enters the cycle if statement is
  111. true
  112.  i *= 2;                   // increment to avoid infinite loop
  113.  document.write(i + ",");  // output
  114. }
  115.  
  116. -Loops 2
  117.  
  118. -Do While Loop
  119.  
  120. var i = 1;                    // initialize
  121. do {                          // enters cycle at least once
  122.  i *= 2;                      // increment to avoid infinite loop
  123.  document.write(i + ",");     // output
  124. } while (i < 100)             // repeats cycle if statement is true at the end
  125.  
  126. -Break
  127.  
  128. for (var i = 0; i < 10; i++) {
  129.   if (i == 5) { break; }     // stops and exits the cycle
  130.   document.write(i + ", ");  // last output number is 4
  131.  
  132. -Continue
  133.  
  134. for (var i = 0; i < 10; i++) {
  135.  if (i == 5) { continue; }   // skips the rest of the cycle
  136.  document.write(i + ", ");   // skips 5
  137. }
  138.  
  139. Variables
  140.  
  141. var a;                      // variable
  142. var b = "init" ;            // string
  143. var c = "Hi" + " " + "Joe"; // = "Hi Joe"
  144. yard = 1 + 2 + "3";           = n33..
  145. var e = [2,3,5,8];          // array
  146. var f = false;              // boolean            Strict mode
  147. var g = /()/;                // RegEx
  148. var h = function(){};       // function object
  149. const PI = 3.14;            // constant
  150. var a = 1, b = 2, c = a + b;// one line
  151. let z = 'zzz';              1/ block scope local vari
  152.  
  153. -Strict mode
  154.  "use strict";    // Use strict mode to write secure code
  155. x = 1;            // Throws an error because variable is no
  156.  
  157. -Variables 2
  158.  
  159. -Values
  160.  
  161. false, true                  // boolean
  162. 18, 3.14, Ob10011, OxF6, NaN // number
  163. "flower", 'John'             // string
  164. undefined, null , Infinity   // special
  165.  
  166. -Operators
  167.  
  168. a = b + c - d;               // addition, substraction
  169. a = b * (c / d) ;             // multiplication, division
  170. x = 100 % 48;          // modulo. 100 / 48 remainder = 4
  171. a++; b--;              // postfix increment and decrement
  172.  
  173. Variables 3
  174.  
  175. -Bitwise operators
  176.  
  177. &     AND                             5 & 1 (0101 & 0001)    1 (1)
  178. |     OR                              5 | 1 (0101 | 0001)    5 (101)
  179. ~     NOT                             ~5 (~0101)             10 (1010)
  180. ^     XOR                             5 ^ 1 (0101 ^ 0001)    4 (100)
  181. <<    left shift                      5 << 1 (0101 << 1)     10 (1010)
  182. >>    right shift                     5 >> 1 (0101 >> 1)     2 (10)
  183. >>>   zero fill right shift           5 >>> 1 (0101 >>> 1)   2 (10)
  184.  
  185. Variables
  186.  
  187. -Arithmetic
  188.  
  189.  a * (b + c)   // grouping
  190.  person.age    // member
  191.  person[age]   // member
  192.  1(a == b)     // logical not
  193.  a != b        // not equal
  194.  typeof a      // type (number, object, function...)
  195.  x << 2 x >> 3 // minary shifting
  196.  a = b         // assignment
  197.  a == b        // equals
  198.  a != b        // unequal
  199.  a === b       // strict equal
  200.  a !== b       // strict unequal
  201.  a < b a > b   // less and greater than
  202.  a <= b a >= b // less or equal, greater or eq
  203.  a += b        // a = a + b (works with - * %...)
  204.  a && b        // logical and
  205.  a || b        // logical or
  206.  
  207. Strings
  208.  
  209. var abc = "abcdefghijklmnopqrstuvwxyz";
  210. var esc = 'I don\'t \n know'; // \n new line
  211. var len = abc.length;         // string length
  212. abc.index0f("Imno");         // find substring, -1 if doesn't contain
  213. abc.lastIndex0f("Imno");     // last occurance
  214. abc.slice(3, 6);             // cuts out "def", negative values count from behind
  215. abc.replace("abc","123");    // find and replace, takes regular expressions
  216. abc.toUpperCase();          // convert to upper case
  217. abc.toLowerCase();          // convert to lower case
  218. abc.concat(" ", str2);      // abc + " " + str2
  219. abc.charAt(2);              // character at index: "c"
  220. abc[2];                    // unsafe, abc[2] = "C" doesn't work
  221. abc.charCodeAt(2);         // character code at index: "c" -> 99
  222. abc.split(",");            // splitting a string on commas gives an array
  223. abc.split(""),             // splitting on characters
  224. 128.toString(16);          // number to hex(16), octal (8) or binary (2)
  225.  
  226.  
  227. -Dates
  228.  
  229. Fri Jul 06 2018 04:48:33 GMT-0700 (Pacific Daylight Time)
  230. var d = new Date();
  231. 1530877713578 miliseconds passed since 1970
  232. Number(d)
  233. Date("2017-06-23");                                 // date declaration
  234. Date("2017");                                       // is set to Jan 01
  235. Date("2017-06-23T12:00:00-09:45");                  // date - time YYYY-MM-
  236. DDTHH:MM:SSZ
  237. Date("June 23 2017");                               // long date format
  238. Date("Jun 23 2017 07:45:00 GMT+0100 (Tokyo Time)"); // time zone
  239.  
  240. -Get Times
  241.  
  242. var d = new Date();
  243. a = d.getDay();                 // getting the weekday
  244. getDate();                      // day as a number (1-31)
  245. getDay();                       // weekday as a number (0-6)
  246. getFullYear();                  // four digit year (yyyy)
  247. getHours();                     // hour (0-23)
  248. getMilliseconds();              // milliseconds (0-999)
  249. getMinutes();                   // minutes (0-59)
  250. getMonth();                     // month (0-11)
  251. getSeconds();                   // seconds (0-59)
  252. getTime();                      // milliseconds since 1970
Advertisement
Add Comment
Please, Sign In to add comment