Wsu Js Cheat Sheet
Wsu Js Cheat Sheet
Wsu Js Cheat Sheet
#################
TABLE OF CONTENTS
JavaScript Basics 3
Variables in JavaScript 3
The Next Level: Arrays 4
Operators 5
Functions 6
JavaScript Loop 7
If - Else Statements 8
Strings 8
Regular Expression Syntax 9
Numbers and Math 11
Dealing with Dates in JavaScript 13
DOM Mode 14
Working with the User Browser 17
JavaScript Events 19
2 of 24
JAVASCRIPT BASICS
</script>
Including Comments
Single line comments - //
VARIABLES IN JAVASCRIPT
const — Can not be reassigned and not accessible before they appear
within the code.
Data Types
Numbers — var age = 23
Variables — var x
Operations — var b = 1 + 2 + 3
3 of 24
True or fase statements — var c = true
Objects
var person = {
firstName:"John",
lastName:"Doe",
age:20,
nationality:"German"
};
Array Methods
concat() — Join several arrays into one
4 of 24
sort() — Sorts elements alphabetically
OPERATORS
Basic Operators
+ — Addition
- — Subtraction
* — Multiplication
/ — Division
% — Modulus (remainder )
++ — Increment numbers
-- — Decrement numbers
Comparison Operators
== — Equal to
!= — Not equal
5 of 24
<= — Less than or equal to
? — Ternary operator
Logical Operators
&& — Logical and
|| — Logical or
! — Logical not
Bitwise Operators
& — AND statement
| — OR statement
~ — NOT
^ — XOR
FUNCTIONS
function name(parameter1, parameter2, parameter3) {
Outputting Data
alert() — Output data in an alert box in the browser window
6 of 24
document.write() — Write directly to the HTML document
Global Functions
decodeURI() — Decodes a Uniform Resource Identifier (URI) created
by encodeURI or similar
JAVASCRIPT LOOPS
for (before loop; condition for loop; execute after loop) {
7 of 24
IF - ELSE STATEMENTS
if (condition) {
} else {
STRINGS
var person = "John Doe";
Escape Characters
\' — Single quote
\\ — Backslash
\b — Backspace
\f — Form feed
\n — New line
\r — Carriage return
\t — Horizontal tabulator
\v — Vertical tabulator
String Methods
charAt() — Returns a character at a specified position inside a
string
8 of 24
fromCharCode() — Returns a string created from the specified sequence
of UTF-16 code units
Pattern Modifiers
e — Evaluate replacement
9 of 24
x — Allow comments and whitespace in pattern
U — Ungreedy pattern
Brackets
[abc] — Find any of the characters between the brackets
Metacharacters
. — 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
\0 — NUL character
\t — Tab character
10 of 24
\xdd — Character specified by a hexadecimal number dd
Quantifiers
n+ — Matches any string that contains at least one n
Number Properties
MAX_VALUE — The maximum numeric value representable in JavaScript
Number Methods
toExponential() — Returns a string with a rounded number written as
exponential notation
11 of 24
toFixed() — Returns the string of a number with a specified number of
decimals
Math Properties
E — Euler’s number
PI — The number PI
Math Methods
abs(x) — Returns the absolute (positive) value of x
exp(x) — Value of Ex
12 of 24
max(x,y,z,...,n) — Returns the number with the highest value
Setting Dates
Date() — Creates a new date object with the current date and time
DOM MODE
Node Properties
attributes — Returns a live collection of all attributes registered
to and element
nextSibling — Gives you the next node at the same node tree level
14 of 24
nodeType — Returns the type of a node
Node Methods
appendChild() — Adds a new child node to an element as the last child
node
15 of 24
lookupPrefix() — Returns a DOMString containing the prefix for a
given namespaceURI, if present
normalize() — Joins adjacent text nodes and removes empty text nodes
in an element
Element Methods
getAttribute() — Returns the specified attribute value of an element
node
Window Properties
closed — Checks whether a window has been closed or not and returns
true or false
17 of 24
screenLeft — The horizontal coordinate of the window (relative to
screen)
Window Methods
alert() — Displays an alert box with a message and an OK button
prompt() — Displays a dialogue box that prompts the visitor for input
18 of 24
setInterval() — Calls a function or evaluates an expression at
specified intervals
Screen Properties
availHeight — Returns the height of the screen (excluding the Windows
Taskbar)
JAVASCRIPT EVENTS
Mouse
onclick — The event occurs when the user clicks on an element
19 of 24
onmouseout — User moves the mouse pointer out of an element or one of
its children
Keyboard
onkeydown — When the user is pressing a key down
Frame
onabort — The loading of a media is aborted
Form
onblur — When an element loses focus
20 of 24
oninput — User input on an element
onselect — The user selects some text (for <input> and <textarea>)
Drag
ondrag — An element is dragged
Clipboard
oncopy — User copies the content of an element
Media
onabort — Media loading is aborted
oncanplay — The browser can start playing media (e.g. a file has
buffered enough)
21 of 24
onended — The media has reach its end
Animation
animationend — A CSS animation is complete
Other
22 of 24
transitionend — Fired when a CSS transition has completed
Errors
try — Lets you define a block of code to test for errors
finally — Lets you execute code, after try and catch, regardless of
the result
23 of 24
SyntaxError — A syntax error has occurred
24 of 24