JavaScript Cheat Sheet - A Comprehensive List For Quick Reference - Clue Mediator
JavaScript Cheat Sheet - A Comprehensive List For Quick Reference - Clue Mediator
Basics
Variables
Data Types
Strings
Numbers and Math
Arrays
Dates
JSON
Regular Expressions
If-Else
Loops
Global Functions
Events
Promises
Errors
1. Basics
On page script:
1 <script type="text/javascript">
2 ...
3 ...
4 </script>
1 <script src="filename.js"></script>
1 setTimeout(function () {
2
3 }, 1000);
Functions:
1 function addNumbers(a, b) {
2 return a + b;
3 }
4 x = addNumbers(1, 2);
1 document.getElementById("elementID").innerHTML = "";
Output:
Comments:
1 /* Multi line
2 comment */
3
4 // One line comment
2. Variables
1 var a; // variable
2 var b = "init"; // string
3 var c = "Hi" + " " + "Joe"; // = "Hi Joe"
4 var d = 1 + 2 + "3"; // = "33"
5 var e = [2,3,5,8]; // array
6 var f = false; // boolean
7 var g = /()/; // RegEx
8 var h = function(){}; // function object
9 const PI = 3.14; // constant
10 var a = 1, b = 2, c = a + b; // one line
11 let z = 'zzz'; // block scope local variable
Strict mode:
Values:
Operators:
1 a = b + c - d; // addition, substraction
2 a = b * (c / d); // multiplication, division
3 x = 100 % 48; // modulo. 100 / 48 remainder = 4
4 a++; b--; // postfix increment and decrement
Bitwise operators:
Arithmetic:
1 a * (b + c) // grouping
2 person.age // member
3 person[age] // member
4 !(a == b) // logical not
5 a != b // not equal
6 typeof a // type (number, object, function...)
7 x << 2 x >> 3 // minary shifting
8 a = b // assignment
9 a == b // equals
10 a != b // unequal
11 a === b // strict equal
12 a !== b // strict unequal
13 a < b a > b // less and greater than
14 a <= b a >= b // less or equal, greater or eq
15 a += b // a = a + b (works with - * %...)
16 a && b // logical and
17 a || b // logical or
3. Data Types
Objects:
4. Strings
1 var pi = 3.141;
2 pi.toFixed(0); // returns 3
3 pi.toFixed(2); // returns 3.14 - for working with money
4 pi.toPrecision(2) // returns 3.1
5 pi.valueOf(); // returns number
6 Number(true); // converts to number
7 Number(new Date()) // number of milliseconds since 1970
8 parseInt("3 months"); // returns the first number: 3
9 parseFloat("3.5 days"); // returns 3.5
10 Number.MAX_VALUE // largest possible JS number
11 Number.MIN_VALUE // smallest possible JS number
12 Number.NEGATIVE_INFINITY// -Infinity
13 Number.POSITIVE_INFINITY// Infinity
Math:
6. Arrays
Methods:
concat, copyWithin, every, fill, filter, find, findIndex, forEach, indexOf, isArray, join, lastIndexOf, map,
pop, push, reduce, reduceRight, reverse, shift, slice, some, sort, splice, toString, unshift, valueOf
7. Dates
Get Times:
8. JSON
Send:
9. Regular Expressions
1 var a = str.search(/CheatSheet/i);
Modifiers:
Patterns:
1 \ Escape character
2 \d find a digit
3 \s find a whitespace character
4 \b find match at beginning or end of a word
5 n+ contains at least one n
6 n* contains zero or more occurrences of n
7 n? contains zero or one occurrences of n
8 ^ Start of string
9 $ End of string
10 \uxxxx find the Unicode character
11 . Any single character
12 (a|b) a or b
13 (...) Group section
14 [abc] In range (a, b or c)
15 [0-9] any of the digits between the brackets
16 [^abc] Not in range
17 \s White space
18 a? Zero or one of a
19 a* Zero or more of a
20 a*? Zero or more, ungreedy
21 a+ One or more of a
22 a+? One or more, ungreedy
23 a{2} Exactly 2 of a
24 a{2,} 2 or more of a
25 a{,5} Up to 5 of a
10. If-Else
11. Loops
For Loop:
While Loop:
1 var i = 1; // initialize
2 while (i < 100) { // enters the cycle if statement i
3 i *= 2; // increment to avoid infinite loo
4 document.write(i + ", "); // output
5 }
Do While Loop:
1 var i = 1; // initialize
2 do { // enters cycle at least once
3 i *= 2; // increment to avoid infinite loo
4 document.write(i + ", "); // output
5 } while (i < 100) // repeats cycle if statement is t
Break:
Continue:
1 for (var i = 0; i < 10; i++) {
2 if (i == 5) { continue; } // skips the rest of the cycle
3 document.write(i + ", "); // skips 5
4 }
13. Events
1 <button onclick="myFunction();">
2 Click here
3 </button>
Mouse
onclick, oncontextmenu, ondblclick, onmousedown, onmouseenter, onmouseleave, onmousemove,
onmouseover, onmouseout, onmouseup
Keyboard
onkeydown, onkeypress, onkeyup
Frame
onabort, onbeforeunload, onerror, onhashchange, onload, onpageshow, onpagehide, onresize,
onscroll, onunload
Form
onblur, onchange, onfocus, onfocusin, onfocusout, oninput, oninvalid, onreset, onsearch, onselect,
onsubmit
Drag
ondrag, ondragend, ondragenter, ondragleave, ondragover, ondragstart, ondrop
Clipboard
oncopy, oncut, onpaste
Media
onabort, oncanplay, oncanplaythrough, ondurationchange, onended, onerror, onloadeddata,
onloadedmetadata, onloadstart, onpause, onplay, onplaying, onprogress, onratechange, onseeked,
onseeking, onstalled, onsuspend, ontimeupdate, onvolumechange, onwaiting
Animation
animationend, animationiteration, animationstart
Miscellaneous
transitionend, onmessage, onmousewheel, ononline, onoffline, onpopstate, onshow, onstorage,
ontoggle, onwheel, ontouchcancel, ontouchend, ontouchmove, ontouchstart
14. Promises
States
pending, fulfilled, rejected
Properties
Promise.length, Promise.prototype
Methods
Promise.all(iterable), Promise.race(iterable), Promise.reject(reason), Promise.resolve(value)
15. Errors
Throw error:
Input validation: