JS CheatSheet
JS CheatSheet
Continue If - Else ⇵
for (var i = 0; i < 10; i++) {
if ((age >= 14) && (age < 19)) { // logical
if (i == 5) { continue; } // skips the re
status = "Eligible."; // execute
document.write(i + ", "); // skips 5
} else { // else bl
}
status = "Not eligible."; // execute
}
Switch Statement
Variables x switch (new Date().getDay()) { // input is cu
case 6: // if (day ==
var a; // variable
text = "Saturday";
var b = "init"; // string
break;
var c = "Hi" + " " + "Joe"; // = "Hi Joe"
case 0: // if (day ==
var d = 1 + 2 + "3"; // = "33"
text = "Sunday";
var e = [2,3,5,8]; // array
break;
var f = false; // boolean
default: // else...
var g = /()/; // RegEx
text = "Whatever";
var h = function(){}; // function object
}
const PI = 3.14; // constant
var a = 1, b = 2, c = a + b; // one line
let z = 'zzz'; // block scope loca
Operators Objects
Events 🕖
a && b // logical and
Numbers and Math ∑
a || b // logical or
<button onclick="myFunction();">
var pi = 3.141;
Click here
pi.toFixed(0); // returns 3
</button>
pi.toFixed(2); // returns 3.14 - for worki
pi.toPrecision(2) // returns 3.1 Mouse
pi.valueOf(); // returns number
onclick, oncontextmenu, ondblclick, onmousedown,
Number(true); // converts to number
onmouseenter, onmouseleave, onmousemove,
Number(new Date()) // number of milliseconds s
onmouseover, onmouseout, onmouseup
parseInt("3 months"); // returns the first number
parseFloat("3.5 days"); // returns 3.5 Keyboard
Number.MAX_VALUE // largest possible JS numb onkeydown, onkeypress, onkeyup
Number.MIN_VALUE // smallest possible JS num
Number.NEGATIVE_INFINITY// -Infinity Frame
Number.POSITIVE_INFINITY// Infinity onabort, onbeforeunload, onerror, onhashchange, onload
onpageshow, onpagehide, onresize, onscroll, onunload
Math.
var pi = Math.PI; // 3.141592653589793 Form
Math.round(4.4); // = 4 - rounded onblur, onchange, onfocus, onfocusin, onfocusout,
Math.round(4.5); // = 5 oninput, oninvalid, onreset, onsearch, onselect, onsubmit
Math.pow(2,8); // = 256 - 2 to the power o
Math.sqrt(49); // = 7 - square root Drag
Math.abs(-3.14); // = 3.14 - absolute, posit ondrag, ondragend, ondragenter, ondragleave,
Math.ceil(3.14); // = 4 - rounded up ondragover, ondragstart, ondrop
Math.floor(3.99); // = 3 - rounded down
Math.sin(0); // = 0 - sine Clipboard
oncopy, oncut, onpaste
Math.cos(Math.PI); // OTHERS: tan,atan,asin,ac Media
Math.min(0, 3, -2, 2); // = -2 - the lowest value
onabort, oncanplay, oncanplaythrough, ondurationchange
Math.max(0, 3, -2, 2); // = 3 - the highest value onended, onerror, onloadeddata, onloadedmetadata,
Math.log(1); // = 0 natural logarithm onloadstart, onpause, onplay, onplaying, onprogress,
Math.exp(1); // = 2.7182pow(E,x) onratechange, onseeked, onseeking, onstalled,
Math.random(); // random number between 0 onsuspend, ontimeupdate, onvolumechange, onwaiting
Math.floor(Math.random() * 5) + 1; // random integ
Animation
Constants like Math.PI: animationend, animationiteration, animationstart
E, PI, SQRT2, SQRT1_2, LN2, LN10, LOG2E, Log10E
Miscellaneous
transitionend, onmessage, onmousewheel, ononline,
Dates 📆 onoffline, onpopstate, onshow, onstorage, ontoggle,
onwheel, ontouchcancel, ontouchend, ontouchmove,
Mon Feb 17 2020 13:42:03 GMT+0200 (Eastern European ontouchstart
Standard Time)
var d = new Date();
1581939723047 miliseconds passed since 1970
Number(d)
Arrays ≡
Date("2017-06-23"); // date declara var dogs = ["Bulldog", "Beagle", "Labrador"];
Date("2017"); // is set to Ja var dogs = new Array("Bulldog", "Beagle", "Labrado
Date("2017-06-23T12:00:00-09:45"); // date - time
Date("June 23 2017"); // long date fo alert(dogs[1]); // access value at ind
Date("Jun 23 2017 07:45:00 GMT+0100 (Tokyo Time)"); dogs[0] = "Bull Terier"; // change the first it
Methods
getDate(); // day as a number (1-31)
getDay(); // weekday as a number (0-6) dogs.toString(); // convert
getFullYear(); // four digit year (yyyy) dogs.join(" * "); // join: "
getHours(); // hour (0-23) dogs.pop(); // remove
getMilliseconds(); // milliseconds (0-999) dogs.push("Chihuahua"); // add new
getMinutes(); // minutes (0-59) dogs[dogs.length] = "Chihuahua"; // the sam
getMonth(); // month (0-11) dogs.shift(); // remove
getSeconds(); // seconds (0-59) dogs.unshift("Chihuahua"); // add new
getTime(); // milliseconds since 1970 delete dogs[0]; // change
dogs.splice(2, 0, "Pug", "Boxer"); // add ele
Setting part of a date var animals = dogs.concat(cats,birds); // join tw
var d = new Date(); dogs.slice(1,4); // element
d.setDate(d.getDate() + 7); // adds a week to a dat dogs.sort(); // sort st
dogs.reverse(); // sort st
setDate(); // day as a number (1-31) x.sort(function(a, b){return a - b}); // numeric
setFullYear(); // year (optionally month and d x.sort(function(a, b){return b - a}); // numeric
setHours(); // hour (0-23) highest = x[0]; // first i
setMilliseconds(); // milliseconds (0-999) x.sort(function(a, b){return 0.5 - Math.random()})
setMinutes(); // minutes (0-59)
setMonth(); // month (0-11) concat, copyWithin, every, fill, filter, find, findIndex,
setSeconds(); // seconds (0-59) forEach, indexOf, isArray, join, lastIndexOf, map, pop,
setTime(); // milliseconds since 1970) push, reduce, reduceRight, reverse, shift, slice, some,
sort, splice, toString, unshift, valueOf
Useful Links ↵ }
return reject(new TypeError("Inputs must
resolve(a + b);
JS cleaner Obfuscator }, 1000);
Can I use? Node.js });
}
jQuery RegEx tester var myPromise = sum(10, 5);
myPromsise.then(function (result) {
document.write(" 10 + 5: ", result);
return sum(null, "foo"); // Invalid
}).then(function () { // Won't b
}).catch(function (err) { // The cat
console.error(err); // => Plea
});
States
pending, fulfilled, rejected
Properties
Promise.length, Promise.prototype
Methods
Promise.all(iterable), Promise.race(iterable),
Promise.reject(reason), Promise.resolve(value)
HTML Cheat Sheet is using cookies. | Terms and Conditions, Privacy Policy
ninja_webtech ©2020 HTMLCheatSheet.com