JavaScript

Prompt for a value

var color = window.prompt("What is your favourite color ?", "Default value");

OK / Cancel

var heSaidOk = confirm("If you press OK, we'll do this!");

Base64

Encoding

btoa("Hello world!");

Decoding

atob("SGVsbG8gd29ybGQh");

jQuery

Flow

Arrays

Basic operations

var arr = [];                                                // New array
arr.push("bob");                                             // Add a value
if (jQuery.inArray("value", arr) == -1) { ...}               // If "value" is not in arr, do sth
var str = arr.join(", ");                                    // str = "bob, marc, chris, dan"
var randomItem = arr[Math.floor(Math.random()*arr.length)];  // Random item
arr.splice(index, 1);                                        // Remove 1 item at index "index"

Fill an array with numbers from 1 to n

var n = 50;
var numbers = $.map($(Array(n)),function(val, i) { return i+1; })

Sort a numeric array

function numOrd(a, b){ return (a-b); }
var myArray = ...;
myArray = myArray.sort(numOrd);

Do something for each element

var arr = [1, 2, 3];
arr.forEach(function(item) { ... });

Remove some elements

var arr = [1, 2, 3];
arr = $.grep(arr, function(item, index) {
   if (someTest(item)) {
      return false; // We remove the element
   }
   return true; // We keep the element
});

Form elements

Select

$("#mySelect").empty();         // Empty the dropdown
$("<option />", { val: your_value, text: your_text }).appendTo($("#mySelect"));   // Add a new option
$("#mySelect").val();           // Returns the value of the option
$("#mySelect option:selected"); // Returns the label of the option
Print/export