Type JavaScript Examples: | Output Timing: s |
Quick reference to basic JavaScript commands. Search online for tutorials
Comment | // this is a one-line comment /* this comment could contain linebreaks */ |
Constant | numbers 12.3 and strings "hi", "hi".charAt(0) is "h" |
Variable | start with a letter, then use letters, digits or _ (declared with var) |
Expression | built from +, -, *, /, pow(x,y), sqrt(), PI, E, log(), floor(), ceil(), random(), sin(), cos(), tan(), atan() and constants, variables, functions |
Assignment | v = expression; |
Block | { statement 1; ... statement n; } |
Condition | expression == expression or <=, <, >, >=, !=... or !cond, cond1 && cond2, cond1 || cond2 |
if - else | if (condition) block 1 else block 2 // optional |
for-loop | for (var i=start; endcondition; i++) block |
while-loop | while (condition) block |
Function | f = function(v1, ..., vn) { statement 1; ... return expression; } |
List (array) | a = [1,2,"hi"] a[0] is 1, a.length is 3, a+[4] is [1,2,"hi",4] |
Object (hash tables) |
object = {attribute:expression, method:function} a = {x:2, y:3, s:function(){return this.x+this.y}} a.x is 3, a["x"] is 3, a.s() is 5 |
Output | write("Hello"); writeln(" world"); |
This page is useful for experimenting with basic JavaScript and simple algorithms (e.g. those covered in a discrete mathematics course). It should work in any browser that has JavaScript enabled (including smartphone browsers).
You can save your own local copy of this page (using Save As...), and use it even when you are not connected to the internet.