JavaScript the Basics¶
Syntax Conventions¶
All programing languages have programing rules known as syntax that make up the language. The syntax includes specific characters that need to be placed appropriately in order for the program to interpret and execute the commands. The following items briefly discuss these conventions and how they should be used.
| Syntax Convention | Description |
|---|---|
| Case sensitivity | myvariable is not the same as myVariable. |
var |
All JavaScript variables and functions must be identified with unique names called identifiers. Identifiers can be short names like x and y or more descriptive like dateOfBirth or lastName.The var keyword is used to create a new variable. Variables are containers for storing data values. The rules for creating variable or function names:• Names must begin with a letter, $, or _.• Names can contain letters, digits, underscores, and dollar signs. • Names are case sensitive ( y and Y are different variables).• Reserved words (JavaScript keywords) cannot be used as names. Best Practice: Use camel case: combine multiple words starting with a lower case letter, each successive word starting with a capital letter. For example, "Date of Birth" becomes dateOfBirth; "Last Name" becomes lastName. Use descriptive abbreviations.var lastName; |
= |
The equal sign is used to assign values to variables.var lastName = 'Watts'; |
; |
All line statements must end with a semicolon (;). Block statements generally do not end with a semicolon. |
| Simple Statements | Simple statements perform one and only one function. In this example the statement sets the word Radiology into the MSH-3 field of an HL7 message.message.setNode('MSH-3', 'Radiology'); |
| Compound Statements | Compound statements are comprised of multiple function calls. This example first changes radiology to uppercase and then sets it into MSH-3.message.setNode('MSH-3', StringUtils.upperCase('radiology')); |
| Block Statements | Block statements start and end with curly brackets and are widely used with if statements, for loops, and other JavaScript constructs. A block can contain multiple simple or compound statements.{ var sendingApp = StringUtils.upperCase('radiology'); message.setNode('MSH-3', sendingApp); } |
| White Space | JavaScript ignores extra spaces, allowing white space to be inserted to improve readability. However, trailing white space at the end of a line is flagged by the QIE JavaScript validation tool as line x col true. Trailing whitespace. Remove the extra spaces to clear the error. |
| Splitting code across multiple lines | If a line of code does not fit on one line, break it after an operator, a comma, or within a string. In the following block, the if statement is a compound statement of two functions, message.setNode and qie.callWebService. The qie.callWebService call is broken across 4 lines, split at the open parenthesis and after each comma.if (StringUtils.equalsIgnoreCase('CPS10', centricityVersion)) {message.setNode("/", qie.callWebService(channelCache.getValue('wsName'),"CCD Export",parameterMap));} |
' and " |
Single and double quotes are interchangeable within JavaScript, but must be matched within a given statement. A value that starts with a single quote must end with a single quote. Quotes typically surround textual values (strings and literals). |
+ |
The plus symbol has different meanings in JavaScript. With numerical values it performs arithmetic addition. With strings it concatenates the two values together.var string1 = "Nothing is impossible, \n\r";var string2 = "the word itself says I'm possible! \n\r";var string3 = "Audrey Hepburn";var quote = string1 + string2 + string3;The variable quote now holds the entire quote including carriage returns and punctuation:Nothing is impossible, the word itself says I'm possible! Audrey Hepburn |
// |
Single line comments use two forward slashes.//This is a single line commentvar x = 5; //this is an inline comment. |
/* */ |
Multi-line comments start with /* and end with */. All lines between the opening and closing comment characters are commented out./*var x = '5'; var y = '3'; var z = x + y;*/ |
Comparison Operators¶
The comparison operator is used to compare two values to see if they are equal. The following comparison operators are available in JavaScript:
| Operator | Description |
|---|---|
== |
Is equal to (value only). JavaScript converts both values to the same type automatically, then compares the two values.var string5 = '5'; var number5 = 5;(string5 == number5) equals true |
=== |
Is exactly equal to (value and type). JavaScript compares both the values and the object type; both must match.var string5 = '5'; var number5 = 5;(string5 === number5) equals false |
!= |
Is not equal (value only).var string5 = '5'; var number5 = 5;(string5 != number5) equals false |
!== |
Is not equal (neither value nor type).var string5 = '5'; var number5 = 5;(string5 !== number5) equals true |
> |
Is greater than.var number4 = 4; var number5 = 5;(number5 > number4) equals true |
< |
Is less than.var string4 = '4'; var number5 = 5;(number5 < string4) equals false |
>= |
Is greater than or equal to.var number5 = 5; var numberFive = 5;(number5 >= numberFive) equals true |
<= |
Is less than or equal to.var number4 = 4; var string5 = '5';(number4 <= string5) equals true |