Syntax and Command Blocks
Knowing the basic syntax of any language that you choose to code in is as important as knowing how to shift gears in a car with a manual transmission. Understanding how to use a language's syntax is essential to programming in it. This section provides basic JavaScript syntax rules and command block examples to get you started programming quickly.
Syntax
Syntax in JavaScript differs from that of LotusScript or any other scripting language, but its structure is similar. The basic command line is a one-line expression followed by an optional semicolon:
document.write("The Slalom software is awesome!");
The previous command line invokes the write method of the document and closes with a semicolon. Even though the semicolon is optional, it's a good habit to pick up. It helps define the end of your command line and structures your code so that it clearer and easier to read. Multiple commands can also appear on one line as long as a semicolon is used to mark the end of the command:
document.write("Ice");document.write("Cream");
Command Blocks
Multiple lines of code can be placed inside command blocks using curly braces, { and } . Command blocks are used to group together large pieces of code into a single entity. This is useful when working with loops and defining functions.
A command block looks like this:
{ document.writeln("When is it a good time to visit Louise?"); document.writeln("Any time is a good time!"); }
Command blocks can also be embedded:
{ JavaScript code goes here { More JavaScript code goes here } }
Each time a command block is opened with an open curly brace ( { ), it must be closed with a closed curly brace ( } ). It is also common practice to indent each successive command block, as in the previous example. Extra spaces and tabs do not have any effect on the program when the code is run.
TIP
JavaScript is case sensitive!
JavaScript is also case sensitive when working with objects, properties, methods , keywords, operators, and variable names . Be watchful of your case use so that at code runtime you don't pull your hair out trying to figure out why your code isn't working. Here are some examples:
Document.writeLn("Can't use uppercase letters to call objects!"); document.WriteLN("Can't mix uppercase and lowercase to call methods either!"); document.writeln("This is better!");
Or:
var MyVar = "This variable name must match exactly when it's called."
myvar = "This won't match the var above when called."
MyVar = "Call the variable exactly as you declared it or stick to lowercase!"
Категории