DEV Community

Cover image for Understanding JavaScript Strict Mode

Understanding JavaScript Strict Mode

Accreditly on June 05, 2023

In JavaScript, the language provides a feature known as 'strict mode', introduced in ECMAScript 5 (ES5), that helps developers avoid common JavaScr...
Collapse
 
algot profile image
AlgoT

Really recommend also trying out TypeScript. These kind of mistakes wouldn't even let your code execute, because TypeScript would throw errors. TypeScript is also very configurable, so you can make it as strict as you want, or you can make it more relaxed - depends on what kind of mistakes you find more often than others. Huge time saver.

Collapse
 
accreditly profile image
Accreditly

Big fans of TypeScript here!

Collapse
 
algot profile image
AlgoT

Big fans of anything that saves time & catches bugs before it's a production fire!

Collapse
 
Sloan, the sloth mascot
Comment deleted
Collapse
 
dragosdev profile image
Dragos Barbuta

Hey, great post about 'use strict'! It's like a friendly coach for writing better JavaScript. Here's a quick extra thought - have you considered how it handles variables?

Without 'strict mode', if you forget to declare a variable, JavaScript thinks you want it global (available everywhere). This can cause funny bugs. Like this:

function oops() {
  surprise = "Surprise! I'm global.";
}
oops();
console.log(surprise); // "Surprise! I'm global."
Enter fullscreen mode Exit fullscreen mode

But with 'strict mode', JavaScript stops us. If we forget, it gives an error. This is super helpful for avoiding bugs. Like so:

"use strict";
function noOops() {
  surprise = "No surprises here."; // Error!
}
noOops();
Enter fullscreen mode Exit fullscreen mode

But here's a cool fact - if you're using modules (a way to split your code into separate files), you don't have to worry about this. Modules automatically apply 'strict mode', keeping our code nice and tidy. So, another win for 'use strict'!

Collapse
 
abdulla1201 profile image
abdulla1201 • Edited

Really recommend also trying out TypeScript. These kind of mistakes wouldn't even let our code execute, because TypeScript would throw errors. TypeScript is also very configurable, so you can make it as strict as you want, or you can make it more relaxed - depends on what kind of mistakes you find more often than others. Huge time saver.
Regards: Pubg Nickname

Collapse
 
free_fire_aee184a74134468 profile image
free fire

Really recommend also trying out TypeScript. These kind of mistakes wouldn't even let our code execute, because TypeScript would throw errors. TypeScript is also very configurable, so you can make it as strict as you want, or you can make it more relaxed - depends on what kind of mistakes you find more often than others. Huge time saver.
Regard: Free Fire Nickname

Collapse
 
jonrandy profile image
Jon Randy 🎖️

...it's a best practice to start your scripts with the "use strict" directive.

Unnecessary in Javascript modules since they are in strict mode by default.

Collapse
 
accreditly profile image
Accreditly

This is true, but it's still good practice elsewhere 👍

Collapse
 
johner97 profile image
Johner97

This post explains everything perfectly.

Collapse
 
josepedrolorenzini profile image
Jose_Lorenzini

Typescript does that !

Collapse
 
Sloan, the sloth mascot
Comment deleted
Collapse
 
richardpaulhall profile image
richardpaulhall

Much of what should have been in JavaScript from the beginning.

Collapse
 
ttsoares profile image
Thomas TS

After some week studying TS I was bitten by JSDoc and switched...
In the long run my bet is that JSDoc will render TS obsolete...
Plus, it is always nice to NOT use Micro$oft stuff ! ;-)