You have an environment that runs JavaScript. Now it's time to actually write some. This lesson walks through the few primitives every JavaScript program is built from — printing output, doing arithmetic, joining strings, leaving comments — and ends with a 12-line program that builds a real test result summary. By the end of the lesson you'll be writing small, useful scripts on your own.
console.log — your best friend
console.log prints whatever you pass it to the terminal (in Node.js) or the browser console (in a browser). It is the most-used line of JavaScript in any QA toolkit, and it's the fastest way to see what your code is doing.
console.log("Test passed!");Output:
Test passed!
You can pass a number:
console.log(42);Output:
42
Or several values at once, separated by commas — console.log puts a space between them automatically:
console.log("Status:", 200, "OK");Output:
Status: 200 OK
This trick is especially useful for debugging. When a test fails, sprinkling console.log("response:", response) around lets you see exactly what value your code is working with at each step.
Comments — code that's just for humans
Anything after // on a line, or between /* */, is ignored by JavaScript. Comments are how you explain why the code is doing something, especially in test files where the next person to read your code is your future self at 11pm trying to fix a flaky test.
// Skip this test in CI — runs too slowly on shared machines
console.log("Skipping slow test");
/*
* Block comments are useful for longer notes,
* like a multi-line explanation of an unusual workaround.
*/A useful rule: comment the why, not the what. The code already says what it does. Future you wants to know why it does that — what bug, what spec, what tradeoff prompted it.
Arithmetic
JavaScript has the math operators you'd expect.
console.log(10 + 5); // 15 — addition
console.log(10 - 5); // 5 — subtraction
console.log(10 * 5); // 50 — multiplication
console.log(10 / 5); // 2 — division
console.log(10 % 3); // 1 — modulo (the remainder)The modulo operator (%) returns the remainder after division. It looks niche but it's surprisingly useful in test data generation — for example, "give every 3rd test user a premium plan" turns into index % 3 === 0. You'll see modulo used for round-robin assignment, alternating row colours, and bucketing test data into groups.
String concatenation
To join strings, you can use +:
const status = "passed";
console.log("Test " + status);Output:
Test passed
This works, but quickly gets ugly when you have several pieces:
console.log("Test " + name + " " + status + " in " + duration + "ms");Modern JavaScript provides template literals — strings wrapped in backticks (`) that let you embed values with ${...}:
const name = "login";
const status = "passed";
const duration = 124;
console.log(`Test ${name} ${status} in ${duration}ms`);Output:
Test login passed in 124ms
Template literals are the standard for any test code — building dynamic URLs, building CSS selectors, formatting test report messages. Use them anywhere you'd otherwise reach for +.
A real QA example
Putting it all together. Here is a complete 12-line program that prints a test result summary — exactly the kind of utility you might write in your first month on the job.
// Daily test summary — run after the suite completes
const passed = 28;
const failed = 4;
const total = passed + failed;
const passRate = (passed / total) * 100;
console.log("=== Test Run Summary ===");
console.log(`Total: ${total}`);
console.log(`Passed: ${passed}`);
console.log(`Failed: ${failed}`);
console.log(`Pass rate: ${passRate}%`);
console.log("========================");Save it as summary.js and run with node summary.js. Output:
=== Test Run Summary ===
Total: 32
Passed: 28
Failed: 4
Pass rate: 87.5%
========================
Read each line: declare numbers, do arithmetic, print formatted output. That is 90% of every QA utility script you'll ever write — the rest is just more of the same with bigger numbers and prettier formatting.
Predict the output
What does each console.log print?
Match each line of code to its expected output.
- console.log(2 + 3 * 4)
- console.log("2" + "3")
- console.log(10 % 4)
- console.log(`Status: ${200}`)
- console.log("hi", "QA")
The two tricky ones: 2 + 3 * 4 is 14 because multiplication runs before addition (just like in maths class). And "2" + "3" is the string "23", not the number 5 — when one side of + is a string, JavaScript joins them as text. That subtle behaviour is one of the most common sources of test bugs, and we'll meet it again in the next lesson.
⚠️ Common mistakes
- Forgetting the quotes around text.
console.log(passed)prints the value of a variable namedpassed— butconsole.log("passed")prints the literal word "passed". They look almost identical and produce very different results. The errorpassed is not definedmeans you wrote a word JavaScript thought was a variable name. - Using straight quotes vs smart quotes.
"hello"works.“hello”(the curly versions auto-inserted by Word, Notion, and some chat apps) does not — JavaScript only understands straight quotes ("and') and backticks (`). If you seeUnexpected token, suspect smart-quote contamination from a copy-paste. - Mixing up
+for numbers and strings.2 + 3is5, but"2" + 3is"23". JavaScript's automatic type-conversion can hide test bugs when an API returns a number-as-string. If your assertion compares values from different sources, double-check the types withtypeof(lesson 4).
🎯 Practice task
Write a real script in your js-for-qa folder. 15-20 minutes.
-
Create a file
report.js. -
Declare four variables for a test run:
suiteName,passed,failed, andskipped. Pick any name and any reasonable numbers. -
Calculate
total = passed + failed + skipped. -
Calculate
passRate = (passed / total) * 100. -
Use
console.logand template literals to print a multi-line report:Suite: <suiteName> Total: <total> | Passed: <passed> | Failed: <failed> | Skipped: <skipped> Pass rate: <passRate>% -
Run with
node report.jsand confirm the output matches your inputs. -
Stretch: add a
duration(in seconds) variable and use%to print "took X minutes Y seconds" — for example a duration of120should print "took 2 minutes 0 seconds". Hint:Math.floor(duration / 60)for the minutes,duration % 60for the seconds.
You've now written something with real QA shape. The next lesson formalises what variables actually are and the data types they can hold.