Scripter Javascript Tutorial
javascriptmusiclogicscripterbookexcerpttutorial61 The if...else
statement
MDN: Control Flow and Error Handling: if...else statement
The if...else
statement accommodates handling code to
execute when a condition is either true or false. The syntax is
very similar:
if ( <condition is true> ) {
<do something>
else {
} <do something else>
}
In the above example:
Lines 1–2: These are exactly the same as the
if
statement.Line 3: The first curly brace closes the true code block and the false code block is opened with the
else
keyword and the curly brace.Line 4: The code to be executed when the condition is false.
Line 5: The
false
code block, and the entireif...else
statement, is closed with the curly brace.
To expand upon the example in HandleMIDI()
, because
there are many types of events:
function HandleMIDI(event) {
if (event instanceof NoteOn) {
Trace("NoteOn");
else {
} Trace(event);
} }
In the above example, the following is happening:
Line 1: Scripter calls the
HandleMIDI()
function, passing anevent
.Line 2: The
if
statement evaluates whether the event is an instance ofNoteOn
.Line 3: If the event is
NoteOn
, then the event is written to the console.Line 4: The
if
statement is closed with the curly brace.
For a simple track with one note, the following is output to the console:
***Creating a new MIDI engine with script***
Evaluating MIDI-processing script...
Script evaluated successfully!
NoteOn
[NoteOff channel:1 pitch:61 [C#3] velocity:64]
[ControlChange channel:1 number:120 [All Sound Off] value:0]
>
In the output, note the following:
Line 5: The
NoteOn
event is captured per the condition eventinstanceof NoteOn
and all that is output is the string “NoteOn
”.Lines 6–7: Neither event is a
NoteOn
, so the script outputs the events themselves to the console.