Show Menu
Cheatography

Javascript Basics Cheat Sheet (DRAFT) by

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

Types of data

string
"­"
var name = "John Doe";
integer
45
var years = 14;
float
12.5
var price = 405.17;
boolean
true/false
var registered = true;
array
[]
var cars = ['Ford', 'Fiat', 'Merce­des'];
object
{}
var user = {
   name: "John",
   lastname:"Doe"
}

Functions

declar­ation
function doSome­thing() {
   console.log('done');
}
declar­ation as expression
same as above
var doSome­thing = function(){
   console.log('done');
}
anonimous function
function(){
   console.log('done');
}
calling (execu­ting) function
doSome­thi­ng();
function with parameters
function doSome­thi­ng(­param1, param2) {
   param1=param2 + "some text";
   console.log('param2:' + param1);
}
calling function with parameters
doSome­thi­ng(­'value of param1', 'value of param2');
calling function with parameters
sending variables
var a = 1;
var b = 2;
doSomething(p, d);
inside of function p will be 1 and d will be 2
function that returns
function doSome­thing() {
   var result = "­result to be returned";
   return result;
}
getting result from a function
function needs to have return
var fresult = dosome­thi­ng();
getting result from anonimous function
function needs to have return
var fresult = function() {
   var result = "­result to be returned";
   return result;
}
fresult will be "­result to be return­ed"

Objects