Comparing JavaScript Strict Mode and TypeScript: What Sets Them Apart?
4 min read

What is JavaScript strict mode?
Strict mode was introduced to JavaScript because of how loose and unrestricted the language is. The language is missing several restrictions that would have been beneficial from the start; this is what Strict mode addresses. Essentially, it helps developers write better and more secure code by eliminating some silent JavaScript errors and ensuring they throw errors instead. It was introduced in ECMAScript 5.
Strict mode is declared by adding “use strict”
at the beginning of a script or to individual functions. When applied to the beginning of a script, it has a global scope. To use it with functions, we place Strict mode on the first line. This mode helps catch common coding mistakes and prevents the use of certain unsafe features. For instance, it disallows the use of undeclared variables and throws errors for assignments to non-writable properties.
Applies to the entire file
"use strict";
Applies to an individual function
function example(){
"use strict";
i = 5; // this will cause an error because i is not declared
}
Typescript
TypeScript is a strict superset of JavaScript, meaning it has everything JavaScript offers and more. This includes static type checking, which allows developers to define types for variables, function parameters, and return values. This type of system enables compile-time checks, catching errors before the code executes, rather than while it is running.
Differences
Scope of Enforcement:
Strict Mode: Operates at runtime, influencing how JavaScript code is executed.
TypeScript: Provides compile-time checks to ensure type correctness before the code is executed.
Error Detection:
Strict Mode: Catches certain errors during execution, such as assigning to read-only properties or using undeclared variables.
TypeScript: Identifies type-related errors during compilation, preventing issues like type mismatches and undefined properties.
Code Annotation:
Strict Mode: Requires the explicit
"use strict";
directive in JavaScript files to activate Strict mode.TypeScript: Utilises type annotations and interfaces to define data structures and function signatures, enhancing code clarity and maintainability.
Why can't we just use JavaScript Strict mode instead of TypeScript?
JavaScript strict mode enhances runtime behaviour by catching common mistakes and enforcing safer coding practices, but it has limitations compared to TypeScript, such as:
No static type checking:
Strict mode only affects runtime behaviour; it doesn't check for type correctness at compile time. This means you can still run into type-related errors (e.g., passing a string to a function expecting a number). TypeScript catches such errors before execution.Lack of advanced features:
TypeScript introduces features like interfaces, generics, enums, and access modifiers, making it easier to write scalable and maintainable applications.
How to know which to use
Your choice between strict mode and TypeScript depends on your project's complexity, team size, and long-term goals.
Use JavaScript Strict mode if:
You're working on small scripts or quick prototypes.
You don't want a compilation step in your workflow.
Performance and file size are a concern (TypeScript requires compilation).
The project has minimal complexity, and dynamic typing is acceptable.
Use TypeScript if:
You're building large, complex applications with multiple contributors.
You need to enforce strict type safety to prevent runtime errors.
You want improved maintainability with better documentation through types.
You're working on enterprise-level projects that require scalability.
Consider using both together:
If you are already using TypeScript, it can generate "use strict";
directives in the compiled JavaScript output, giving you the benefits of both stricter runtime checks and compile-time safety.
Why you should know both
Understanding both JavaScript strict mode and TypeScript is essential because:
Broader knowledge: Knowing strict mode helps you write better vanilla JavaScript, while TypeScript knowledge prepares you for more complex projects.
Working with different environments: Some projects might not support TypeScript, so strict mode becomes a useful alternative.
Gradual migration: If you're moving from JavaScript to TypeScript, knowing strict mode helps ensure better code practices during the transition.
Conclusion
While JavaScript strict mode is useful for enforcing safer coding habits at runtime, TypeScript provides a much more robust solution for managing code complexity, catching errors earlier, and improving developer productivity. Choosing the right tool depends on your project needs, but learning both helps you adapt to various development environments.