More Interpreted Languages: Tcl, Lua, JavaScript, Io

a side-by-side reference sheet

grammar and invocation | variables and expressions | arithmetic and logic | strings | regexes | dates and time
arrays | dictionaries | functions | execution control | file handles | files | directories
processes and environment | libraries and namespaces | objects | reflection | contact

tcl lua javascript io
version used
 
8.5 5.1 ECMAScript 5 20090105
show version $ tclsh
% info tclversion
$ lua -v $ js -e 'print(version())'

$ node --version
$ io
Io> System version
grammar and invocation
tcl lua javascript io
interpreter
 
$ tclsh foo.tcl $ lua foo.lua $ js foo.js

$ node foo.js
$ io foo.io
repl
 
$ tclsh $ lua $ js

$ node
$ io
command line program none $ lua -e 'print("hi world!")' $ js -e 'print("hi world!");'

$ node -e 'var sys = require("sys");
  sys.puts("hi world!");'
none
block delimiters
 
{} or "" do end {} ()
statement separator newline or ;

newline not a separator inside {}, "", [] or after backslash: \
newline or ;

newline not separator inside {}, (), or after binary operator.

newline can be put in "" or '' if preceded by backslash
; or newline

newline not separator inside (), [], {}, "", '', or after binary operator

newline sometimes not separator when following line would not parse as a valid statement
newline or ;

newline not separator inside () or ""
are expressions statements no no yes yes
end-of-line comment # comment -- comment // comment // comment
# comment
multiple line comment if (0) {
  commented out
  can contain {} if balanced
}
--[[
  commented out
  also commented out
]]
/* comment
another comment */
/* comment
another comment */
variables and expressions
tcl lua javascript io
local variable # set variable inside procedure
proc foo {args} {
  set x 1
  
}
local x = 1 var x = 1; x := 1
global variable # set variable outside procedure
set g 1

proc incr_global {} {
  global g
  incr g
}
-- assign without using local
g = 1

function incr_global()
  g = g + 1
end
// assign without using var
g = 1;

function incr_global () { g++; }
# no globals, but root object is
# named Lobby

g := 1

incr_global := block(
  Lobby g = Lobby g + 1
)
assignment set x 1 x = 1 x = 1; x := 1

# exception if x doesn't exist:
x = 1
parallel assignment
 
lassign {1 2 3} x y z

# 3 is discarded:
lassign {1 2 3} x y

# z is set to "":
lassign {1 2} x y z
x, y, z = 1, 2, 3

-- 3 is discarded:
x, y = 1, 2, 3

-- z is set to nil:
x, y, z = 1, 2
none none
swap lassign "$x $y" y x x, y = y, x tmp = x;
x = y;
y = tmp;
tmp := x
x = y
y = tmp
null
 
"" nil null nil
null test
 
v eq "" v == nil v === null v == nil
undefined variable access error nil undefined raises exception
conditional expression expr $x > 0 ? $x : -$x none x > 0 ? x : -x if(x > 0, x, -x)
arithmetic and logic
tcl lua javascript io
true and false
 
1 0 true false true false true false
falsehoods 0 "false" "no"
most strings cause error in boolean context; nonzero numbers are true
false nil false null undefined "" 0 NaN false nil
logical operators && || ! and or not && || ! not is postfix:
and or not
relational expression if {$x > 3} {}
# outside of conditionals use expr:
expr $x > 3
x > 3 x > 3 x > 3
relational operators == != > < >= <=

# string comparison:
eq ne
== ~= < > >= <= === !== < > >= <=

perform type coercion:
== !=
== != < > >= <=
min and max expr min(1,2,3)
expr max(1,2,3)
math.min(1,2,3)
math.max(1,2,3)
Math.min(1,2,3)
Math.max(1,2,3)
list(1,2,3) min
list(1,2,3) max
arithmetic expression expr 1 + 3
# expr not needed in conditionals:
if {1 + 3} {}
1 + 3 1 + 3 1 + 3
arithmetic operators
 
+ - * none / % + - * / none % ^ + - * / none % + - * / none %
integer division
 
expr $x / $y math.floor(x / y) Math.floor(x / y) (x / y) floor
integer division by zero
 
error returns assignable value inf, nan, or -inf depending upon whether dividend is positive, zero, or negative.

There are no literals for any of these values.
returns assignable value Infinity, NaN, or -Infinity depending upon whether dividend is positive, zero, or negative.

There are literals for
Infinity and NaN.
returns assignable value inf, nan, or -inf depending upon whether dividend is positive, zero, or negative.

There are no literals for any of these values.
float division
 
expr $x * 1.0 / $y x / y x / y x / y
float division by zero
 
returns assignable value Inf if dividend is positive and -Inf if negative. Raises error if dividend is zero.

There is a literal for
Inf.
same behavior as for integers same behavior as for integers same behavior as for integers
power expr 2 ** 32
expr pow(2, 32)
2 ^ 32
math.pow(2, 32)
Math.pow(2, 32) 2 ** 32
sqrt
 
expr sqrt(2) math.sqrt(2) Math.sqrt(2) 2 sqrt
sqrt -1
 
error nan NaN nan
transcendental functions exp log sin cos tan asin acos atan atan2
# how to use math functions:
expr exp(2)
expr atan2(1, 1)
::tcl::mathfunc::exp 2
::tcl::mathfunc::atan2 1 1
math.exp math.log math.sin math.cos math.tan math.asin math.acos math.atan math.atan2 Math.exp Math.log Math.sin Math.cos Math.tan Math.asin Math.acos Math.atan Math.atan2 exp log sin cos tan asin acos atan atan2
float truncation
round towards zero, round to nearest integer, round down, round up
expr int(3.1)
expr round(3.1)
expr floor(3.1)
expr ceil(3.1)
none
none
math.floor(3.1)
math.ceil(3.1)
none
Math.round(3.1)
Math.floor(3.1)
Math.ceil(3.1)
none
3.1 round
3.1 floor
3.1 ceil
absolute value
 
expr abs(-3) math.abs(-3) Math.abs(-3) (-3) abs
integer overflow
 
arbitrary length integers introduced in 8.5 all numbers are floats all numbers are floats conversion to float
float overflow
 
error inf Infinity inf
random number
uniform integer, uniform float, normal float
expr int(rand() * 100)
expr rand()
none
math.random(100) - 1
math.random()
none
Math.floor(Math.random() * 100)
Math.random()
none
Random value(100) floor
Random value
Random gaussian
random seed
how to set
expr srand(17) math.randomseed(17) none Random setSeed(17)
bit operators
left shift, right shift, and, inclusive or, exclusive or, complement
<< >> & | ^ ~ none << >> & | ^ ~ << >> & | ^ bitwiseComplement
strings
tcl lua javascript io
string literal "don't say \"no\""
{don't say "no"}
"don't say \"no\""
'don\'t say "no"'
"don't say \"no\""
'don\'t say "no"'
"don't say \"no\""
newline in literal
 
yes yes, if preceded by backslash yes no
character escapes in double quotes:
\a \b \f \n \r \t \v \\ \" \oooo \uhhhh \xhh
single and double quotes:
\a \b \f \n \r \t \v \" \' \\ \ddd
single and double quotes:
\b \f \n \r \t \v \uhhhh \xhh \" \' \\
\a \b \f \n \r \t \v \" \\
variable interpolation set count 3
set item "ball"
"$count ${item}s"
none none count := 3
item := "ball"
"#{count} #{item}s" interpolate
string concatenation set s1 "Hello, "
set s2 "World!"
set s $s1$s2
s = "Hello, " .. "World!" s = "Hello, " + "World!"; s := "Hello, " .. "World!"
split
 
split "do re mi" none "do re mi".split(" ") "do re mi" split(" ")
join
 
join [list "do" "re" "mi"] " " table.concat({"do","re","mi"}, " ") ["do", "re", "mi"].join(" ") list("do", "re", "mi") join(" ")
sprintf set fmt "lorem %s %d %f"
format $fmt "ipsum" 13 3.7
string.format("lorem %s %d %f",
  "ipsum", 13, 3.7)
none none
case manipulation string toupper "lorem"
string tolower "LOREM"
none
string.upper("lorem")
string.lower("LOREM")
none
"lorem".toUpperCase()
"LOREM".toLowerCase()
none
"lorem" asUppercase
"LOREM" asLowercase
"lorem" asCapitalized
strip string trim " lorem "
string trimleft " lorem"
string trimright "lorem "
none " lorem ".trim()
# some browsers:
" lorem".trimLeft()
"lorem ".trimRight()
" lorem " asMutable strip
" lorem" asMutable lstrip
"lorem " asMutable rstrip
pad on right, pad on left, center format "%10s" "lorem"
format "%-10s" "lorem"
none none "foo" alignRight(10, " ")
"lorem" alignLeft(10, " ")
"lorem" alignCenter(10, " ")
string to number use expr to interpret as numbers:
set x "12"
expr 7 + $x
set y ".037"
expr 73.9 + $y
7 + tonumber("12")
73.9 + tonumber(".037")
arithmetic operators attempt numeric conversion of string operands
7 + parseInt("12", 10)
73.9 + parseFloat(".037")
7 + ( "12" asNumber )
73.9 + ( ".037" asNumber )
number to string
 
all values are strings "value: " .. 8 "value: " + 8 "value: " .. (8 asString)
length
 
string length "lorem" string.len("lorem") "lorem".length "lorem" size
index of substring
 
string first "ipsum" "lorem ipsum" string.find("lorem ipsum", "ipsum") "lorem ipsum".indexOf("ipsum") "lorem ipsum" findSeq("ipsum")
extract substring
 
string range "lorem ipsum" 6 10 string.sub("lorem ipsum", 7, 11) "lorem ipsum".substr(6, 5)
"lorem ipsum".substring(6, 11)
"foo bar" exSlice(4,7)
chr and ord format %c 65
scan A %c ascii_value
string.char(65)
string.byte("A")
String.fromCharCode(65)
"A".charCodeAt(0)
65 asCharacter
"A" at(0)
regular expressions
tcl lua javascript io
character class abbreviations and anchors char class abbrevs:
. \d \D \s \S \w \W

anchors: ^ $ \A \m \M \y \Y \Z
char class abbrevs:
. %a %c %d %l %p %s %u %w %x %z

anchors: ^ $
char class abbrevs:
. \d \D \s \S \w \W

anchors: ^ $ \b \B
match test
 
if [regexp -- {1999} $s] {
  puts "party!"
}
if string.match(s, "1999") then
  print("party!")
end
if (s.match(/1999/)) {
  alert("party!");
}
case insensitive match test regexp -nocase -- {lorem} "Lorem" none "Lorem".match(/lorem/i)
modifiers -all -expanded -indices -inline
-line -lineanchor -linestop -nocase
none g i m
substitution set s "do re mi mi mi"
regsub -all -- "mi" $s "ma"
s = "do re mi mi mi"
s = string.gsub(s, "mi", "ma")
s = "do re mi mi mi";
s.replace(/mi/g, "ma");
group capture set s "2009-06-03"
set rx {^(\d{4})-(\d{2})-(\d{2})$}
regexp -- $rx $s - yr mo dy
s = "2010-06-03"
rx = "(%d+)-(%d+)-(%d+)"
yr, mo, dy = string.match(s, rx)
rx = /^(\d{4})-(\d{2})-(\d{2})$/;
m = rx.exec('2009-06-03');
yr = m[1];
mo = m[2];
dy = m[3];
dates and time
tcl lua javascript io
current date/time set t [clock seconds] t = os.time() var t = new Date(); t := Date now
to unix epoch, from unix epoch t
set t2 1315716177
t
t2 = 1315716177
Math.round(t.getTime() / 1000)
var epoch = 1315716177;
var t2 = new Date(epoch * 1000);
t asNumber
epoch := 1315716177
t2 := Date clone fromNumber(epoch)
strftime set fmt "%Y-%m-%d %H:%M:%S"
clock format $t -format $fmt
os.date("%Y-%m-%d %H:%M:%S", t) none t asString("%Y-%m-%d %H:%M:%S")
strptime none none none s := "2011-05-03 10:00:00"
fmt := "%Y-%m-%d %H:%M:%S"
t := Date fromString(s, fmt)
parse date w/o format set t [clock scan "July 7, 1999"] none var t = new Date("July 7, 1999"); none
get date parts clock format $t -format "%Y"
clock format $t -format "%m"
clock format $t -format "%d"
none t.getFullYear()
t.getMonth() + 1
t.getDate() # getDay() is day of week
t year
t month
t day
get time parts clock format $t -format "%H"
clock format $t -format "%M"
clock format $t -format "%S"
none t.getHours()
t.getMinutes()
t.getSeconds()
t hour
t minute
t second floor
build date/time from parts none none var yr = 1999;
var mo = 9;
var dy = 10;
var hr = 23;
var mi = 30;
var ss = 0;
var t = new Date(yr,mo-1,dy,hr,mi,ss);
t := Date now
t setYear(1999)
t setMonth(9)
t setDay(10)
t setHour(23)
t setMinute(30)
t setSecond(0)
sleep
 
after 500 none none System sleep(0.5)
arrays
tcl lua javascript io
literal
 
set nums [list 1 2 3 4]
set nums {1 2 3 4}
nums = { 1, 2, 3, 4 } nums = [1,2,3,4] nums := list(1,2,3,4)
size
 
llength $nums inconsistent when array contains nil values:
# nums
nums.length nums size
lookup
 
lindex $nums 0 nums[1] nums[0] nums at(0)
slice
 
lrange $nums 1 2 none nums.slice(1,3) nums slice(1,3)
concatenation
 
set a [concat {1 2 3} {4 5 6}] none a = [1,2,3].concat([4,5,6]); list(1,2,3) appendSeq(list(4,5,6))
manipulate back of array set a {6 7 8}
lappend a 9
set i [lindex $a end]
set a [lreplace $a end end]
a = {6,7,8}
table.insert(a, 9)
i = table.remove(a)
a = [6,7,8];
a.push(9);
i = a.pop();
a := list(6,7,8)
a push(9)
i := a pop
manipulate front of array set a {6 7 8}
set a [concat {5} $a]
set a [lassign $a i]
a = {6,7,8}
table.insert(a, 1, 5)
i = table.remove(a, 1)
a = [6,7,8];
a.unshift(5);
i = a.shift();
a := list(6,7,8)
a prepend(5)
i := a removeFirst
iteration foreach i $nums { puts $i } for k,v in ipairs(nums) do
  print(v)
end
var len = nums.length;
for (var i=0; i<len; i++ ) {
  alert(nums[i]);
}
nums foreach(v, v println)
sort
 
set a {3 1 4 2}
set a [lsort $a]
a = {3,1,4,2}
table.sort(a)
var a = [3,1,4,2];
a.sort();
a := list(3,1,4,2)
a := a sort
reverse
 
set a {1 2 3}
set a [lreverse $a]
none var a = [1,2,3];
a.reverse();
a := list(1, 2, 3)
a := a reverse
member, not a member expr {7 in $nums}
expr {7 ni $nums}
none none nums contains(7)
nums contains(7) not
intersection
 
package require struct::set

::struct::set intersect {1 2} {2 3}
none none list(1,2) intersect(list(2,3))
union
 
package require struct::set

::struct::set union {1 2} {2 3 4}
none none list(1,2) union(list(2,3,4))
set difference
 
package require struct::set

::struct::set difference {1 2 3} {2}
none none list(1,2,3) difference(list(2))
map
 
package require struct::list

proc sqr {x} {return [expr $x*$x]}
::struct::list map {1 2 3} sqr
none nums.map(function(x) {return x*x}) nums map(x, x*x)
filter
 
package require struct::list

proc gt1 {x} {return [expr $x>1]}
::struct::list filter {1 2 3} gt1
none nums.filter(function(x) {return x>1}) nums select(x, x>1)
reduce
 
package require struct::list

::struct::list fold {1 2 3} 0
  ::tcl::mathop::+
none nums.reduce(function(m,o) {
  return m+o;
}, 0)
nums reduce(m, o, m+o, 0)
min and max element math.min(unpack({1,2,3}))
math.max(unpack({1,2,3}))
Math.min.apply(Math, [1,2,3])
Math.max.apply(Math, [1,2,3])
list(1,2,3) min
list(1,2,3) max
dictionaries
tcl lua javascript io
literal set d [dict create t 1 f 0] d = { t=1, f=0 } d = { "t":1, "f":0 };
keys do not need to be quoted if they are a legal JavaScript variable name and not a reserved word
d := Map with("t", 1, "f", 0)
size
 
dict size $d size = 0
for k, v in pairs(d) do
  size = size + 1
end
var size = 0;
for (var k in d) {
  if (d.hasOwnProperty(k)) size++;
}
d size
lookup dict get $d t d.t
d["t"]
d.t
d["t"]
d at("t")
update dict set d t 2 d["t"] = 2
d.t = 2
d["t"] = 2;
d.t = 2;
d.atPut("t", 2)
out of bounds behavior error returns nil returns undefined returns nil
is key present
 
dict exists $d t d["t"] ~= nil d.hasOwnProperty("t"); d hasKey("t")
delete dict unset d t d.t = nil
d["t"] = nil
delete d["t"];
delete d.t;
d removeAt("t")
iteration foreach {k v} $d {
  code
}
for k,v in pairs(d) do
  use k or v
end
for (var k in d) {
  use k or d[k]
}
d foreach(k, v,
  k println
  v println
)
functions
tcl lua javascript io
function declaration proc add { x y } {
  expr $x + $y
}
function add(x, y)
  return x + y
end
function add(x, y) {
  return x+y;
}
add := block(x, y,
  x+y
)
function invocation
 
add 1 2 add(1, 2) add(1, 2) add call(1, 2)
missing argument value error nil undefined nil
extra arguments
 
error ignored available in arguments ignored
default value
 
proc log {x {base 10 }} { body } none none none
variable number of arguments last arg contains list of remaining values declare function with ellipsis:
function foo(...)
  local arg = {...}
args in arguments[0], arguments[1], … with number of args in arguments.length arguments in
call message arguments
return value return arg or empty string return arg or nil return arg or undefined. If invoked with new and return value not an object, returns this return arg or last expression evaluated
multiple return values none function roots(x)
  r = math.sqrt(x)
  return r, -r
end
r1,r2 = roots(4)
none none
lambda declaration set sqr {{x} {return [expr $x*$x]}} sqr = function(x) return x*x end sqr = function(x) { return x*x; } all functions are lambdas:
sqr := block(x, x*x)
lambda invocation
 
apply $sqr 2 sqr(2) sqr(2) sqr call(2)
default scope
 
local global unless declared with local global unless declared with var local
nested function visibility not visible outside containing function visible outside containing function not visible outside containing function visible if stored in visible variable
execution control
tcl lua javascript io
if if { 0 == $n } {
  puts "no hits"
} elseif { 1 == $n } {
  puts "1 hit"
} else {
  puts "$n hits"
}
if n == 0 then
  print("no hits")
elseif n == 1 then
  print("one hit")
else
  print(n .. " hits")
end
if (0 == n) {
  alert("no hits");
} else if (1 == n) {
  alert("1 hit");
} else {
  alert(n + " hits");
}
if (n == 0,
  "no hits",
  if (n == 1,
    "1 hit",
    "#{n} hits" interpolate
  )
)
while while { $i < 100 } {
  incr i
}
while i < 100 do
  i = i + 1
end
while ( i < 100 ) {
  i += 1;
}
while (i < 100,
  i = i + 1
)
break and continue break continue break none break continue break continue
for for {set i 0} {$i < 10} {incr i} {
  puts $i
}
for i = 0, 9 do
  print(i)
end
for (var i=0; i<10; i++) {
  alert(i);
}
for (i, 0, 9,
  i println
)
raise exception error "bad arg" error "bad arg" throw "bad arg"; Exception raise("bad arg")
catch exception catch risky retval
if { retval != 0 } {
    puts "risky failed"
}
if not pcall(risky) then
  print "risky failed"
end
try {
  risky();
} catch (e) {
  alert("risky failed");
}
msg := "risky failed\n"
e := try(risky call)
e catch(Exception, write(msg))
finally/ensure none none acquire_resource();
try {
  risky();
} finally {
  release_resource();
}
none
uncaught exception behavior stderr and exit stderr and exit error to console; script terminates. Other scripts in page will execute stderr and exit
generator to be added to Tcl 8.6 crt = coroutine.create(
  function (n)
    while (true) do
      coroutine.yield(n % 2)
      n = n + 1
    end
  end
)

status, retval =
  coroutine.resume(crt, 1)

if status then
  print("parity: " .. retval)
else
  print("couldn't resume crt")
end

_, retval = coroutine.resume(crt)
print("parity: " .. retval)
none none; although Io has coroutines, yield and resume do not pass values. Also, yield returns control to a scheduler, not the caller.
file handles
tcl lua javascript io
standard file handles stdin
stdout
stderr
io.stdin
io.stdout
io.stderr
File standardInput
File standardOutput
File standardError
read line from stdin gets stdin line line = io.stdin:read() js:
var line = readline();
line := File standardInput readLine
write line to stdout puts "Hello, World!" print "Hello, World!" var sys = require('sys');

sys.puts("Hello, World!");
"Hello, World!" println
open file set f [open "/tmp/foo"] f = io.open("/tmp/foo") var fs = require('fs');

f = fs.openSync("/tmp/foo", "r");
f := File with("/tmp/foo")
f openForReading
open file for writing set f [open "/tmp/foo" "w"] f = io.open("/tmp/foo", "w") var fs = require('fs');

f = fs.openSync("/tmp/foo", "w");
f := File with("/tmp/foo")
f remove
f openForUpdating
close file
 
close $f f:close() fs.closeSync(f); f close
read line
 
gets $f f:read() f readLine
iterate over a file by line while { [gets $f s] >= 0 } {
  use s
}
for s in f:lines() do
  use s
end
var fs = require('fs');

var file = fs.readFileSync("/etc/hosts").toString();
file.split("\n").forEach(function (s) {
  use s
});
f foreachLine(s,
  use s
)
chomp string trimright $line "\r\n" none, read() and lines() remove trailing newlines none, readLine and readLines remove trailing newlines
read file
 
read $f f:read("*a") var fs = require('fs');

fs.readFileSync("/tmp/foo", "utf8");
f contents
write to file
 
puts -nonewline $f "lorem ipsum" f:write("lorem ipsum") fs.writeSync(f, "lorem ipsum"); f write("lorem ipsum")
flush file handle flush $f f:flush() none f flush
files
tcl lua node.js io
file test, regular file test file exists "/etc/hosts"
file isfile "/etc/hosts"
none var path = require('path');

path.existsSync("/etc/hosts");
file := "/etc/hosts"
File exists(file)
File with(file) isRegularFile
copy file, remove file, rename file file copy "/tmp/foo" "/tmp/bar"
file delete "/tmp/foo"
file rename "/tmp/bar" "/tmp/foo"
none var fs = require('fs');

??
fs.unlink("/tmp/foo");
fs.rename("/tmp/bar", "/tmp/foo");
foo := "/tmp/foo"
bar := "/tmp/bar"
File with(foo) copyToPath(bar)
File with(foo) remove
File with(bar) moveTo(foo)
set file permissions set s "/tmp/foo"
file attributes $s -permissions 0755
none var fs = require('fs');

fs.chmod("/tmp/foo", 0755);
temporary file set tmp [::fileutil::tempfile foo]
set f [open $tmp "w"]
puts $f "lorem ipsum"
close $f
puts "tmp file: $tmp"
f = io.tmpfile()
f:write("lorem ipsum\n")
f:close()
??
directories
tcl lua node.js io
build pathname file join "/etc" "hosts" var path = require('path');

path.join("/etc", "hosts");
dirname and basename file dirname "/etc/hosts"
file tail "/etc/hosts"
var path = require('path');

path.dirname("/etc/hosts");
path.basename("/etc/hosts");
iterate over directory by file var fs = require('fs');
var sys = require('sys');

var a = fs.readdirSync("/etc");
for (var i=0; i<a.length; i++) {
  sys.puts(a[i]);
}
dir := Directory with("/etc")
dir items foreach(file,
  file name println
)
make directory file mkdir "/tmp/foo/bar" var fs = require('fs');

fs.mkdirSync("/tmp/foo", 0755);
fs.mkdirSync("/tmp/foo/bar", 0755);
path := "/tmp/foo/bar"
Directory with(path) createIfAbsent
remove empty directory file delete "/tmp/foodir" var fs = require('fs');

fs.rmdirSync("/tmp/foo/bar");
Directory with("/tmp/foodir") remove
remove directory and contents file delete -force "/tmp/foodir"
directory test
 
file isdirectory "/tmp" Directory with("/tmp") exists
processes and environment
tcl lua node.js io
command line args [lindex $argv 0]
[lindex $argv 1]
# arg
arg[0]
arg[1]
process.argv.length
process.argv[0]
process.argv[1]
System args size
System args at(0)
System args at(1)
environment variable $env(HOME) os.getenv("HOME") System getEnvironmentVariable("HOME")
exit
 
exit 0 os.exit(0) process.exit(0) System exit(0)
set signal handller
 
none none none
external command exec ls os.execute("ls") var exec =
  require('child_process').exec;
var child = exec('ls');
System runCommand("ls")
backticks set f [ open |ls ]
read f
f = io.popen("ls")
s = f:read("*a")
var exec =
  require('child_process').exec;
var f = function(err, fout, ferr) {
  output in fout
};
var child = exec('ls', f);
(System runCommand("ls")) stdout
libraries and namespaces
tcl lua javascript io
library $ cat foo.tcl
proc add {x y} {expr $x + $y}
$ cat foo.lua
function add(x, y) return x+y end
$ cat foo.js
function add(x,y) {
  return x+y;
}
$ cat Foo.io
Foo := Object clone
Foo add := method(x, y, x + y)
import library source foo.tcl
add 3 7
require 'foo'
add(3,7)
<script src="foo.js"/>

<script>
alert(add(3,7));
</script>
imported when first referenced:
Foo add(3,7)
library path none package.path node.js, not available in repl:
require.paths
Importer FileImporter directories
library path environment variable TCLLIBPATH LUA_PATH none none
namespace declaration namespace module use an Object:
foo := Object clone
namespace separator :: . .
list installed packaged, install a package $ npm ls
$ npm install tmp
objects
tcl lua javascript io
create blank object o = {} var o = new Object(); or
var o = {};
o := Object clone
set attribute
 
o.score = 21 o.score = 21; o score := 21
get attribute if o.score == 21 then
  print("Blackjack!")
end
if (o.score == 21) {
  alert("Blackjack!");
}
if (o score == 21,
  "Blackjack!" println
)
define method function o.doubleScore(self)
  return 2 * self.score
end
o.doubleScore = function() {
  return this.score * 2;
};
o doubleScore := method(2 * score)
invoke method print("Answer: " .. o:doubleScore()) alert("Answer: " + o.doubleScore()); ans := o doubleScore asString
"Answer: " .. ans println
clone object
 
var o2 = Object.create(o); o2 := o clone
object literal o = {
  score=21,
  doubleScore=function(self)
    return 2*self.score
  end
}
var o = {
  score: 21,
  doubleScore: function() {
    return this.score * 2;
  }
};
none
reflection
tcl lua javascript io
inspect type
 
type(o) typeof o
has method?
 
typeof o.foo == 'function' o getSlot("foo") isActivatable
message passing o["foo"](1,1) o getSlot("foo")(1+1)
eval
 
assert(loadstring("x = 1+1"))() x = eval("1 + 1"); x := doString("1+1")
inspect methods o slotNames select(v,
  o getSlot(v) isActivatable)
inspect attributes o slotNames select(v,
  o getSlot(v) isActivatable not)
__________________________________________ __________________________________________ __________________________________________ __________________________________________

General

version used

The version used for verifying the examples in this cheat sheet.

javascript:

In the JavaScript standard the language is called ECMAScript. Four versions of the standard have been adopted:

ecmacript version date
1 June 1997
2 June 1998
3 December 1999
5 December 2009

Here is a summary of ECMAScript 5 compliance for recent browsers:

browser compliance
Chrome 7-12 all but "use strict"
Chrome 13+ all
Firefox 4+ all
IE 9 all but "use strict"
IE 10 all
Safari 5.1 all but Function.prototype.bind and zero-width char in identifiers
Safari 6 all

show version

How to get the version.

Grammar and Invocation

interpreter

The customary name of the interpreter and how to invoke it.

unix:

On Unix, scripts are executing by passing the file containing the script to the interpreter as an argument:

bash ~/configure.sh

If the executable bit is set, the file can be run directly:

~/configure.sh

To determine the name of the interpreter that will process the script, Unix will look for the presence of a shebang (#!) at the start of the file. If the pathname to a command follows the shebang, it will be used to interpret the script. If no shebang is present, the script will be interpreted with sh, which is bash on modern Unix systems.

Arguments that follow the command will be passed to interpreter as command line arguments.

If it is undesirable to specify the pathname of the interpreter, the env command can be used to search the PATH directories:

#!/usr/bin/env lua

javascript:

To use a browser to execute JavaScript code, put the code inside a <script> tag in an HTML page and open the page with the browser.

shebang

repl

The customary name of the repl.

javascript:

You can use the JavaScript console as a repl. Here are the keystrokes to launch the console:

chrome firefox safari
mac win mac win mac
⌥⌘J Ctrl+Shift+J Fn F12 Ctrl+Shift+K ⌥⌘C

command line program

How to pass in a program to the interpreter on the command line.

block delimiters

How blocks are delimited.

tcl:

The block delimiters {} and "" are the same as the string delimiters. Double quotes "" cause variable interpolation and as a result they are not often used to delimit blocks.

The following three lines of code behave the same:

if {true} {puts "true"}

if "true" "puts \"true\""

if "true" "puts {true}"

lua:

The function and if keywords open blocks which are terminated by end keywords. The repeat keyword opens a block which is terminated by until.

statement separator

How the parser determines the end of a statement.

are expressions statements

Whether an expression can be used where a statement is expected.

tcl:

Code fragments such as

1 + 1

or

[expr 1 + 1]

result in invalid command name errors when used as statements.

The following is a valid statement:

expr 1 + 1

The above cannot be used as an argument to a command without putting it inside square brackets, however.

Since the constructs which can be used as statements and the constructs which can be used in the positions where expressions are normally used are disjoint, we claim that expressions are not statements in Tcl.

end-of-line comment

How to make the remainder of the line a comment.

multiple line comment

How to comment out multiple lines.

tcl:

The method described requires that there not be an unmatched right curly bracket in the comment.

lua:

The double bracket notation [[ ]] is the syntax for a multiline string literal.

Variables and Expressions

local variable

How to declare a local variable.

global variable

How to declare and access a global variable.

assignment

How to assign a value to a variable.

parallel assignment

Whether parallel assignment is supported, and if so how to do it.

swap

How to swap the values in two variables.

null

The null literal.

null test

How to test if a value is null.

tcl

Tcl has has no null value.

javascript:

null == undefined is true. The triple equality operator === is thus necessary to test if a variable is null.

undefined variable access

What happens when the value in an undefined variable is accessed.

tcl:

The following test can be used to determine if a variable is defined:

expr ![info exists v]

lua:

There is no distinction between a variable being defined and a variable having a nil value. Assigning nil to a variable frees it.

javascript:

undefined == null is true. The triple equality operator === is necessary to test if a variable is undefined.

io:

The following test can be used to determine if a variable is defined:

not_defined := false
e := try(v)
e catch(Exception,
  not_defined = true
)

conditional expression

How to write a conditional expression.

lua:

notes on Lua and the ternary operator

Arithmetic and Logic

true and false

The literals for true and false.

falsehoods

Values which are false in conditional expressions.

tcl:

0 is false and all other numeric values are true. For non-numeric strings, "no" and "false" are false, and "yes" and "true" are true. The comparison is case insensitive. All other non-numeric strings raise an error when evaluated in a boolean context.

logical operators

Logical and, or, and not.

relational expressions

How to write a relational expression.

tcl

To evaluate a relational expression outside of the the conditional of an if statement, the expr command can be used.

Use square brackets to make an expression an argument of a command:

puts [expr $x>1]

relational operators

The available relational operators.

tcl:

The eq and ne operators always perform comparisons on their operators as strings. If the arguments are numeric, they are converted to a standard floating point representation.

% expr {"0" eq "00"}
0
% expr {"0" == "00"}
1

The == and != operators try to convert their arguments to a numeric form for the purpose of comparison. String comparison is used when no numeric conversion is possible:

% expr {"lorem" == "ipsum"}
0

The relational operators can be invoked as commands in the following manner:

% ::tcl::mathop::== 1 1
1
% ::tcl::mathop::!= 1 1
0
% ::tcl::mathop::> 1 1
%::tcl::mathop::< 1 1
0
% ::tcl::mathop::>= 1 1
1
% ::tcl::mathop::<= 1 1
1
% ::tcl::mathop::eq "lorem" "ipsum"
0
%::tcl::mathop::ne "lorem" "ipsum"
1

min and max

How to find the min and max value in mix of values or variables.

arithmetic expression

How to evaluate an arithmetic expression.

tcl:

Arithmetic expressions are normally evaluated with the expr command. However, the conditional argument of an if statement is always evaluated as an expression.

arithmetic operators

The binary arithmetic operators.

The operators for addition, subtraction, multiplication, float division, integer division, modulus, and exponentiation. Some languages provide a function pow instead of an operator for exponentiation.

tcl:

Arithmetic operators are normally used as arguments to expr, but commands also exist for each of them:

::tcl::mathop::+
::tcl::mathop::-
::tcl::mathop::*
::tcl::mathop::/
::tcl::mathop::**
::tcl::mathop::%

integer division

How to perform integer division.

lua:

All Lua numbers are floating point.

integer division by zero

What happens when a float is divided by zero.

lua

As already noted there are no literals for inf and nan. Once could assign the correct values
to variables with the following code

inf = 1 / 0
nan = 0 / 0

inf can be compared to itself, but nan cannot. In other words:

# this prints 'true'
=(1 / 0) == (1 / 0)
# this prints 'false'
=(0 / 0) == (0 / 0)

float division

How to perform floating point division, even if the operands might be integers.

float division by zero

The result of dividing a float by zero.

power

How to compute exponentiation.

The examples calculate 2 to the 32 which is the number of distinct values that can be stored in 32 bits.

sqrt

How to get the square root of a number.

sqrt -1

The result of taking the square root of -1.

transcendental functions

Functions for computing the natural exponent, natural logarithm, sine, cosine, tangent, arcsine, arccosine, arctangent, and atan2.

The trigonometric functions are all in radians. atan2 takes two arguments which are the x and y co-ordinates of a vector in the Cartesian plane. It returns
the angle to the positive x-axis made by the vector.

float truncation

Ways to convert a float to a nearby integer: towards zero; to the nearest integer; towards positive infinity; towards negative infinity.

absolute value

How to get the absolute value of a number.

integer overflow

What happens when an operation yields an integer larger than the largest representable value.

float overflow

What happens when an operation yields a float larger than the largest representable value.

random number

The examples show how to generate a uniform random integer in the range from 0 to 99, inclusive; how to generate a uniform float in the range 0.0 to 1.0; how to generate a float from a standard normal distribution

random seed

How to set the seed for the random number generator.

Setting the seed to a fixed number can be used for repeatable results.

bit operators

The available bit operators.

Strings

string literal

The syntax for a string literal.

newline in literal

Are newlines permitted in a string literal?

lua:

One can avoid backslashes by using the double bracket string notation. The following are equivalent:

s = "one\
two\
three"

s = [[one
two
three]]

character escapes

Backslash escape sequences that can be used in a string literal.

variable interpolation

The syntax for interpolating variables into a string literal.

string concatenation

The string concatenation operator.

split

tcl:

split takes an optional 2nd argument which is a string containing the characters to split on. split can only split on characters, not strings or regular expressions. For each pair of adjacent splitting characters in the input string, there will be an empty string in the result list.

lua:

how to split a string in Lua

join

How to concatenate the elements of an array into a string with a separator.

sprintf

How to create a string using a printf style format.

case manipulation

strip

pad on right, pad on left, center

How to pad a string to a given length on the right; how to pad on the left; how to center a string inside a larger string of a given length.

string to number

How to convert a string to a number.

tcl:

All values are strings. The expr function will concatenate the arguments together and then evaluate the string as a numerical expression. If all the numbers are integers, the expression is computed using integer arithmetic. Otherwise floating point arithmetic is used. The variables can contain compound arithmetic expressions; the following script outputs 10:

set a "7+3"
puts [expr $a]

lua:

Arithmetic operators will attempt to convert strings to numbers; if the string contains non-numeric data an error results. Note that relational operators do not perform conversion. Thus the following expression is false:

 0 == '0'

javascript:

Numbers can be explicitly converted to strings with toString(). Numeric literals must be put inside parens to invoke the toString() method.

(8).toString()
x = 7
x.toString()

number to string

How to convert a number to a string.

lua:

print and the .. operator will convert all types to strings.

javascript:

The plus operator + performs concatenation on strings. If either operand is a string, then the other operand will be converted to a string.

100 == "100"

Numbers can be explicitly converted to strings with toString(). Numeric literals must be put inside parens to invoke the toString() method.

(8).toString()
x = 7
x.toString()

length

How to get the number of characters in a string.

index substring

How to get the index of the leftmost occurrence of a substring.

extract substring

How to extract a substring.

chr and ord

converting characters to ASCII codes and back

Regular Expressions

character class abbreviations and anchors

The supported character class abbreviations and anchors

abbrev description
. any character; doesn't match newline when -linestop in effect
^ beginning of string; beginning of line when -lineanchor in effect
$ end of string; end of line when -lineanchor in effect
\A beginning of string
%a letter
\b, \y word boundary
\B, \Y not a word boundary
%c control character
\d, %d digit [0-9]
\D not a digit [^0-9]
%l lowercase letter
\m beginning of a word
\M end of a word
%p punctuation character
\s white space
\S not white space
%u uppercase letter
\w, %w alphanumeric character. \w also matches underscore
\W not a word character
\Z end of string
%z the null character (ASCII zero)

match test

How to test whether a regular expression matches a string.

case insensitive match test

How to test whether a regular expression matches a string, ignoring case.

modifiers

Available flags for modifying regular expression behavior.

tcl:

modifier description
-all causes regexp to return number of matches in string; causes regsub to replace all occurrences of match
-expanded ignore whitespace in patten
-indices modifies group capture: returns start and end indices of the substring instead of the substring itself
-inline return the total match and each of the group captures as a list. Normally the number of matches is returned
-line same as using -lineanchor and -linestop
-lineanchor causes ^ and $ to match the beginning and ending of lines (\A and \Z still match beginning and ending of string)
-linestop causes . to not matcch a newline
-nocase makes matching case insensitive

javascript:

modifier description
g when used with match, causes all occurrences of pattern in string to be return in an array; when used with replace causes all occurrences to be replaced
i makes matching case insensitive
m causes ^ and $ to match beginning and ending of lines instead of the whole string

substitution

How to replace all occurrences of a pattern in a string with a substitution.

tcl:

To replace only the first occurrence of the pattern, omit the -all flag:

set s "do re mi mi mi"
regsub "mi" $s "ma"

lua:

To specify the maximum number of pattern occurrences to be replaced, provide a fourth argument:

s = "do re mi mi mi"
s = string.gsub(s, "mi", "ma", 1)

javascript:

If the g modifier is not used, only the first occurrence of the pattern will be replaced.

s = "do re mi mi mi";
s.replace(/mi/, "ma");

group capture

Date and Time

current date/time

How to get the current time.

to unix epoch, from unix epoch

How to convert a date/time object to the Unix epoch; how to convert a date/time object from the Unix epoch.

The Unix epoch is the number of seconds since January 1, 1970 UTC. In the case of Tcl and Lua, the native date/time object is the Unix epoch, so no conversion is needed.

strftime

How to use a strftime style format string to convert a date/time object to a string representation.

strftime is from the C standard library. The Unix date command uses the same style of format.

strptime

How to parse a string into a date/time object using a strftime style format string. strptime is from the C standard library.

parse date w/o format

How to parse a string into a date/time object without using a format string.

get date parts

How to get the year as a four digit integer; how to get the month as an integer from 1 to 12; how to get the day of the month as an integer from 1 to 31.

get time parts

How to get the hour as an integer from 0 to 23; how to get the minute as an integer from 0 to 59; how to get the second as an integer from 0 to 59.

build date/time from parts

How to assemble a date/time object from the 4 digit year, month (1-12), day (1-31), hour (0-23), minute (0-59), and second (0-59).

sleep

How to sleep for half a second.

Arrays

The names given by the languages for their basic container types:

tcl lua javascript io
array list table Array List
dictionary array, dict table Object Map

tcl:

Tcl arrays are associative arrays. The dict type is new in Tcl 8.5. See the documentation for array and dict.

lua:

Note that Lua uses the same data structure for arrays and dictionaries.

literal

Array literal syntax.

size

How to get the number of elements in an array.

lua:

The # operator returns the index of a non-nil value in the array that is followed by a nil value. Unfortunately different Lua implementations behave differently when there are multiple such indices. In particular there is no guarantee that the index will be the index of the last non-nil value in the array.

# a returns the number of times that a for loop used with ipairs will execute.

lookup

How to access a value in an array by index.

tcl:

Does not support negative indices. To change the 2nd element in the list nums to 8, use

lset nums 1 8

lset will not increase the size of the list if the index is out of bounds. For more list manipulation commands see http://www.tcl.tk/man/tcl/tutorial/Tcl15.html.

lua:

Lua arrays are indexed from one.

slice

How to slice a subarray from an array.

concatenation

How to concatenate two arrays.

manipulate back of array

How to push or pop elements from the back an array. The return value of the pop command is the value of the item which is removed.

With these commands an array can be used as a stack.

manipulate front of array

How to shift or unshift elements to the front of an array. The return value of the shift command is the value of the item which is removed.

With these commands an array can be used as a stack. When combined with the commands from the previous section the array can be used as a queue.

iteration

How to iterate through the elements of an array.

sort

How to sort an array.

io:

sort does not modify the receiver. sortInPlace modifies the receiver:

a := list(3,1,4,2)
a sortInPlace

reverse

How to reverse the order of the elements in an array.

io:

reverse does not modify the receiver. reverseInPlace modifies the receiver:

a := list(1,2,3)
a reverseInPlace

member, non a member

How to test whether a value is an element of an array; how to test whether a value is not an element of an array.

intersection

How to compute an intersection.

union

How to compute the union of two arrays.

set difference

How to find the elements of an array that are not in another array.

map

How to apply a function to the elements of an array.

None of the examples modify the source array.

lua:

map implementation

javascript:

Array.map() passes three values to the callback function on each iteration: the value in the array, the index of the value in the array, and the entire array.

filter

How to select the elements of an array for which a predicate is true.

None of the examples modify the source array.

lua:

filter implementation

reduce

How to reduce an array using a binary operation and an initial value.

None of the examples modify the source array.

lua:

reduce implementation

min and max element

How to find the largest or the smallest element in an array.

Dictionaries

tcl:

The dict type was introduced in Tcl 8.5.

In earlier versions of Tcl associative arrays were used when a dictionary type was needed, and they remain an alternative in Tcl 8.5. Associative arrays are stored in variables as strings. The array command is used to treat the string as an associative array:

array set d [list "t" 1 "f" 0]
array size d
$d(t)
array set d [list "t" 2]
info exists d(t)
array unset d "t"
foreach {k v} [array get d] {
  puts k
  puts v
}

literal

The syntax for a dictionary literal.

size

How to get the number of entries in a dictionary.

lookup

How to look up the value associated with a key.

javascript:

JavaScript objects support both o.k and o['k'] style attribute access, but in the former case k must be a valid JavaScript identifier.

update

How to set or update the value associated with a key.

out of bounds behavior

What happens when a lookup is performed for a key the dictionary does not have.

is key present

How to find out whether a value is associated with a key.

delete

How to remove a key/value pair from a dictionary.

iteration

How to iterate through all the key/value pairs of a dictionary.

Functions

Python has both functions and methods. Ruby only has methods: functions defined at the top level are in fact methods on a special main object. Perl subroutines can be invoked with a function syntax or a method syntax.

function declaration

How to declare a function.

function invocation

How to invoke a function.

missing argument value

Value of an argument variable if a function is invoked with fewer arguments than are declared.

extra arguments

If a function is invoked with more arguments than are declared, how the function can access them.

default value

How to declare a default value for an argument.

variable number of arguments

How to write a function which accepts a variable number of argument.

lua:

In version 5.1 and earlier the global variable arg was set automatically. In version 5.2 one must explicitly assign to a local array.

It is possible to get the number of arguments that were provided without assigning to a local array:

function foo(...)
  print('passed ' .. select('#', ...) .. ' arguments')
end

return value

How the return value of a function is determined.

multiple return values

lua:

If a function returns multiple values, the first value can be selected by placing the invocation inside parens:

function roots(x)
  return math.sqrt(x), -math.sqrt(x)
end
(roots(4))

lambda declaration

How to define a lambda function.

lambda invocation

How to invoke a lambda function.

default scope

What scope do variables declared inside a function have by default.

tcl:

Tcl variables inside functions have local scope.

nested function visibility

Whether nested functions are visible outside of the function in which they are defined.

Execution Control

if

The if statement.

tcl:

Tcl also has a switch:

switch $a 0 { puts "no" } 1 { puts "yes" } 2 { puts "maybe" } default { puts "error" }

while

How to create a while loop.

break and continue

break exits a for or while loop immediately. continue goes to the next iteration of the loop.

for

How to create a C-style for loop.

lua:

Provide a third argument to set the step size to something other than the default of one:

for i = 0, 90, 10 do
  print(i)
end

raise exception

How to raise an exception.

catch exception

How to handle an exception.

finally/ensure

Clauses that are guaranteed to be executed even if an exception is thrown or caught.

uncaught exception behavior

System behavior if an exception goes uncaught. Most interpreters print the exception message to stderr and exit with a nonzero status.

tcl:

A tcl process can have multiple interpreters and multiple threads. However, each interpreter is limited to a single thread.

generator

How to create and use a generator.

File Handles

print to standard output

tcl:

To prevent puts from appending a newline to the output, use

puts -nonewline "hello"

read from standard input

How to read a line from standard input.

standard file handles

Constands for the standard file handles.

open file

How to create a file handle for reading.

open file for writing

How to create a file handle for writing.

io

If remove is not invoked, the file will not be truncated before writing. Part of the old contents will still be visible if they exceed the length of the new contents.

close file

How to close a file handle.

read line

iterate over a file by line

chomp

Remove a newline, carriage return, or carriage return newline pair from the end of a line if there is one.

read file

write to file

flush file handle

Files

file test, regular file test

copy file, remove file, rename file

set file permissions

temporary file

Directories

Processes and Environment

javascript:

Browser-embedded JavaScript cannot interact with the browser's operating system. All examples in this section assume the JavaScript code is being executed by node.js.

command line arguments

How to access the command line arguments.

environment variable

How to access an environment variable.

exit

How to terminate the process and set the status code.

set signal handler

How to register a signal handler.

external command

How to execute an external command.

tcl

When using tclsh as a shell or repl, external commands that do not have the same name as a built-in or user defined function can be executed directly without using the exec command.

backticks

How to execute an external command and read the standard output into a variable.

Libraries and Namespaces

library

What a library looks like.

import library

How to import a library.

library path

How to access the library path.

library path environment variable

The environment variable which governs the library path.

namespace declaration

How to declare a namespace.

namespace separator

The separator used in namespace names.

list installed packages, install a package

How to list the installed 3rd party packages; how to install a 3rd party package.

Objects

create blank object

How to create a blank object without any attributes or methods, or at least without any but the default attributes and methods.

set attribute

How to set an object attribute.

get attribute

How to get an object attribute.

define method

How to define a method for an object.

invoke method

How to invoke an object method.

clone

How to make a copy of an object. Changes to the copy will not affect the original.

object literal

The syntax for an object literal.

Reflection

class

has method?

message passing

eval

methods

attributes

Tcl

Tcl Tutorial
Tcl Reference Manual
Tcl Standard Library

Tcl has some traits which will be familiar to anyone who has done Unix shell script programming:

(1) variables have a $ prefix except when values are assigned to them, in which case they are used unadorned:

set v 42
puts $v

(2) statements consist of a command and zero or more arguments. Commands which are not built-in functions or user defined functions are resolved by looking for an external command in the search path and running it.

(3) the values are always stored in variables as strings. Commands which expect numeric arguments perform an implicit conversion, so there isn't much practical consequence for the developer.

(4) in the absence of quoting ("", {}, []) the arguments of a command are parsed into words using whitespace; commas are not used to separate arguments.

(5) square brackets [] must be used to execute a function and use its return value as an argument. A combination of square brackets [] and the expr command must be used to evaluate an expression and use its value as an argument:

puts [format "%10s" "lorem"]
puts [expr 1 + 1]

Square brackets can be used to invoke an external command, but the value of the square brackets containing an external command is always the empty string: "".

Unlike shells, *, ?, ~ in variable names are not automatically expanded using the filesystem, but this behavior can be invoked with the glob command.

Lua

Lua 5.1 Reference Manual
Lua Tutorial Directory

The Lua REPL is actually a REL; that is, it doesn't print the value of statement. Furthermore expressions cannot in general be used in the position of a statement. An expression can be converted to a statement and printed by preceding it with an equals sign:

> =1 + 1
2
> ="lorem ipsum"
lorem ipsum

There is no built-in pretty printer for tables. All numbers are stored as double precision floats. The table type can be used as both an array and a dictionary.

JavaScript

JavaScript Guide
Core Global Objects
Eloquent JavaScript
ECMA 262 5th Edition (pdf) 2009
jQuery
DOM 3 Core
DOM 3 Events

JavaScript in HTML

JavaScript is embedded in an HTML document using the <script> tag. The browser interprets the contents of the tag as JavaScript:

<script>
  var sum = 1 + 2;
  alert('the sum is ' + sum);
</script>

Alternatively the JavaScript can be in an external file named by the src attribute of the <script> tag:

<script src="foo.js"></script>

<script> tags can be placed in either the <head> or the <body> of an <html> document. They are executed as they are encountered by the browser. If there is a syntax error, then none of the JavaScript in the <script> tag will be executed. If there is an unhandled exception the browser stops execution of the <script> at that point. Neither syntax errors nor unhandled exceptions prevent the browser from executing JavaScript in subsequent <script> tags.

If placed in the <body>, a call to document.write() will insert HTML text into the location of document at the point of the <script> tag.

all javascript loaded event

  1. as the value of a DOM event attribute. The JavaScript is executed when the user performs the action that triggers the event.
  2. as a URL with protocol javascript:. If the URL is the target of a link, the JavaScript is executed when the user clicks on the link. bookmarklet

sandboxing of JavaScript

http requests and DOM manipuation

jQuery

Browser User JavaScript

In Chrome and Safari it is still possible for the user to execute JavaScript in the address bar:

javascript:alert('Hello World!')

Command Line JavaScript

SpiderMonkey: Introduction to the JavaScript shell
Rhino Shell
Node.js

There are several options for installing JavaScript engines which can be run from the command line. When run outside of the browser in this manner there is DOM or browser and any code which expects to find a global document or window variable will fail.

SpiderMonkey is the JavaScript engine used by Firefox. It is implemented in C++. A SpiderMonkey shell can be installed which can be invoked from the command line as js. Rhino is another JavaScript engine which comes with a command line shell. It also can be invoked as js. Rhino is implemented in Java but otherwise differs little from SpiderMonkey.

JavaScript which is executing in a browser cannot interact with the operating system. The SpiderMonkey shell adds built-in functions for reading from stdin, reading from a file, running the JavaScript in a file, and writing to stdout.

Io

Io Programming Guide
Io Reference

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License