Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- www.plusfeeds.com
- https://www.plusfeeds.com/javascript-for-beginners-cheat-sheet/
- Javascript Cheat Sheet
- Basics
- -On page script
- <script type="text/javascript">
- </script>
- Include external JS file
- <script src="filename.js"></script>
- -Functions
- function addNumbers(a, b) {
- return a + b; ;
- }
- x = addNumbers(1, 2);
- -Edit DOM element
- document.getElementByld("elementID").innerHTML = "Hello World!";
- -Output
- console.log(a); // write to the browser console
- document.write(a); // write to the HTML
- alert(a); // output in an alert box
- confirm("Really?"); // yes/no dialog, returns true/false depending on user click
- prompt("Your age?","0"); // input dialog. Second argument is the initial value
- -Comments
- /* Multi line
- comment */
- // One line
- -If - Else www.plusfeeds.com
- if ((age >= 14) && (age < 19)) { // logical condition
- status = "Eligible."; // executed if condition is true
- } else { // else block is optional
- status = "Not eligible."; // executed if condition is false
- }
- -Switch Statement
- switch (new Date().getDay()) { // input is current day
- case 6: // if (day == 6)
- text = "Saturday";
- break;
- case 0: // if (day == 0)
- text = "Sunday";
- break;
- default: // else...
- text = "Whatever";
- }
- -Data Types
- var age = 18; // number
- var name = "Jane"; // string
- var name = {first:"Jane", last:"Doe"}; // object
- var truth = false; // boolean
- var sheets = ("HTML","CSS","JS"); // array
- var a; typeof a; // undefined
- var a = null; // value null
- -Objects
- var student = // object name
- firstName:"Jane", // list of properties and values
- lastName:"Doe",
- age:18,
- height:170,
- fullName : function() { // object function
- return this.firstName + " " + this.lastName;
- }
- student age = 19; // setting value
- student[age]++; // incrementing
- name = student.fullName(); // call object function
- -Loops
- -For Loop
- for (var i = 0; i < 10; i++){
- document.write(i + ": " + i*3 + "<br />");
- }
- var sum = 0;
- for (var i = 0; i < a.length; i++) {
- sum + = a[i];
- } // parsing an array
- html = "";
- for (var i of custOrder) {
- html += "<li>" + i + "</li>";
- }
- -While Loop
- var i = 1; // initialize
- while (i < 100) { // enters the cycle if statement is
- true
- i *= 2; // increment to avoid infinite loop
- document.write(i + ","); // output
- }
- -Loops 2
- -Do While Loop
- var i = 1; // initialize
- do { // enters cycle at least once
- i *= 2; // increment to avoid infinite loop
- document.write(i + ","); // output
- } while (i < 100) // repeats cycle if statement is true at the end
- -Break
- for (var i = 0; i < 10; i++) {
- if (i == 5) { break; } // stops and exits the cycle
- document.write(i + ", "); // last output number is 4
- -Continue
- for (var i = 0; i < 10; i++) {
- if (i == 5) { continue; } // skips the rest of the cycle
- document.write(i + ", "); // skips 5
- }
- Variables
- var a; // variable
- var b = "init" ; // string
- var c = "Hi" + " " + "Joe"; // = "Hi Joe"
- yard = 1 + 2 + "3"; = n33..
- var e = [2,3,5,8]; // array
- var f = false; // boolean Strict mode
- var g = /()/; // RegEx
- var h = function(){}; // function object
- const PI = 3.14; // constant
- var a = 1, b = 2, c = a + b;// one line
- let z = 'zzz'; 1/ block scope local vari
- -Strict mode
- "use strict"; // Use strict mode to write secure code
- x = 1; // Throws an error because variable is no
- -Variables 2
- -Values
- false, true // boolean
- 18, 3.14, Ob10011, OxF6, NaN // number
- "flower", 'John' // string
- undefined, null , Infinity // special
- -Operators
- a = b + c - d; // addition, substraction
- a = b * (c / d) ; // multiplication, division
- x = 100 % 48; // modulo. 100 / 48 remainder = 4
- a++; b--; // postfix increment and decrement
- Variables 3
- -Bitwise operators
- & AND 5 & 1 (0101 & 0001) 1 (1)
- | OR 5 | 1 (0101 | 0001) 5 (101)
- ~ NOT ~5 (~0101) 10 (1010)
- ^ XOR 5 ^ 1 (0101 ^ 0001) 4 (100)
- << left shift 5 << 1 (0101 << 1) 10 (1010)
- >> right shift 5 >> 1 (0101 >> 1) 2 (10)
- >>> zero fill right shift 5 >>> 1 (0101 >>> 1) 2 (10)
- Variables
- -Arithmetic
- a * (b + c) // grouping
- person.age // member
- person[age] // member
- 1(a == b) // logical not
- a != b // not equal
- typeof a // type (number, object, function...)
- x << 2 x >> 3 // minary shifting
- a = b // assignment
- a == b // equals
- a != b // unequal
- a === b // strict equal
- a !== b // strict unequal
- a < b a > b // less and greater than
- a <= b a >= b // less or equal, greater or eq
- a += b // a = a + b (works with - * %...)
- a && b // logical and
- a || b // logical or
- Strings
- var abc = "abcdefghijklmnopqrstuvwxyz";
- var esc = 'I don\'t \n know'; // \n new line
- var len = abc.length; // string length
- abc.index0f("Imno"); // find substring, -1 if doesn't contain
- abc.lastIndex0f("Imno"); // last occurance
- abc.slice(3, 6); // cuts out "def", negative values count from behind
- abc.replace("abc","123"); // find and replace, takes regular expressions
- abc.toUpperCase(); // convert to upper case
- abc.toLowerCase(); // convert to lower case
- abc.concat(" ", str2); // abc + " " + str2
- abc.charAt(2); // character at index: "c"
- abc[2]; // unsafe, abc[2] = "C" doesn't work
- abc.charCodeAt(2); // character code at index: "c" -> 99
- abc.split(","); // splitting a string on commas gives an array
- abc.split(""), // splitting on characters
- 128.toString(16); // number to hex(16), octal (8) or binary (2)
- -Dates
- Fri Jul 06 2018 04:48:33 GMT-0700 (Pacific Daylight Time)
- var d = new Date();
- 1530877713578 miliseconds passed since 1970
- Number(d)
- Date("2017-06-23"); // date declaration
- Date("2017"); // is set to Jan 01
- Date("2017-06-23T12:00:00-09:45"); // date - time YYYY-MM-
- DDTHH:MM:SSZ
- Date("June 23 2017"); // long date format
- Date("Jun 23 2017 07:45:00 GMT+0100 (Tokyo Time)"); // time zone
- -Get Times
- var d = new Date();
- a = d.getDay(); // getting the weekday
- getDate(); // day as a number (1-31)
- getDay(); // weekday as a number (0-6)
- getFullYear(); // four digit year (yyyy)
- getHours(); // hour (0-23)
- getMilliseconds(); // milliseconds (0-999)
- getMinutes(); // minutes (0-59)
- getMonth(); // month (0-11)
- getSeconds(); // seconds (0-59)
- getTime(); // milliseconds since 1970
Advertisement
Add Comment
Please, Sign In to add comment