---
title: "Why JavaScript use “use strict” directive in programming?"  
description: "Why JavaScript use “use strict” directive in programming?"  
author: "Ravi Vishwakarma"  
published: 2021-07-19  
canonical: https://answers.mindstick.com/qa/93747/why-javascript-use-use-strict-directive-in-programming  
category: "web application"  
tags: ["javascript", "web development", "web application development"]  
reading_time: 3 minutes  

---

# Why JavaScript use “use strict” directive in programming?

Why [JavaScript](https://www.mindstick.com/articles/1530/design-a-simple-stylish-calculator-using-html-css-and-javascript) use “use [strict](https://yourviews.mindstick.com/view/81367/usa-imposes-strict-ban-on-syria)” directive in [programming](https://www.mindstick.com/articles/12214/web-development-company-in-india-laid-on-the-foundation-of-concrete-java-programming)?\

## Answers

### Answer by user

Strict mode isn't just a subset: it intentionally has different semantics from normal code.

Browsers not supporting strict mode will run strict mode code with different behavior from browsers that do, so don't rely on strict mode without feature-testing for support for the relevant aspects of strict mode.

Strict mode code and non-strict mode code can coexist, so scripts can opt into strict mode incrementally.

**Strict mode makes several changes to normal JavaScript semantics.**\

1. Strict mode eliminates some JavaScript silent errors by changing them to throw errors.

2. Strict mode fixes mistakes that make it difficult for JavaScript engines to perform optimizations: strict mode code can sometimes be made to run faster than identical code that’s not strict mode.

3. Strict mode prohibits some syntax likely to be defined in future versions of ECMAScript.

4. It prevents, or throws errors, when relatively “unsafe” actions are taken (such as gaining access to the global object).

5. It disables features that are confusing or poorly thought out.

6. Strict mode makes it easier to write “secure” JavaScript.

7. helps to write a cleaner code

8. changes previously accepted silent errors (bad syntax) into real errors and throws an error message

9. makes it easier to write 'secure' JavaScript

## How to use strict mode

Strict mode can be used in two ways, remember strict mode doesn’t work with block statements enclosed in {} braces.

1. Used in global scope for the entire script.

2. It can be applied to individual functions.

## Using Strict mode for the entire script

```
 // applicable to whole program
'use strict';
function hello() {
    string = 'hello'; // throws an error
}
hello();
```

## Unused semicolon

```
‘use strict’
A = ‘hello’ // throw an error
```

## Undeclared variable not allowed

```
‘use strict’
A = “hello”; // throw an error
```

## Undeclared object are not allowed

```
‘use strict’
Person = { name : “ravi “ , age:20 }; // throw an error
```

\

### Answer by Ethan Karla

'use strict' it is a literal expression. this expression is not present in earlier version javascript. Main purpose of this expression, you can't access variable, function, objects without declare.

## Why are use 'use strict'

## Strict Mode Makes It Easier to Write safe Code in JavaScript. we can find error very quickly. The variable will not be rewritten from it.

```
Syntax<script>        'use strict'; // mandatory       statement</script>
```

```
'use strict';
x = 3.14;       // This will cause an error because x is not declared
```

```
'use strict';
myFunction();
function myFunction() {
  y = 3.14;   // This will also cause an error because y is not declared
}
```

```
x = 3.14;       // This will not cause an error.
myFunction();
function myFunction() {
  'use strict';
  y = 3.14;   // This will cause an error
}
```

Example

```
<!DOCTYPE html>
<html>
<body>
<h2>With 'use strict':</h2>
<script>
'use strict';
function x(p1, p1) {
}  // This will cause an error
</script>
</body>
</html>
```

![Why JavaScript use “use strict” directive in programming?](https://answers.mindstick.com/questionanswer/78d34436-78d2-4dac-8f0d-dfa005aeaf8b/images/3ccf4cd8-27e9-4701-b513-35976c529790.png)\


---

Original Source: https://answers.mindstick.com/qa/93747/why-javascript-use-use-strict-directive-in-programming

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
