Skip to content

Help center

Learn to script

The ideas behind formulas and small programs, from Novus Learn's JavaScript course: values, conditions, functions, loops, lists, text, errors and testing. Ask SW Agent and it quotes the lessons.

Describes Sonic Weaver v5.20, released .

About these lessons

These lessons come from Novus Learn, our own learning app, whose JavaScript course was written to be checked: every example was run before it was published. They teach the ideas a formula or a small script is made of, one step at a time, for someone who has never programmed.

SW Agent has every lesson's full text. Ask it a question, for example how a loop works or what a function returns, and it quotes the lesson that answers it.

Programs, values, and types

Run a program, print a value, and name every kind of value JavaScript has.

  • What a program actually is: say what a program is in one plain sentence, without using the word code.
  • Running a program and reading its output: run a snippet and find where its output appears.
  • Numbers and arithmetic: use the six arithmetic operators and predict their results.
  • Text values, a first look: write text with double quotes, single quotes and backticks, and choose between them.
  • True, false, and comparing values: name the two boolean values and produce them with comparison operators.
  • Naming values: let and const: declare a value with const and explain why that is the default choice.
  • undefined, null, and naming every type: say what undefined means and where it comes from without you writing it.
  • Coercion, and the two kinds of equals: predict whether + will add two values or join them as text.

Making decisions

Make a program take a different path depending on its input.

  • if, else, and blocks: write an if statement that runs some code only when a condition is true.
  • Truthiness and the falsy values: say what JavaScript does when the condition of an if is not a boolean.
  • and, or, and the nullish fallback: combine two conditions with && and || to make one condition.
  • switch, and when to avoid it: write a switch statement with case labels, break and default.
  • The conditional operator: write a conditional expression that produces one of two values.
  • Project: building a decision table: turn a set of written rules into an ordered decision table before writing any code.

Functions: naming and reusing behaviour

Package behaviour behind a name someone else can call.

  • Declaring and calling your first function: say in your own words what a function is and why a program would want one.
  • Parameters, arguments and defaults: use the words parameter and argument correctly, and know which is which.
  • Return values and early return: explain the difference between printing a value and returning one.
  • Function expressions and arrow functions: store a function in a const and call it through that name.
  • Scope and shadowing: say which parts of a program can see a given name, and which cannot.
  • Closures: functions that remember: explain in one sentence what a closure is, in words someone else would follow.
  • Pure functions and side effects: state the two conditions a function must meet to be called pure.

Iteration and array transformation

Turn one collection into another without writing the same loop twice.

  • Repeating work with for and while: read a for loop header and say exactly how many times its body will run.
  • for...of and for...in: items and keys: write a for...of loop that reads every value of an array or string.
  • forEach, and when a plain loop is clearer: write a forEach call that runs a function once for every element.
  • map: one array in, a new array out: use map to build a new array from an existing one without touching the original.
  • filter, find, some and every: asking questions of a collection: use filter to build a shorter array containing only the elements that pass a test.
  • reduce: many values into one: explain what the accumulator is and how it travels from one call to the next.
  • Project: chaining transformations into a pipeline: read a chain of array methods top to bottom and say what the data looks like at each step.

Structured data: arrays and objects

Hold many related values and reach into them safely.

  • Arrays: many values under one name: say why a list of values needs one name plus a position, not one variable per value.
  • Changing a list: adding, removing, copying: add and remove elements at either end with push, pop, unshift and shift, and say what each one gives back.
  • Objects: values with names instead of positions: say when a name is a better label for a value than a position, and write an object literal.
  • Shapes inside shapes: read a value several levels down a nested structure, one step at a time.
  • Unpacking and rebuilding: destructuring and spread: pull named properties out of an object into their own variables in one statement.
  • References: when two names mean one thing: explain what a variable holding an object actually stores.
  • Reaching in safely: optional chaining and defaults: use ?. to read through a value that might be null or undefined without throwing.

Strings and text processing

Take apart, clean up and rebuild text you did not write.

  • Strings cannot be changed, only rebuilt: say what a string is: a fixed, ordered run of characters you can read but never edit in place.
  • Building text out of values: build a string from fixed wording and changing values using a template literal and ${}.
  • Finding things and cutting pieces out: ask whether text contains, starts with or ends with something and get a true or false answer.
  • Splitting, joining and cleaning up: turn one string into an array of pieces with split, choosing a separator that suits the data.
  • Comparing text people actually typed: compare two pieces of typed text fairly by trimming the ends and putting both into one case.
  • Regular expressions: a first pass: read a simple regular expression out loud: which characters it matches and how many of them.

Modelling real data without a browser

Choose a shape for real data and change it without breaking it.

  • Choosing a shape for your data: explain what the shape of some data is, in words, before writing any code.
  • JSON: turning data into text and back: say what serialisation is and why data has to become text to leave a program.
  • Lookup tables: finding one thing quickly: explain why searching a list again and again gets slow, in plain terms.
  • Map and Set: when a plain object is the wrong tool: say two things a Map does that a plain object cannot.
  • Changing data without breaking it: explain what it means for two names to point at the same object.
  • Project: modelling an invoice: design a shape for a real document and justify each level of nesting.

Errors, validation, and defensive code

Decide what your code does when the input is wrong.

  • What an error actually is: describe what happens to a running program at the moment something goes wrong.
  • Throwing, and the objects you throw: throw an error on purpose, with a message that tells the reader what to change.
  • try, catch, finally: catch an error so the program continues, and say exactly which lines were skipped.
  • Validating input at the boundary: name the boundary of a program and say why input is checked there and not everywhere.
  • Returning outcomes instead of throwing: decide whether a particular failure is exceptional or an ordinary outcome.
  • Failing loudly, not silently: spot the three common ways JavaScript code fails without saying anything.

Testing and debugging

Prove your code works and find out why when it does not.

  • What a test actually is: say in one sentence what a test is, and what makes one worth keeping.
  • Arrange, act, assert: split any test into three visible steps: arrange the inputs, act once, assert on the result.
  • Edge cases and boundaries: work through a checklist of edge cases instead of picking test values at random.
  • Test doubles without a framework: explain what a test double is and which problem it exists to solve.
  • Debugging by bisection: turn 'it does not work' into a case that fails the same way every time.
  • How this maps to Vitest: recognise describe, it and expect, and say which hand-written part each one replaces.

Help center

Keep going

Was this page helpful?

Anonymous. Only this page’s address and your yes/no are recorded, and only if you allowed analytics cookies.