Show Menu

JavaScript Cheat Sheet by Pyro19D

JavaScript

Value Types

NumberAny numeric value
StringCharacters inside quote marks
BooleanTrue or False
NullEmpty and meanin­gless
ObjectAny value associated with the object
FunctionValue returned by a function

Operators

x + y (numeric)Adds x and y together
x + y (string)Concat­inates x and y together
x - ySubtracts y from x
x * yMultiplies x and y together
x / yDivides x by y
x % yModulus: The remainder when x is divided by y
x++, ++xAdds one to x
x--, --xSubtracts one from x
-xReverses the sign on x

Assignments

x = ySets x to the value of y
x += ySame as x = x + y
x -= ySame as x = x - y
x *= ySame as x = x * y
x /= ySame as x = x / y
x %= ySame as x = x % y

Adding to an array

.length propertyAdds one value to the end of an array
push()Adds one or more items to the end of an array
unshift()Adds one or more items to the beginning of an array.

Misc

parseInt()Takes a value and tries to convert it to an integer
isNaN()Returns true if something is not a number.
setTim­eout()Specify that am action should occur on a particular schedule
Math.floorRounds a number down
Math.r­andomGeneraters a random number between 0 and 1
getEle­men­tsB­yTa­gName()
getEle­men­tById()
getEle­men­tsB­yCl­ass­Name()
elemen­t.i­nne­rHTMLreturns all the HTML between the opening and closing tags
elemen­t.n­odeTypeproperty, returns the number of the node type
elemen­t.c­hil­dNodesA node list of child nodes that are indexed numeri­cally, and appear in source order.
elemen­t.c­hil­dNo­des.lengthReturns the number of child nodes of the element
 

Event Handlers

onabortThe user aborted loading the page
onblurThe user left the object
onchangeThe user changed the object
onclickThe user clicked an object
onerrorThe script encoun­tered an error
onfocusThe user made an object active
onloadThe object finished loading
onmous­eoverThe cursor moved over an object
onmouseoutThe cursor moved off an object
onselectThe user selected the contents of an object
onsubmitThe user submitted a form
onunloadThe user left the page
onmoveThe window is moved
onmous­edownA mouse button is pressed down
onmouseupA mouse button is released
onmous­emoveThe mouse moves
ondblclickA double click of a mouse button
onclickA single click (down and then up) of a mouse button
onresetA reset button is clicked on a form
onkeydownTriggered when a key is pressed down
onkeyupTriggered when a key is released
onkeypressTriggered when a key is pressed and then released

Comparisons

x == yReturns true if x and y are equal
x === yReturns true if x and y are identical
x != yReturns true if x and y are not equal
x !== yReturns true if x and y are not identical
x > yReturns true if x is greater than y
x >= yReturns true if x is greater than or equal to y
x < yReturns true if x is less than y
x <= yReturns true if x is less than or equal to y
x && yReturns true if both x and y are true
x || yReturns true if either x or y are true
!xReturns true if x is false

Remove item from an array

pop()Removes the last item from the array
shift()Removes the first item from the array
 

jQuery - Adding and removing content to a page

.html()Retrieve or replaces the HTML inside the selection
.text()Works like .html() but does not accept HTML tags
.append()Adds HTML as the last child element of the selected element
.prepend()Like .append(), but adds HTML directly after the opening tag for the selection
.before()Add HTML outside of a selection, just before the selected element
.after()Add HTML outside of a selection, just after the selected element
.remove()Completely remove a selected element
.repla­ceW­ith()Completely replace selected element

jQuery - other

.addClassAdds a specified class to an element
.remov­eClassRemoves a specified class from an element
.toggl­eCl­ass()Add a class if it doesn't already exist, or remove the class if it does
.css()Lets you directly read or change CSS properties of an element
.attr()Reads a specified HTML attribute from a tag. Sets the specified HTML attribute if a second argument is passed.
.remov­eAttr()Completely removes an attribute from a tag.
.each()To loop through selection of elements with your own anonymous function.

jQuery Filters

:even and :oddSelects every other element in a group
:first and :lastSelects the first or the last element in a group
:not()to find elements that don't match a particular selctor type
:has()finds elements that contain another selector
:conta­ins()Finds elements that contain specific text
:hiddenLocates elements that are hidden
:visibleLocates elements that are visible
 

Share This Cheat Sheet!

Favourited by 42 Members:

PaulBlp SkyeBella clazito AdamJTP vbgenie Eulerintegral333 dmooreTrinity claysmith Theosis Dranzz tuuli and 32 more ...

Comments

el el, 11:46 24 Jan 13

you got a mistake there, length does not add a value to an array. :(

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.