Variables, Strings, Numbers, and Operators
Learn how JavaScript works with numbers and arithmetic by calculating totals, then transform it into a POS-style calculator web page.
The project
Let’s revisit POS systems. When you shop at a store and check out at the counter, the cashier uses a machine that prints a receipt. Behind that machine is software written with code that handles the transaction and generates the receipt. Here’s what a sample receipt looks like.
You have been hired to create this point-of-sale system! You can see the sample receipt.
You know how to print stuff on the screen, but don’t want to hardcode everything. You want a flexible enough system such that if the price of an item changes, you don’t have to change the code manually at various places. The same is the case for the quantity of items.
The programming concept
Today’s main programming concepts are variables and arithmetic operators. Variables can hold many types of information:
Numbers (Like prices or quantities)
Strings (Like
"Milk"
or"Cashier Name"
)Booleans (True/False values for conditions)
Operators tell the computer what kind of math or text operation to perform:
Numeric operators:
+
,-
,*
,/
,%
String operators: Using
+
to join words or values (called ...