HiveBrain v1.2.0
Get Started
← Back to all entries
snippetjavascriptTip

What does 'use strict' do and what are some of the key benefits to using it?

Submitted by: @import:30-seconds-of-code··
0
Viewed 0 times
benefitsjavascriptwhatandstrictdoesusesometheusing

Problem

Strict mode can be applied to entire scripts or individual functions by including 'use strict' before any other statements.
```js title="script.js"
'use strict';
const x = "Hello from a strict mode script";
js title="other.js"

Solution

This enforces more strict parsing and error handling of JavaScript code, as described below.

## No accidental global variables

Strict mode makes it impossible to accidentally create global variables due to mistyped variable names. Assignments, which would accidentally create global variables, instead throw an error in strict mode:


'use strict';
const x = "Hello from a strict mode script";
js title="other.js"
function strict() {
'use strict';
const x = 'Hello from a strict mode function';

Code Snippets

This enforces more strict parsing and error handling of JavaScript code, as described below.

## No accidental global variables

Strict mode makes it impossible to accidentally create global variables due to mistyped variable names. Assignments, which would accidentally create global variables, instead throw an error in strict mode:
## Elimination of silent errors

Strict mode changes some previously-accepted mistakes into errors. These include:

- Assignments which would otherwise silently fail
- Deleting undeletable properties or plain names
- Duplicated names in function arguments
- 0-prefixed octal literals
- Setting properties on primitives
## Simplified `eval`

Strict mode makes `eval` more transparent by preventing the introduction of new variables in the surrounding scope. In strict mode, `eval` creates variables only for the code being evaluated.

Context

From 30-seconds-of-code: use-strict

Revisions (0)

No revisions yet.