Including JavaScript in an HTML Page
<script type="text/javascript">
// JS code goes here
</script>
Call an External JavaScript File
<script src="myscript.js"></script><code></code>
Including Comments
//
Single line comments
/* comment here */
Multi-line comments
var const let
var age = 23 var x var a = "init" var b = 1 + 2 + 3 var c = true const PI = 3.14 var name = {firstName:"John", lastName:”Doe"} var person = {
firstName:"John",
lastName:"Doe",
age:20,
nationality:"German"
};
var fruit = ["Banana", "Apple", "Pear"];
concat() indexOf() join() lastIndexOf() pop() push() reverse() shift() slice() sort() splice() toString() unshift() valueOf()
var age = 23 var x var a = "init" var b = 1 + 2 + 3 var c = true const PI = 3.14 var name = {firstName:"John", lastName:”Doe"} Basic Operators
| + | Addition |
| - | Subtraction |
| * | Multiplication |
| / | Division |
| (..) | Grouping operator |
| % | Modulus (remainder) |
| ++ | Increment numbers |
| -- | Decrement numbers |
Comparison Operator
| == | Equal to |
| === | Equal value and equal type |
| != | Not equal |
| !== | Not equal value or not equal type |
| > | Greater than |
| < | Less than |
| >= | Greater than or equal to |
| <= | Less than or equal to |
| ? | Ternary operator |
Logical Operators
| && | Logical and |
| || | Logical or |
| ! | Logical not |
Bitwise Operators
| & | AND statement |
| | | OR statement |
| ~ | NOT |
| ^ | XOR |
| << | Left shift |
| >> | Right shift |
| <<< | Zero fill right shift |
function name(parameter1, parameter2, parameter3) { // what the function does }
alert() confirm() console.log() document.write() prompt()
decodeURI() decodeURIComponent() encodeURI() encodeURIComponent() eval() isFinite() isNaN() Number() parseFloat() parseInt()
for (before loop; condition for loop; execute after loop) {
// what to do during the loop
}
for while do while break continue if (condition) {
// what to do if condition is met
} else {
// what to do if condition is not met
}
var person = "John Doe";
| \' | — | Single quote |
| \" | — | Double quote |
| \\ | — | Backslash |
| \b | — | Backspace |
| \f | — | Form feed |
| \n | — | New line |
| \r | — | Carriage return |
| \t | — | Horizontal tabulator |
| \v | — | Vertical tabulator |
charAt() charCodeAt() concat() fromCharCode() indexOf() lastIndexOf() match() replace() search() slice() split() substr() substring() toLowerCase() toUpperCase() valueOf() | e | — | Evaluate replacement |
| i | — | Perform case-insensitive matching |
| g | — | Perform global matching |
| m | — | Perform multiple line matching |
| s | — | Treat strings as single line |
| x | — | Allow comments and whitespace in pattern |
| U | — | Non Greedy pattern |
| [abc] | Find any of the characters between the brackets |
| [^abc] | Find any character not in the brackets |
| [0-9] | Used to find any digit from 0 to 9 |
| [A-z] | Find any character from uppercase A to lowercase z |
| (a|b|c) | Find any of the alternatives separated with | |
| . | — | Find a single character, except newline or line terminator |
| \w | — | Word character |
| \W | — | Non-word character |
| \d | — | A digit |
| \D | — | A non-digit character |
| \s | — | Whitespace character |
| \S | — | Non-whitespace character |
| \b | — | Find a match at the beginning/end of a word |
| \B | — | A match not at the beginning/end of a word |
| \0 | — | NUL character |
| \n | — | A new line character |
| \f | — | Form feed character |
| \r | — | Carriage return character |
| \t | — | Tab character |
| \v | — | Vertical tab character |
| \xxx | — | The character specified by an octal number xxx |
| \xdd | — | Character specified by a hexadecimal number dd |
| \uxxxx | — | The Unicode character specified by a hexadecimal number xxxx |
| n+ | — | Matches any string that contains at least one n |
| n* | — | Any string that contains zero or more occurrences of n |
| n? | — | A string that contains zero or one occurrences of n |
| n{X} | — | String that contains a sequence of X n’s |
| n{X,Y} | — | Strings that contains a sequence of X to Y n’s |
| n{X,} | — | Matches any string that contains a sequence of at least X n’s |
| n$ | — | Any string with n at the end of it |
| ^n | — | String with n at the beginning of it |
| ?=n | — | Any string that is followed by a specific string n |
| ?!n | — | String that is not followed by a specific string n |
MAX_VALUE MIN_VALUE NaN NEGATIVE_INFINITY POSITIVE_INFINITY
toExponential() toFixed() toPrecision() toString() valueOf() | E | Euler’s number |
| LN2 | The natural logarithm of 2 |
| LN10 | Natural logarithm of 10 |
| LOG2E | Base 2 logarithm of E |
| LOG10E | Base 10 logarithm of E |
| PI | The number PI |
| SQRT1_2 | Square root of 1/2 |
| SQRT2 | The square root of 2 |
abs(x) acos(x) asin(x) atan(x) atan2(y,x) ceil(x) cos(x) exp(x) floor(x) log(x) max(x,y,z,...,n) min(x,y,z,...,n) pow(x,y) random() round(x) sin(x) sqrt(x) tan(x)
Date() Date(2017, 5, 21, 3, 23, 10, 0) Date("2017-06-23")
getDate() getDay() getFullYear() getHours() getMilliseconds() getMinutes() getMonth() getSeconds() getTime() getUTCDate() parse
setDate() setFullYear() setHours() setMilliseconds() setMinutes() setMonth() setSeconds() setTime() setUTCDate()
attributes baseURI childNodes firstChild lastChild nextSibling nodeName nodeType nodeValue ownerDocument parentNode previousSibling textContent
appendChild() cloneNode() compareDocumentPosition() getFeature() hasAttributes() hasChildNodes() insertBefore() isDefaultNamespace() isEqualNode() isSameNode() isSupported() lookupNamespaceURI() lookupPrefix() normalize() removeChild() replaceChild()
getAttribute() getAttributeNS() getAttributeNode() getAttributeNodeNS() getElementsByTagName() getElementsByTagNameNS() hasAttribute() hasAttributeNS() removeAttribute() removeAttributeNS() removeAttributeNode() setAttribute() setAttributeNS() setAttributeNode() setAttributeNodeNS()
closed defaultStatus document frames history innerHeight innerWidth length location name navigator opener outerHeight outerWidth pageXOffset pageYOffset parent screen screenLeft screenTop screenX screenY self status top
alert() blur() clearInterval() clearTimeout() close() confirm() focus() moveBy() moveTo() open() print() prompt() resizeBy() resizeTo() scrollBy() scrollTo() setInterval() setTimeout() stop()
availHeight availWidth colorDepth height pixelDepth width
onclick oncontextmenu ondblclick onmousedown onmouseenter onmouseleave onmousemove onmouseover onmouseout onmouseup
onkeydown onkeypress onkeyup
onabort onbeforeunload onerror onhashchange onload onpagehide onpageshow onresize onscroll onunload
onblur onchange onfocus onfocusin onfocusout oninput oninvalid onreset onsearch onselect onsubmit
ondrag ondragend ondragenter ondragleave ondragover ondragstart ondrop
oncopy oncut onpaste
onabort oncanplay oncanplaythrough ondurationchange onended onerror onloadeddata onloadedmetadata onloadstart onpause onplay onplaying onprogress onratechange onseeked onseeking onstalled onsuspend ontimeupdate onvolumechange onwaiting
animationend animationiteration animationstart
transitionend onmessage onoffline ononline onpopstate onshow onstorage ontoggle onwheel ontouchcancel ontouchend ontouchmove ontouchstart
try catch throw finally
name message EvalError RangeError ReferenceError SyntaxError TypeError URIError