Show Menu
Cheatography

Javascript Cheat Sheet (DRAFT) by

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Script tag

In HTML, Javascript must be inserted between the <sc­rip­t> </s­cri­pt> tags.

window.al­ert()

window.al­ert() creates a window alert. For example:

<sc­rip­t>
window.al­ert(5 + 6);
</s­cri­pt>

Declaring variables

Muiltiple variables can be declared at once.

var x, y, z
x = 5
y = 1
z = x + y

Comments

Comments can be used to annotate code, or to prevent execution of code without removing it. It is reccom­ended to use complete sentences to ensure it is readable. Comments should be updated when code is changed to ensure it remains readable and relevant.

Single line comments start with //
Any code written on this line will be ignored by Javasc­ript.


Multi line comments start with / and end with /

Function names

Functions should be named like this: functi­ons­Nam­eLi­keThis

Class names

Classes should be named like this: ClassN­ame­dLi­keThis
 

Using innerHTML

To access an HTML element, Javascript can use the docume­nt.g­et­Ele­men­tBy­Id(id) method.

<h1­>My First Web Page</­h1>
<p>My First Paragr­aph­</p>

<p id="­dem­o"><­/p>

<sc­rip­t>
docume­nt.g­et­Ele­men­tBy­Id(­"­dem­o").i­nn­erHTML = 5 + 6;
</s­cri­pt>

Javascript has been used above to insert the text into the paragraph.

Functions

A function is a block of code which performs a task.

A JavaScript function is defined with the function keyword, followed by a name, followed by parent­heses (). The code to be executed, by the function, is placed inside curly brackets: {}


function name(p­ara­meter1, parame­ter2, parame­ter3) {
code to be executed
}

Variable Names

Variables should be named like this: variab­les­Nam­edL­ikeThis

Naming Strings

When naming strings it is preferable to use single quotes ' than double quotes "
This is helpful when creating strings which include HTML.
 

Using docume­nt.w­rite()

 
docume­nt.w­rite() can also be used

<sc­rip­t>
docume­nt.w­rite(5 + 6);
</s­cri­pt>

using consol­e.log()

You can also use consol­e.log() to display data.

<sc­rip­t>
consol­e.l­og(­5+6);
</s­cri­pt>

Objects

Objects are variables too. But objects can contain many values. This code assigns many values (Fiat, 500, white) to a variable named car:

var car = {type:­"­Fia­t", model:­"­500­", color:­"­whi­te"};

Constants

If a value is constant and immutable it should be named in CONSTA­NT_­VAL­UE_CASE

Semi-C­olons

Semi-c­olons should always be used after a statement in JS. Not using semi-c­olons can lead to problems which are hard to debug. Nether­the­less, it is a good habit to get into.

Naming variables

When naming variables, do not use these reserved words!

abstract else instanceof super
boolean enum int switch
break export interface synchr­onized
byte extends let this
case false long throw
catch final native throws
char finally new transient
class float null true
const for package try
continue function private typeof
debugger goto protected var
default if public void
delete implements return volatile
do import short while
double in static with