Make Strict-Mode Errors Visible
Make Strict-Mode Errors Visible
Strict mode changes several permissive script behaviors into errors. Accidentally assigning an undeclared school variable should fail rather than silently creating a global property. Modules and class bodies already run in strict mode.
Before this topic, review Truthiness and Missing Values if its terms are unfamiliar.
What you’ll learn
- Explain make strict-mode errors visible through the school example.
- Trace the shown operations and distinguish their result from displayed output.
- Make the practice change and explain its consequence: A direct output call needs no undeclared assignment, so it succeeds.
Work through the school example
This is JavaScript using language features and the host console. Read the initial values, then follow each operation to its displayed result. A declaration names a value, a function call performs an operation, and console.log displays its arguments.
JavaScript · school.js · complete example
'use strict';
try {
undeclaredMark = 72;
} catch (error) {
console.log(error.name);
}
Expected output
ReferenceErrorRun in JavaScript compiler →Recognize the important boundary
Strict mode is not a type checker or a security sandbox. Declare the name instead of disabling strict mode to conceal the error.
Try a small variation
JavaScript · original source for practice
'use strict';
try {
undeclaredMark = 72;
} catch (error) {
console.log(error.name);
}
Your task: Replace undeclaredMark = 72 with console.log(72). Predict the result before running the edited program.
The source is repeated here. If the edit targets the HTML rather than the script, use the markup repeated below; the worked answer also repeats the changed markup.
Worked answer and reasoning
A direct output call needs no undeclared assignment, so it succeeds.
JavaScript · complete worked variation
'use strict';
try {
console.log(72);
} catch (error) {
console.log(error.name);
}
Expected output
72Run in JavaScript compiler →Predict the original program
JavaScript · original program for this question
'use strict';
try {
undeclaredMark = 72;
} catch (error) {
console.log(error.name);
}
Keep the useful idea
Strict mode changes several permissive script behaviors into errors. Accidentally assigning an undeclared school variable should fail rather than silently creating a global property. Modules and class bodies already run in strict mode.
Check your variation against this explanation: A direct output call needs no undeclared assignment, so it succeeds.
Use the JavaScript array reference library for individual array operations and boundary tests to check a rule independently.
Reaching the recap and completing the quizzes are separate milestones.