Skip to content
engineering workbench.by StudyPort Academy
Learning path
Foundations15 minLESSON 11 / 34

Strings & numeric operations

01

Understand the idea

A display rounds a price correctly, but the next addition joins digits as text. Formatting changed the type of the value.

One boundary between values and display

A price needs arithmetic before it needs a currency label. Mixing those steps can turn a later addition into string joining. Separating calculation from display also makes it easier to test the number independently of how it looks on screen.

Strings produce new strings

Strings are sequences. such as trim, slice, and toUpperCase return strings without changing the original. Indexes describe positions, and slice's end position is excluded. For user-facing text, remember that a visible symbol can occupy more than one JavaScript string code unit.

A string is text in quotes. A template literal uses backticks and can insert the result of an between a dollar sign and braces. String methods such as trim return a new string; they do not edit the original text in place.

Calculation, text, and display operations
OperationReturnsUseful for
String.trim()A new stringRemoving whitespace at the ends
String.includes(text)A booleanChecking whether text occurs
number.toFixed(2)A stringDisplaying a chosen number of decimal places
Math.round / floor / ceilA numberNearest integer / down / up
Math.random()A number from 0 inclusive to 1 exclusiveCasual randomness, not security
Intl.NumberFormatLocale-aware display textFormatting a number for the reader’s locale

Numbers have precision limits

Template literals insert expression results into text. They are useful for a final label, but that label is not suitable for arithmetic. Keep the underlying numeric value separately instead of trying to recover it from a currency label later.

Rounding and randomness

Math.floor rounds downward, Math.ceil upward, and Math.round to a nearest integer according to its tie rule. toFixed produces a formatted string. JavaScript numbers use binary floating point, so some decimal fractions cannot be represented exactly. For ordinary currency calculations, integer minor units help avoid accumulating those rounding surprises.

Build the final label

Math.random produces a value from zero inclusive to one exclusive. Scaling and flooring can select an index; rounding can bias the endpoints. It is useful for a learning game, but not for security-sensitive tokens.

The example stores 250 cents instead of starting with the decimal 2.50. It calculates the total, converts cents to the display unit, and formats two decimal places at the end. Integers are exact within the safe-integer range; fractional rates and rounding policies still need deliberate handling in a real money calculation.

02

Mental model

FOLLOW THE VALUE

250 cents × 3
numeric calculation
750 cents

Calculate before formatting.

01 / 03Trace the transformation

03

Live example

The total remains a number in cents. Only the final label formats it as dollars with two decimal places.

Opening editor…
LIVE PREVIEW
Console 0

Your console output appears here.

Saved on this deviceJavaScript · ES2022
04

Guided challenge

Normalize the display name

Remove surrounding whitespace before building the label. Keep all arithmetic in numeric cents.

Opening editor…
LIVE PREVIEW
Console 0

Your console output appears here.

Saved on this deviceJavaScript · ES2022

EXPECTED CONSOLE OUTPUT

Ada: $7.50
number

Hints 0/2

    Use all hints or make 3 failed runs (0/3).

    05

    Free sandbox

    Make a variation, test an assumption, or break the example on purpose. This draft is saved separately.

    Opening editor…
    LIVE PREVIEW
    Console 0

    Your console output appears here.

    Saved on this deviceJavaScript · ES2022
    06

    Quiz

    A quick check for understanding. Retry as often as you like; your learning path stays open.

    01What type does toFixed return?
    02Does trim modify the original string?

    Ready to call this one understood?

    Mark this lesson complete to earn 100 XP. You can always revisit it.

    Sign in to save progress
    Next lesson