Javascript cheatsheet for beginners
IF-ELSE STATEMENTS –
if (condition) {
//what to do if condition is met
}
else{
//what to do if condition is not met
}
STRINGS-
var person = “John Doe”;
ESCAPE CHARACTERS:
\’ -Single quote
\” -Double quote
\\ – Backslash
\b – Backspace
\t – Horizontal tabulator
\n -New line
STRING METHODS-
- charAt() – Returns a character at a specified position inside a string
- charCodeAt() – Gives you the unicode of character at that position
- concat() – Concatenates (joins) two or more strings into one
- fromCharCode() – Returns a string created from the specified sequence of UTF-16 code units
- indexOf() – Provides the position of the first occurrence of a specified text within a string
- lastindexOf() – Same as indexOf() but with the last occurrence, searching backwards
- match() – Retrieves the matches of a string against a search pattern
- replace() – Find and replace specific text in a string
- search() – Executes a search for a matching text and returns its position
- slice()- Extracts a section of a string and returns ft as a neW string
- split()- Splits a string object into an array of strings at a specified position
- substr() – Similar to slice() but extracts a substring depended on a specified number of characters
- substring() – Also similar to slice) but can’t accept negative indices
- toUpperCase() – Convert strings to uppercase
- valueOf() – Returns the primitive value (that has no properties or methods) of a string object
GLOBAL FUNCTIONS –
- decodeURI() – Decodes a Uniform Resource identifier (URI) created by encodeURI or similar
- decodeURIComponent() – Decodes a URI component
- encodeURI()- Encodes a URI into UTF-8
- encodeURIComponent() – Same but for URI components
- eval()- Evaluates JavaScript code represented as a string
- isFinite() – Determines whether a passed value is a finite number
- isNaN()- Determines whether a value is NaN or not
LOOPS –
for (before loop; condition for loop; execute after loop) {// what to do during the loop }
- for –The most common way to create a loop in Javascript
- while – Sets up conditions under which a loop executes
- do while – Similar to the while loop, however, it executes at least once and performs a check at the end to see if the condition is met to execute again
- break – Used to stop and exit the cycle at certain conditions
- continue – Skip parts of the cycle if certain conditions are met
EVENTS –
Mouse:
- onclick- The event occurs when the user clicks on an element
- oncontextmenu – User right-clicks on an element to open a context menu
- ondblclick – The user double-clicks on an element
- onmousedown – User presses a mouse button over an element
- onmouseenter – The pointer moves onto an element
- onmouseleave – Pointer moves out of an element
- onmousemove- The pointer is moving while it is over an element
- onmouseover- When the pointer is moved onto an element or one of its children
- onmouseout – User moves the mouse pointer out of an element or one of its children
- onmouseup – The user releases a mouse button while over an element
Keyboard:
- onkeydown- When the user is pressing a key down
- onkeypress – The moment the user starts pressing a key
- onkeyup – The user releases a key
Form:
- onblur- When an element loses focus
- onchange- The content of a form element changes (for <input>, <select>and <textarea>)
- onfocus – An element gets focus
- onfocusin – When an element is about to get focus
- onfocusout – The element is about to lose focus
- oninput- User input on an element
- oninvalid – An element is invalid
- onreset – A form is reset
- onsearch – The user writes something in a search field (for <input=”search”>)
- onsubmit – A form is submitted