This document was uploaded by user and they confirmed that they have the permission to share
it. If you are author or own the copyright of this book, please report to us by using this DMCA
report form. Report DMCA
Overview
Download & View Javascript-js-cheat-sheet as PDF for free.
For Loop for (var i = 0; i < 10; i++) { document.write(i + ": " + i*3 + " "); } var sum = 0; for (var i = 0; i < a.length; i++) { sum + = a[i]; } // parsing an array html = ""; for (var i of custOrder) { html += "
" + i + "
"; } While Loop var i = 1; while (i < 100) { i *= 2; document.write(i + ", "); }
// // // //
initialize enters the cycle increment to avo output
// // // // //
initialize enters cycle at increment to avo output repeats cycle if
Break for (var i = 0; i < 10; i++) { if (i == 5) { break; } document.write(i + ", "); }
// stops and ex // last output
Delay - 1 second timeout setTimeout(function () { }, 1000); Functions function addNumbers(a, b) { return a + b; ; } x = addNumbers(1, 2); Edit DOM element document.getElementById("elementID").innerHTML = "
write to the browse write to the HTML output in an alert yes/no dialog, retu input dialog. Secon
Comments /* Multi line comment */ // One line
If - Else ⇵
Continue for (var i = 0; i < 10; i++) { if (i == 5) { continue; } document.write(i + ", "); }
// skips the re // skips 5
var a; var b = "init"; var c = "Hi" + " " + "Joe"; var d = 1 + 2 + "3"; var e = [2,3,5,8]; var f = false; var g = /()/; var h = function(){}; const PI = 3.14; var a = 1, b = 2, c = a + b; let z = 'zzz';
if ((age >= 14) && (age < 19)) { status = "Eligible."; } else { status = "Not eligible."; }
// // // //
logical execute else bl execute
Switch Statement
Variables x // // // // // // // // // // //
variable string = "Hi Joe" = "33" array boolean RegEx function object constant one line block scope loca
switch (new Date().getDay()) { case 6: text = "Saturday"; break; case 0: text = "Sunday"; break; default: text = "Whatever"; }
// input is cu // if (day ==
// if (day ==
// else...
Data Types ℜ
Strict mode "use strict"; x = 1;
<script src="filename.js">
Output
Do While Loop var i = 1; do { i *= 2; document.write(i + ", "); } while (i < 100)
Include external JS file
// Use strict mode to write secure // Throws an error because variable
Arithmetic a * (b + c) person.age person[age] !(a == b) a != b typeof a x << 2 x >> 3 a = b a == b a != b a === b a !== b a < b a > b a <= b a >= b a += b a && b a || b
grouping member member logical not not equal type (number, object, functi minary shifting assignment equals unequal strict equal strict unequal less and greater than less or equal, greater or eq a = a + b (works with - * %. logical and logical or
returns 3 returns 3.14 - for worki returns 3.1 returns number converts to number number of milliseconds s returns the first number returns 3.5 largest possible JS numb smallest possible JS num -Infinity Infinity
Math. var pi = Math.PI; Math.round(4.4); Math.round(4.5); Math.pow(2,8); Math.sqrt(49); Math.abs(-3.14); Math.ceil(3.14); Math.floor(3.99); Math.sin(0);
name = {first:"Jane", last:"Doe"}; truth = false; sheets = ["HTML","CSS","JS"]; a; typeof a; a = null;
// // // // //
object boolean array undefin value n
Objects
Operators a = b + c - d; a = b * (c / d); x = 100 % 48; a++; b--;
var var var var var
// // // // // // // // //
3.141592653589793 = 4 - rounded = 5 = 256 - 2 to the power o = 7 - square root = 3.14 - absolute, posit = 4 - rounded up = 3 - rounded down = 0 - sine
Strings ⊗ var abc = "abcdefghijklmnopqrstuvwxyz"; var esc = 'I don\'t \n know'; // \n new line var len = abc.length; // string length abc.indexOf("lmno"); // find substring, abc.lastIndexOf("lmno"); // last occurance abc.slice(3, 6); // cuts out "def", abc.replace("abc","123"); // find and replac abc.toUpperCase(); // convert to uppe abc.toLowerCase(); // convert to lowe abc.concat(" ", str2); // abc + " " + str abc.charAt(2); // character at in abc[2]; // unsafe, abc[2] abc.charCodeAt(2); // character code abc.split(","); // splitting a str abc.split(""); // splitting on ch 128.toString(16); // number to hex(1
OTHERS: tan,atan,asin,ac = -2 - the lowest value = 3 - the highest value = 0 natural logarithm = 2.7182pow(E,x) random number between 0 5) + 1; // random integ
// date declara // is set to Ja // date - time // long date fo (Tokyo Time)");
Get Times var d = new Date(); a = d.getDay(); // getting the weekday getDate(); getDay(); getFullYear(); getHours(); getMilliseconds(); getMinutes(); getMonth(); getSeconds(); getTime();
// // // // // // // // //
day as a number (1-31) weekday as a number (0-6) four digit year (yyyy) hour (0-23) milliseconds (0-999) minutes (0-59) month (0-11) seconds (0-59) milliseconds since 1970
Setting part of a date var d = new Date(); d.setDate(d.getDate() + 7); // adds a week to a dat setDate(); setFullYear(); setHours(); setMilliseconds(); setMinutes(); setMonth(); setSeconds(); setTime();
// // // // // // // //
day as a number (1-31) year (optionally month and d hour (0-23) milliseconds (0-999) minutes (0-59) month (0-11) seconds (0-59) milliseconds since 1970)
var dogs = ["Bulldog", "Beagle", "Labrador"]; var dogs = new Array("Bulldog", "Beagle", "Labrado alert(dogs[1]); dogs[0] = "Bull Terier";
for (var i = 0; i < dogs.length; i++) { console.log(dogs[i]); }
// // // // // // // // // // // //
executes a string as return string from n return string from n return number from s decode URI. Result: encode URI. Result: decode a URI compone encode a URI compone is variable a finite is variable an illeg returns floating poi parses a string and
// par
Methods dogs.toString(); // convert dogs.join(" * "); // join: " dogs.pop(); // remove dogs.push("Chihuahua"); // add new dogs[dogs.length] = "Chihuahua"; // the sam dogs.shift(); // remove dogs.unshift("Chihuahua"); // add new delete dogs[0]; // change dogs.splice(2, 0, "Pug", "Boxer"); // add ele var animals = dogs.concat(cats,birds); // join tw dogs.slice(1,4); // element dogs.sort(); // sort st dogs.reverse(); // sort st x.sort(function(a, b){return a - b}); // numeric x.sort(function(a, b){return b - a}); // numeric highest = x[0]; // first i x.sort(function(a, b){return 0.5 - Math.random()})