Run your first Node.js script
A Node script is a text file containing JavaScript. To execute it, save the file and pass its name to the node command. The first example calculates an order total using integer cents so the arithmetic is exact for these small values.
Local practice: The proposed teaching runtime is Node.js 26.13.1 LTS. These original offline examples were executed on Node.js 22.16.0 (Linux x64); execution on the proposed baseline and in ByteMind is not yet verified. No npm dependencies are required. Use a fresh practice directory. Code blocks are static, not a browser-based Node runner.
Calculate an order total
const quantity = 3;const unitPriceCents = 425;const totalCents = quantity * unitPriceCents;console.log(totalCents);In the directory containing the files above, run:
node order.mjsExpected standard output:
1275Expected exit status: 0.
How it works
const creates a binding that cannot be reassigned. quantity * unitPriceCents is a multiplication expression, and its result is assigned to totalCents. console.log() sends an instructional result to standard output, the normal output stream of a process. Node reads the file and executes its statements. An .mjs extension explicitly selects ECMAScript module mode; the module system will be explored in chapter 5. The code here needs no imports. When no work remains to keep the runtime active, this simple script exits.
Apply a fixed discount
const subtotalCents = 1800;const discountCents = 200;console.log(subtotalCents - discountCents);In the directory containing the files above, run:
node discount.mjsExpected standard output:
1600Expected exit status: 0.
Common mistake
Save plain text, not a rich-text document or a file called order.mjs.txt. Keep money units consistent: multiplying a price in cents produces cents, not dollars. This small integer example does not address taxes, rounding or arbitrary currency precision.
Try it yourself
Write a script for four notebooks priced at 275 cents each. Predict the output, then run it. Add a fixed 100-cent discount and confirm the new output is 1000 cents. Explain why the two outputs differ.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Run your first Node.js script — knowledge check
Check your understanding of run your first node.js script.
What does `node order.mjs` do?
What is printed by quantity 4 and unit price 275 cents?
What unit does the example produce?