Tutorialsteacher

Follow Us

Articles
  • C#
  • C# OOP
  • ASP.NET Core
  • ASP.NET MVC
  • LINQ
  • Inversion of Control (IoC)
  • Web API
  • JavaScript
  • TypeScript
  • jQuery
  • Angular 11
  • Node.js
  • D3.js
  • Sass
  • Python
  • Go lang
  • HTTPS (SSL)
  • Regex
  • SQL
  • SQL Server
  • PostgreSQL
  • MongoDB
  • JavaScript - Get Started
  • What is JavaScript
  • Setup JavaScript Dev Environment
  • HTML <script> Tag
  • JavaScript - Syntax
  • JavaScript - Popup Message
  • JavaScript - Variables
  • JavaScript - Operators
  • JavaScript - Data Types
  • JavaScript - String
  • JavaScript - Numbers
  • JavaScript - Boolean
  • JavaScript - Object
  • JavaScript - Date
  • JavaScript - Date Methods
  • JavaScript - Array
  • JavaScript - Array Methods
  • JavaScript - null and undefined
  • JavaScript - Function
  • JavaScript - if condition
  • JavaScript - switch
  • JavaScript - for loop
  • JavaScript - while loop
  • JavaScript - Scope
  • JavaScript - eval
  • JavaScript - Error Handling
  • JavaScript - strict mode
  • JavaScript - Hoisting
  • Define JS Class
  • JS Object In Depth
  • this Keyword
  • new Keyword
  • Prototype
  • Inheritance
  • Closure
  • IIFE
Entity Framework Extensions - Boost EF Core 9
  Bulk Insert
  Bulk Delete
  Bulk Update
  Bulk Merge

JavaScript Strict Mode (With Examples)

JavaScript is a loosely typed (dynamic) scripting language. If you have worked with server side languages like Java or C#, you must be familiar with the strictness of the language. For example, you expect the compiler to give an error if you have used a variable before defining it.

JavaScript allows strictness of code using "use strict" with ECMAScript 5 or later. Write "use strict" at the top of JavaScript code or in a function.

Example: strict mode
"use strict";

var x = 1; // valid in strict mode
y = 1; // invalid in strict mode
Try it

The strict mode in JavaScript does not allow following things:

  1. Use of undefined variables
  2. Use of reserved keywords as variable or function name
  3. Duplicate properties of an object
  4. Duplicate parameters of function
  5. Assign values to read-only properties
  6. Modifying arguments object
  7. Octal numeric literals
  8. with statement
  9. eval function to create a variable

Let look at an example of each of the above.

Use of undefined variables:

Example: strict mode
"use strict";

x = 1; // error
</code></pre>
        <div className="card-footer example-footer"></div></div>
</div>
    <p>
        Use of reserved keyword as name:
    </p>    <div className="card code-panel">        
           <div className="card-header example-title">Example: strict mode</div>
        <div className="panel-body"><pre className="csharpcode"><code>"use strict";

var for = 1; // error
var if = 1; // error
Try it

Duplicate property names of an object:

Example: strict mode
"use strict";

var myObj = { myProp: 100, myProp:"test strict mode" }; // error
Try it

Duplicate parameters:

Example: strict mode
"use strict";

function Sum(val, val){return val + val }; // error
Try it

Assign values to read-only property:

Example: strict mode
"use strict";

var arr = [1 ,2 ,3 ,4, 5];
arr.length = 10; // error
Try it

Modify arguments object:

Example: strict mode
"use strict";

function Sum(val1, val2){
    arguments = 100; // error
}
Try it

Octal literals:

Example: strict mode
"use strict";

var oct = 030; // error
Try it

with statement:

Example: strict mode
"use strict";

with (Math){
    x = abs(200.234, 2); // error
};
Try it

Eval function to create a variable:

Example: strict mode
"use strict";

eval("var x = 1");// error
Try it

Strict mode can be applied to function level in order to implement strictness only in that particular function.

Example: strict mode
x = 1; //valid

function sum(val1, val2){
    "use strict";

     result = val1 + val2; //error

    return result;
}
Try it
TUTORIALSTEACHER.COM

TutorialsTeacher.com is your authoritative source for comprehensive technologies tutorials, tailored to guide you through mastering various web and other technologies through a step-by-step approach.

Our content helps you to learn technologies easily and quickly for learners of all levels. By accessing this platform, you acknowledge that you have reviewed and consented to abide by our Terms of Use and Privacy Policy, designed to safeguard your experience and privacy rights.

[email protected]

ABOUT USTERMS OF USEPRIVACY POLICY
copywrite-symbol

2024 TutorialsTeacher.com. (v 1.2) All Rights Reserved.