Javascript Quick Reference - www.corewebtraining.
com
Syntax / Layout Popular Methods
Variables If / Else Math methods String methods
var a = 10; if (a === b) { Return absolute value Math.abs(x) Join strings string.concat()
// code;
var b = 20; } Return arccosine Math.acos(x) Checks if a string ends with x string.endsWith()
var c = a + b; if (a === b) { Return arcsine Math.asin(x) Check if string contains x string.includes()
// code;
Numbers } else { Round up Math.ceil(x) Rtn first found position of x string.indexOf()
// code;
var x = 10; Return cosine Math.cos(x) Rtn last found position of x string.lastIndexOf()
}
var x = 10.5; Switch statements Round down Math.floor(x) String to hyperlink string.link()
Strings var month = 'January'; Return lowest value Math.min(x,y,z) Rtn matches against a RegEx string.match()
switch(month) {
var a = “string”; case 'January': Return highest value Math.max(x,y,z) Replace string at position x string.replace()
Arrays // code x to power of y Math.pow(x,y) Search for position of value x string.search()
break;
var users = new Array(); Return random number Math.random() Extract section of string string.slice()
case 'December':
var users = [“Chris”, “Mike”]; // code Round to nearest number Math.round(x) Split string into new strings string.split()
break;
Functions Return sine of number Math.sin(x) Checks if a string begins with x string.starttWith
function myFunction() { default: Return square root Math.sqrt(x) Extract x number of characters string.substr()
// code here; //code
} } Return tangent Math.tan(x) Extract between 2 positions string.substring()
While loop Array methods Convert string to lower case string.toLowerCase()
function myFunction(num1,num2) { var i = 1; Add to beginning of array array.unshift(x) Convert string to upper case string.toUpperCase()
alert(num1 * num2);
} while(i < 10) { Add to end of array array.push(x) Remove whitespace from beg/end string.trim()
document.write(i + "<br>"); Remove from beginning of array array.shift() Return primitive value of string obj string.valueOf()
i++
Objects } Remove from end of array array.pop(x) Number methods
var phone = new Object(); Do While loop Reverse array order array.reverse() Return string in exponential notation number.toExponential()
phone.manufacturer = “Apple”;
phone.model = “iPhone”; var i = 1; Remove element between 2 positions array.slice(x,x) Return number in fixed-point notation number.toFixed()
do { Sort elements alphabetically array.sort() Return a number as a string number.toString()
document.write(i + "<br>");
var phone = { Join arrays array.concat(arrayToAdd) Return a string with specified length number.toPrecision()
i++;
manufacturer: Apple”,
} Check if element is in array array.includes(x) Return primitive value of object number.valueOf()
model: “iPhone”
while (i < 10);
} Join all elements in an array array.join()
For loop Convert elements to string array.toString()
for (i = 1; i < 10; i++) {
document.write(i + "<br>");
}
1
Popular Properties Dates & Date methods Operators DOM & Events
Math properties New date object new Date(); Assignment Shorthand Triggered when clicked onclick
(Same as)
Return Euler, approx 2.718 Math.E Get methods Assignment x=y (x = y) Triggered when change detected onchange
Return natural logarithm of 2. approx 0.693 Math.LN2 Get day of month getDate() Addition x += y (x = x + Triggered when user enters page onload
y)
Return natural logarithm of 10, approx 2.303 Math.LN10 Get day of week getDay() Subtraction x -= y (x = x - y) Triggered when user leaves page onunload
Base 2 logarithm of E, approx 1.443 Math.LOG2E Get year (yyyy) getFullYear() Multiplication x *= y (x = x * Triggered when mouse moves over a element onmouseover
y)
Base 10 logarithm of E, approx 0.434 Math.LOG10E Get hour of day getHours() Division x /= y (x = x / Triggered when a mouse moves out of a element onmouseout
y)
Return the value of PI, approx 3.141 Math.PI Get milliseconds 0-999 getMilliseconds() Remainder(Mod x %= y (x = x / Triggered when mouse button pressed down onmousedown
ulus) y)
Returns square root of 1/2, approx 0.707 Math.SQRT1_2 Get minutes 0-59 getMinutes() Arithmetic Operator Triggered when mouse button released onmouseup
Returns square root of 2, approx 1.414 Math.SQRT2 Get month getMonth() Addition + Triggered when keyboard button pressed down onkeydown
String properties Get seconds 0-59 getSeconds() Subtraction - Triggered when keyboard button released onkeyup
Returns the string’s constructor function string.constructor Get the time since 1/1/70 getTime() Multiplication * Triggered when keyboard button is pressed onkeypress
Return string’s length string.length Set methods Division / Triggered when error occurs with loading a onerror
external file
Used to add properties to a string object string.prototype Set day of month setDate() Remainder(Mod % Triggered after user makes selection onselect
ulus)
Number properties Set year (yyyy) setFullYear() Increment ++ Triggered when a form is submitted onsubmit
Return largest possible number in JS Number.MAX_VALUE Set hour of day setHours() Decrement -- Triggered when an element is dragged ondrag
Return smallest possible number in JS Number.MIN_VALUE Set milliseconds 0-999 setMilliseconds() Comparison Operator Triggered when an element is dropped ondrop
Not a number value Number.NaN Set minutes 0-59 setMinutes() Equality == Triggered when a screen is touched ontouchstart
Represents negative infinity Number.NEGATIVE_INFINITY Set month setMonth() Strict equality === Triggered when a finger is removed from screen ontouchend
Represents infinity Number.POSITIVE_INFINITY Set seconds 0-59 setSeconds() Inequality !=
Used to add properties to a number object Number.prototype Strict inequality !==
Array properties Greater than >
Returns the length of the array array.length Greater than or >=
equal to
Used to add properties to array objects array.prototype Less than <
Less than or <=
equal to
Logical Operator
And &&
Or ||
Not !