Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Concept Entry] Javascript: Strict Mode #5901

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions content/javascript/concepts/strict-mode/strict-mode.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
---
Title: 'Strict Mode'
Description: 'Strict mode enforces stricter rules to catch common coding errors, improve security, and prevent the use of certain problematic features.'
Subjects:
- 'Web Development'
- 'Computer Science'
Tags:
- 'Strict Mode'
CatalogContent:
- 'introduction-to-javascript'
- 'paths/front-end-engineer-career-path'
---

**Strict mode** in JavaScript is a way to opt into a stricter version of the language, enforcing rules that can help prevent common coding mistakes. It makes the code more predictable and easier to debug by restricting certain behaviors that could lead to errors or unexpected behavior in non-strict mode.

## Syntax

Strict mode can be enabled for an entire file by adding `"use strict"` at the very beginning of the script as follows:

```pseudo
"use strict";
// All code here runs in strict mode
```

Strict mode can also be enabled within a specific function, which will only affect the code inside that function:

```pseudo
function example() {
"use strict";
// Only this function's code runs in strict mode
}
```

## Example

The example below shows how strict mode prevents creating global variables accidentally. Without strict mode, assigning a value to an undeclared variable would automatically create a global variable:

```js
x = 10;
console.log(x);
// 10
```

This gives an output as follows:

```shell
10
```

In strict mode, this results in an error, helping prevent unintended global variables:

```js
"use strict";
x = 10;
// Throws an error: 'x is not defined'
```

The output for this code will be:

```shell
ReferenceError: x is not defined
```

## Codebyte Example

The following example showcases that in strict mode, duplicate parameter names are not allowed:

```codebyte/javascript
"use strict";

// Strict mode prevents duplicate parameter names
function example(a, a) {
console.log(a);
}
```

This will throw a SyntaxError before the function is even executed, with a message `SyntaxError: Duplicate parameter name not allowed in this context`.
1 change: 1 addition & 0 deletions documentation/tags.md
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ Static Site
Statistics
Statsmodels
Storage
Strict Mode
Stringr
Strings
Structure
Expand Down
Loading