String, Numbers & Math
String, Numbers & Math: Template Literal Concatenation Animation String and number manipulation is the daily bread of test automation: you read element text from the page, trim a
Template Literal Concatenation Animation
String and number manipulation is the daily bread of test automation: you read element text from the page, trim and normalize it, convert price strings to numbers, and write assertions. Template literals (backtick strings) dramatically simplify this — where Java requires `String.format("Hello %s, score %d", name, score)`, JavaScript uses `` `Hello ${name}, score ${score}` ``: far more readable with less room for error. Why does this matter so much? When building locator strings: `` `[data-testid="user-${userId}"]` `` for dynamic selector generation is both clearer and safer than `"[data-testid=\"user-" + userId + "\"]"`. Multi-line assertion messages also work without `\n` escapes or string concatenation hell. The real divergence from Java is on the number side, and it is sneaky: Java has separate `int` and `double` types and a compiler that warns you; JavaScript has a single `number` type, where `"12" + 3` yields `"123"` while `"12" - 3` yields `9`. Forget to wrap a price you scraped from the page in `Number()` and the test raises no error — it silently compares the wrong things and goes green.
Most Used String Methods
Number & Math Methods
Number Conversions and Math Object
// ─── String → Number conversions ──────────────────────────── let strNum = "42"; console.log(Number(strNum)); // 42 ← safe conversion console.log(parseInt("42px")); // 42 ← parses number, drops rest console.log(parseFloat("3.14")); // 3.14 console.log(+"99"); // 99 ← unary + shorthand console.log(Number("hello")); // NaN ← invalid conversion // ─── Number methods ─────────────────────────────────────────── let price = 19.9876; console.log(price.toFixed(2)); // "19.99" — decimal rounding (returns string!) console.log(Number.isNaN(NaN)); // true console.log(Number.isFinite(1/0)); // false (Infinity) console.log(Number.isInteger(42.0)); // true // ─── Math object ───────────────────────────────────────────── console.log(Math.round(4.6)); // 5 console.log(Math.floor(4.9)); // 4 ← floor (round down) console.log(Math.ceil(4.1)); // 5 ← ceil (round up) console.log(Math.abs(-15)); // 15 ← absolute value console.log(Math.max(1,9,3)); // 9 console.log(Math.min(1,9,3)); // 1 console.log(Math.random()); // random number between 0–1 console.log(Math.sqrt(16)); // 4
Micro Lab: JavaScript QA coding practice
Replace the TODO line with the critical line from the expected solution. This is not a real runtime; the goal is to reinforce writing the correct structure in a controlled way.
Step by Step: JavaScript QA coding practice
Read the goal and async dependencies
Place await / Promise chain in the right spot
Complete the assertion or expectation line
Run and read console or test output
If flaky, add a wait strategy or retry