Bill's mementos - Memento #1 - Introduction & 1st article - JavaScript expressions & fields

Skip to first unread message

Bill Crews

Jan 31, 2023, 10:56:33 PMJan 31
to mementodatabase

Bm 1 Bill’s Jan 2023 memento — Introduction to Bill’s mementos

I hope you’re having a happy 2023 so far! This is the first edition of my mementos — newsletters for the Memento forum about our use of Memento. They'll be published in my native American English as Memento Forum posts on a periodic basis (maybe every month or two). After a short introduction, one or more articles on the use of Memento will appear. Some of it will be topical commentary, but most will be articles intended to be informative & helpful to users who are trying to get the most out of Memento as they use it to do anything from making shopping lists to building database applications.

Sample articles being considered

The kinds of articles I’ve planned to write in my mementos…

  • Getting started with your first library, then just a bit more (or not), then a bit more, …

  • Getting started using JavaScript to automate your activity within Memento

  • Tutorials & how-to articles on things like getting started or date arithmetic in JavaScript

  • Topics for discussion, eliciting contribution via replies to whatever contribution I start with

  • Growing a simple Memento library into a database with libraries linked together & maybe scripted to turn it into a database application. (My general approach: Library + links = database (⇒template file) + scripts = application (⇒catalog template) ).

  • How to design a database of linked libraries

  • How to re-use your JavaScript code & share it across your devices

I’m extremely open to recommendations for future topics! Let me know what our’re interested in seeing by sending me a private email or post back to the thread -- whichever seems more appropriate.

Bill’s mementos (Bm) are intended to be kept as mementos for future reference, as they contain essential information about Memento & its use. To this end, I am setting up the Subject line format, article titles, and hashtags (like #Tutorial) in a standard way so forum members can readily search, find things, and make use of this information as time moves on. Also, the mementos will be archived in the Memento Wiki. See the first section in the Navigation Index in the last column.

Beginning Memento JavaScript

Chapter 1: Expressions & JavaScript fields

The value of a JavaScript field in Memento is the result of the final evaluated expression of the script.

This article assumes you know no JavaScript.


A literal is something you type that represents a value. The most prominent two kinds of literals are numbers and strings (strings of characters that make up text).

12 is a number, and "12" is a string. I almost always use double-quotes to designate a string, but so long as they are a matched pair, you could use single-quotes instead, like '12'. "Bill's mementos" is a string, and the single-quote or apostrophe inside works like any other character. There's nothing special about it, since it's protected by being inside a string.

A literal is the most fundamental expression. There is no calculation or other operation occurring – the value of the expression is merely the value of the literal.

Variables, keywords, and statements

A variable is a simple holder for a value, as we did in high school algebra, often with variables like x and y. In JavaScript, every character is a distinct character, so x and X are different names for variables, as are cafe and café also different.

Variable names must start with a letter, underscore, or dollar sign ($), and can contain alphabetic, numeric, or underscore characters after that. By convention, by default, variable names start with a word in lowercase and are followed by any remaining words needed to clearly identify the variable. Each of these following words begin with a capital letter. For example, a is a conventional variable name, as are aLamb and aLittleLamb -- 3 distinct variables, each with its own value. Because a variable is a holder for a value, that value is its value, so variables can be used in expressions just as literals can.


Scripts consist of a sequence of statements.

A keyword is one of a number of "words" (tokens) that are considered as special by JavaScript and define the beginning of a statement. Statements without a keyword are expressions and yet are still called statements. Our first variable is x, which will hold the value 12 once this statement is executed…

var x = 12;  // 2 slashes like this make the rest of the line just commentary.


// Comments are ignored by JavaScript.

// The statement above defines a variable x and sets its value to 12.

// The remainder of that line and the lines of this paragraph have no effect on the execution of a JavaScript script.

A second way of including a comment in a script is to begin the comment with "/*" and end it with */". For example…

var y = 34;  /* This kind of content may begin and end on one line

or continue across multiple lines. */

JavaScript will continue examining following the "*/" for JavaScript statements.

Variable definition

The var statement causes a variable to be defined and a name assigned to it. Optionally, an initial value can also be assigned to it. A variable is similar to a variable in algebra.

Note: The var keyword works on all Memento platforms, so that's the keyword I always use for variables in Memento scripts. Any script in Bm articles will work on any Memento platform. On the Web, you may see let and/or const used in place of var. They do not work in Memento on Android devices and may not work on iOS devices due to a limitation in Memento in the mobile edition.

Statement termination

In JavaScript, a statement can be spread across several lines, and several statements can share one line. JavaScript uses a semicolon as a statement terminator. So you could code a line like this…

var x = 12; var y = 23; var z = 34;

If you don't put multiple statements on a line, most JavaScript interpreters don't require termination, so you're likely safe omitting the terminator, but caution should be taken. Code seen on the Web and in development organizations almost always include the statement terminator. If you want your code to run on all platforms under all JavaScript interpreters, it is best to provide the semicolon to make sure the interpreter doesn't misunderstand you. I'll use them so you'll get used to where they belong.

/* Another statement */

var x

    = 12; // A line break is treated just like a space.

The literal 12 depicts a number -- a dozen. The literal "12" depicts a 2-character string -- a "1" followed by a "2". It looks the same when displayed on the screen, but it's difficult to reliably do numeric calculations with a string. One reason why is that, during evaluation, operators will very likely automatically convert a number to a string before the operation takes place, and the result may not be what you intended. More on this below.


An operator is a symbol that represents an operation that is to be executed. We've already used the = operator in the var statements above. What it does might surprise you. It sets the value of the operand (variable) on its left to the value of the expression that is on its right (the right operand). It's not used for comparisons. I'll discuss comparisons starting in Chapter 2. The other most common operators for calculation are + to add, - to subtract, * to multiply, and / to divide. There are many more, but these will do for now. These are called binary operators because they operate on an operand on its left and another on its right. The result is a value. For instance…

var x = 12; var y = 23; var z = x + y;  // Afterwards, z is 35

The + operator can be used on strings, as well, but it works very differently…

var first = "Bill"; var last = "Crews"; var z = first + last;  // z is "BillCrews"

Woops! There's no space between them! To handle that, we could say…

var z = first + " " + last;   // z is now "Bill Crews", or we could say…

var z = last + ", " + first;  // z is now "Crews, Bill"

If either operand of the + operator is a string, JavaScript makes sure that both operands are made into strings before the expression is evaluated, thus forcing a string concatenation to take place. As is the case with many things in this article, there are ways around things like this that involve features we haven't covered so far, so I'll leave it at that for now. To make x (still 12) display as dollars, use "$" + x to get "$12". It'll look OK, but because that's a string, don't try to add more dollars to it. You won't get what you want.

If you want to add something to a variable or otherwise perform an operation on a variable, add an equal symbol (=) to the operator. This works for the 4 operators we've discussed so far and for many others you'll learn about later. For example…

x +=1;  // Add 1 to x. If x was 1, it is now 2

x -= y;  // Subtract y from x. If x was 2 and y was 1, x will now be 1 again

JavaScript performs operations in an order of its own determination, so it's best to be explicit about what you want done and when to do it. Use parentheses () to group subexpressions. For instance…

x = (y + 1) * (z - 2);  // If y was 3 and z was 4, x will now be 8

If we had omitted the parentheses, the result might not be what you intended…

x = y+1 * z-2;  // JavaScript will calculate 5 for this, multiplying first!

JavaScript fields

JavaScript fields in Memento take the value of the final expression evaluated in the script. We say that the field returns this value. Therefore, frequently, the final (or maybe the only) statement in a JavaScript field is just the expression itself and not a typical, full statement. For example, to multiply the unitPrice field by the quantity field to get the value of the total price for a line item, define a JavaScript field called lineItemPrice and use the following script…

field("unitPrice") * field("quantity");  // You need no variables for this.

So far, we've studied 2 kinds of statements: a declaration statement (var) and an expression statement. Expressions are often parts of larger statements rather than independent statements of their own.

In the Memento Online Catalog under Education, you will find a template I created for you. It allows you to test various expressions to see what value JavaScript comes up with. There are 2 ways to use it. The main way is to add an entry in the Expression field, tap the checkmark to save the entry, and its value will be displayed in the Value field on the right. If you want to quickly determine JavaScript’s evaluation of an expression without creating an entry, then from the library entries list screen, just tap or click the @ icon, and then enter your Expression. Closely watch the bottom of the screen as an action script displays the Expression and its Value momentarily. The result will fit into as much as 2 lines of output. This method will preserve evaluations for future lookup.

To install the library, download the template from the Online Catalog and add your library based on the template BmExpressions.mlt2.

Stay tuned for future mementos, including Chapter 2 of Beginning Memento JavaScript.


Feb 1, 2023, 2:55:56 AMFeb 1
to mementodatabase
Thank you so much, Bill !!
Reply all
Reply to author
0 new messages