Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 14.9 — Strict Mode and Language Quirks
JavaScript · Example 1 of 3

Silent in sloppy mode, an error in strict mode

The sloppy versions "worked": one created a global totl by accident and one silently ignored a write. The strict versions point at the bug.

Strict mode, switched on by "use strict" and automatically on in modules and classes, turns silent mistakes into errors, removes a few confusing features and makes this safer; the rest of JavaScript's odd legacy behaviours stay in the language forever, so you learn to recognise and avoid them.

Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.

Ready for more? This phase has 5 hands-on exercises with automatic checks.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

Predict and explain the output of "use strict"; function show() { return typeof this; } console.log(show(), show.call(7));.

02

This sloppy code has a typo that strict mode would catch. Add the directive, run it, and fix the bug: let score = 0; function addPoint() { scor = score + 1; } addPoint(); console.log(score);

Explain it without notes

01

What is strict mode, how do you enable it, and where is it on automatically?

02

List four behaviours that change in strict mode.

03

Why does this differ between sloppy and strict functions called without an object?

04

Name some legacy quirks strict mode doesn't fix, and why they can't be fixed.

Silent in sloppy mode, an error in strict mode

Preview

Press Run to see the page.

Console

Nothing logged yet.

Expected console output

sloppy undeclared -> number
strict undeclared -> ReferenceError: totl2 is not defined
sloppy frozen -> 1
strict frozen -> TypeError: Cannot assign to read only property 'a' of object '#<Object>'
strict new prop -> TypeError: Cannot add property b, object is not extensible
strict getter only -> TypeError: Cannot set property value of #<Object> which has only a getter
strict primitive -> TypeError: Cannot create property 'foo' on number '5'
sloppy this -> true
strict this -> undefined