Lesson 1.2Lesson 2 of 24
Strings, numbers and dates
Given a business requirement, utilize strings, numbers, and dates effectively.
Key points
- 1Strings never change: trim, toUpperCase, replace and padStart return a new string that must be stored.
- 2Numbers are binary floating point, so 0.1 + 0.2 is not exactly 0.3. Round with toFixed for display, which returns a string.
- 3Number needs the whole string to be numeric; parseInt and parseFloat read leading digits and stop at the first other character.
- 4Test for NaN with Number.isNaN, because NaN is not equal to anything, itself included.
- 5In new Date(year, month, day) months start at 0 and values out of range roll over. A date-only string such as 2024-03-10 is read as UTC midnight.
What the exam guide checks here
Objective wording from the exam guide (English).
- 1.2.K1String methods and template literals
- 1.2.K2Number behaviour: floating point, NaN, Number and Math methods, parseInt/parseFloat
- 1.2.K3Date: construction, months counted from 0, getters and setters, time zones
- 1.2.S1Pick the expression that formats, parses or compares strings, numbers or dates as required
Lock it in
Answer scenario questions on this topic. Every option comes with an explanation.
Practise 1.2 · 12 questions