Tom's JavaScript Machine

[ Machine | Instructions | Examples | JavaScript Basics | Challenge | Hints | About ]

JavaScript Basics

[ Lexical Elements | Expressions | Declarations & Definitions | Statements | I/O | Extensions ]

In this section, we show only the very basics of JavaScript. For more details, see here.

Items on a (light) grey background are for more advanced use and not (so) relevant for the Challenge.

In the column How, we show how to express something in JavaScript. Text in italics needs to be replaced by appropriate code fragments.

Lexical Elements
(See JavaScript coding conventions for good advice on how to format a program text.)
WhatHowRemarks
Comments
/* comment_text */
// comment text until line end
Comment text between /* */
can span multiple lines.
Is used to explain code fragments, or
to disable code fragments temporarily
Numeric Literals
0   1   3.14   -1.41421
String Literals
'characters-without-single-quote'
Can contain only printable characters,
and no single quotes.
N.B. String values can contain any character.
Identifiers (Names)
n   pi   input   a4
Used to refer to variables and functions.
Do not start with a digit.
Cannot be a keyword.
Are case sensitive.
Keywords
var
if   else   while
function   return
N.B. There are some more keywords,
but we do not treat them here.
Operators
+   -   *   /   ==   !=   =
See Expressions and Statements
for their use and meaning.
Punctuators
(   )   ,   {   }   ;
Expressions
WhatHowRemarks
Grouping (Nesting)
( expr )
To force grouping, overruling the default grouping
Value of a name
name
Of a variable
Equality test
expr == expr
Boolean expression, valid or not
N.B. Do not confuse with assignment =
Inquality test
expr != expr
Boolean expression, valid or not
Arithmetic operations
on numbers
num_expr + num_expr
num_expr - num_expr
- num_expr
num_expr * num_expr
num_expr / num_expr
Addition
Subtraction
Negation
Multiplication
Division
String operations
str_expr + str_expr
str_expr.length

str_expr.charAt(num_expr)

str_expr.substr(num_expr, num_expr)
str_expr.substr(num_expr)
String concatenation
String length

Character indexing starts at 0
s.substr(i, n) returns n characters of s from index i;
thus, s.charAt(i) == s.substr(i, 1)
s.substr(i) returns all characters of s from index i on
Function call
func_name(argument_list)
An argument list is a possibly empty
comma-separated list of expressions.
The values of these expressions are bound to
the corresponding parameter names, and
the statement list of the function is executed.
The value is that of the first return expression
encountered during execution.
Declarations and Definitions
WhatHowRemarks
Variable declaration
var name;
var name = expression;
Without expression, the initial value is undefined.
Function definition
function name(parameter_list) {
 declarations_and_definitions
 statement_list
}
A parameter list is a possibly empty
comma-separated list of distinct names.
In a function call, these names obtain
the arguments as initial value.
Also see the return statement.
Statements
WhatHowRemarks
Assignment
var_name = expression;
Expression value becomes new variable value.
Note the trailing semicolon (;)
Conditional Execution
if ( bool_expr ) {
  statement_list
}
else if ( bool_expr ) {
  statement_list
}
else {
  statement_list
}
The boolean expressions are evaluated
in order until a valid one is encountered;
the corresponding statement list is executed.

The else and else-if parts are optional.
There can be multiple else-if sections.
Conditional Repetition
while ( bool_expr ) {
  statement_list
}
The boolean expression is first evaluated.
The repetition terminates when it is not valid.
If it is valid, then the statement list is executed,
and the process is repeated.
Return from function
return expression;
return;
Without expression, the function value is undefined
(useful when the function is to be called as statement).
Function call
func_name(argument_list);
An argument list is a possibly empty
comma-separated list of expressions.
The values of these expressions are bound to
the corresponding parameter names, and
the statement list of the function is executed.
Execution terminates at the end of the statement list,
or at the first return encountered;
the return value is ignored.
Interactive Input and Output
(useful for locating errors)
WhatHowRemarks
Output window
alert(s);
Shows string s in a popup window
Confirmation window
confirm(s);
Shows string s in a popup window,
and lets the user Cancel or OK,
returning the boolean choice as function value.
Used in conjunction with if or while statement.
Input window
prompt(s);
Shows string s in a popup window,
and lets the user type in a string,
that is returned as function value
Extensions for Tom's JavaScript Machine
(See Operating Instructions for details.)
WhatHowRemarks
Process input
input
readStr()
readNum()
moreInputs()
separator
inputs
nextInputIndex
Produce output
write()
writeln()
writequ()
output
Functions
isQuote(c)
isNewline(c)
Constants
QU
NL

Valid HTML 4.01 Transitional

©2009-2010, Tom Verhoeff (TU/e)
Feedback about this page is welcome