Best Practices - JavaScript in Lucernex
In this article, we will discuss some best practices you should keep in mind while working with JavaScript.
Lucernex conforms to the Standard ECMA-262 Edition 5.1 language specification for JavaScript. Scripts added to Lucernex are restricted to the features and libraries defined in this specification. Lucernex JavaScript cannot access newer features or third-party libraries.
Do not use a word processing program, because they add additional styling and convert characters from their simple forms to their stylized forms. For example, apostrophes and quotes are converted to curly quotes. Curly quotes are different characters from straight quotes and may break your code.
You may also find it useful to use a text editor that provides syntax highlighting. This feature makes it easier to identify errors in syntax such as missing closing brackets, extra semicolons, and so forth.
Recommended text editors include Notepad ++ and Sublime Text.
The reason that we follow these recommendations is that they make your JavaScript more legible. You can find these recommendations in their article JavaScript Style Guide and Coding Conventions.
We also recommend that you use two or three spaces for line indentation.
The reason we check for null is because if we try to do something with a null object, the script will not execute correctly.
GOOD:
Copy
if (value != null && …) {
BAD:
Copy
if (… && value != null) {
The good example below is more efficient because the length property already exists, the system does not have to create an empty string, and the system does not have to compare two strings.
GOOD:
Copy
if (value != null && value.length > 0) {
BAD:
Copy
if (value != "") { // worse performance due to string literal creation
Double quotation marks should be used to enclose string literals or text values. The reason we recommend this best practice is because apostrophes occur more often in strings than double quotes. Also, when you use double quotes, you don't have to use an escape sequence for the one when the other is being used to delineate the string.
Properly formatted:
Copy
1
2
var string1 = "The dog's bark is scary.";
var string2 = 'Tom said "Why is he late?" and looked at Jim.';
Improperly formatted:
Copy
1
2
var string3 = 'The dog's bark is scary.';
var string4 = "Tom said "Why is he late?" and looked at Jim.";