A string is text. Single and double quotes both work; the closing quote must match the opening one. Backticks (`, rather than a single quote) create a template literal. Put an expression between a dollar sign and braces, as in ${minutes}, to insert its value into the text. The backticks and ${...} are punctuation you type; they do not appear in the resulting message. console.log displays text without its surrounding quotes.
The same symbol can behave differently with different types of value. "20" + 5 joins text into "205"; 20 + 5 adds numbers. The operator + performs concatenation if either operand is a string. Avoid relying on JavaScript to guess which operation you intended.
Number("20") converts numeric text into a number. String(20) converts the other way. typeof tells you a value’s type. Conversion is not validation: Number("hello") produces NaN (not a number), and Number("") produces zero. On Day 5 we will reject empty and invalid input deliberately.
A method is an operation accessed through a value with a dot. name.trim() returns text without surrounding whitespace; it does not change the original string. For now, treat methods as tools with a named job.
Predict before running
Which line joins text, and which adds numbers? Change the input to "30" and predict both again.
const input = "20";
console.log(input + 5);
console.log(Number(input) + 5);
const topic = "JavaScript";
console.log(`Today: ${topic}`);Check your prediction
205
25
Today: JavaScriptTrace each statement in order. If your prediction differed, describe the first place your mental model and the result diverged before trying again.
Calculate hours and format a number
JavaScript sometimes stores decimal calculations with a tiny rounding difference. For example, adding 0.1 and 0.2 may show more digits than you expected. You do not need to understand the storage details to use today’s examples. toFixed(2) formats a number to two decimal places and returns a string. Use it for display; it does not make later arithmetic exact.
The remainder operator % finds what is left after division. It is useful for turning 95 minutes into one hour and 35 minutes. Math.floor rounds downward to a whole number. These tools are operations provided by JavaScript, not extra variables you need to declare.
const minutes = 95;
const hours = Math.floor(minutes / 60);
const leftover = minutes % 60;
console.log(`${hours}h ${leftover}m`);
console.log((0.1 + 0.2).toFixed(2));
console.log(typeof (0.1 + 0.2).toFixed(2));Check your prediction
1h 35m
0.30
stringThe hour calculation and the remainder complement each other. Formatting gives readable text, so avoid adding another number to that text.
Inspect a surprising value
When output looks wrong, inspect both the value and its type. console.log(typeof minutesText) tells you whether you are holding text or a number. A numeric-looking string is still text until you convert it. Try Number("12cats"): the result is NaN because the whole text does not represent a number. On Day 5 we will write a function that checks input and reports when it is not allowed.
Modify: use 130 minutes in the example. Predict the hours and remainder before running. Then try 60 and 59: these boundary cases reveal an incorrect division or rounding choice quickly.
Follow the value through a conversion
A conversion creates a value; it does not rewrite the original text. Keep the raw input and the converted result in different variables when you are learning. That makes the point where text becomes a number visible. The same plus sign has two jobs: if a string participates, it can join text; with two numbers, it adds.
Read each expression from left to right. Parentheses make a numerical calculation happen before a later string join. A displayed result looking like a number is not evidence that its type is numeric. Ask what operation created it before using it in another calculation.
const entered = "12";
const amount = Number(entered);
console.log(entered + 3);
console.log(amount + 3);
console.log(entered);Check your prediction
123
15
12 The input is still “12”. Only amount holds the converted number. Reassigning entered is not required, and keeping the original is useful when reporting an input problem.
console.log("Total: " + 2 + 3);
console.log("Total: " + (2 + 3));
console.log(Number("not a number"));Check your prediction
Total: 23
Total: 5
NaN The first line becomes text before reaching the final 3. The second adds first. NaN means the numeric conversion failed; it is not a useful amount. Day 5 will add explicit validation rather than treating conversion as proof of valid input.
Try the rule on new inputs
Change the entered text to “0”, “2.5” and “two”. Predict which conversions can participate in arithmetic. Then produce a sentence containing the sum of two numeric values without accidentally joining their digits.
Your turn
Strings & Conversion: practise
Use name = " Ada " and minutesText = "25". Print exactly Ada studied for 30 minutes. after adding five minutes.
const name = " Ada ";
const minutesText = "25";
// Clean the name, convert the minutes, and build a message.Hint 1: the idea
Trim removes outside spaces. Convert before adding.
Hint 2: a first step
Create a numeric minutes value with Number(minutesText) + 5, then use a template literal.
Show one solution
const name = " Ada ";
const minutesText = "25";
const minutes = Number(minutesText) + 5;
console.log(`${name.trim()} studied for ${minutes} minutes.`);Ada studied for 30 minutes.The text is cleaned and the number converted before the message is assembled. Do not memorize the punctuation: explain which part transforms data and which part formats it.
What you should be able to explain
- Why does toFixed return something you should display rather than add to?
- Convert 130 minutes into hours and leftover minutes.
- Explain the difference between converting a value and checking whether it is allowed.
Completion is saved in this browser only. It does not measure mastery.