• Home
  • Jobs
  • Courses
  • Certifications
  • Companies
  • Online IDE
  • Login
  • Signup
MYTAT
  • Home
  • Jobs
  • Courses
  • Certifications
  • Companies
  • Online IDE
  • Login
  • Signup
JavaScript
  • JavaScript Introduction
  • JavaScript Where To
  • JavaScript Output
  • JavaScript Statements
  • JavaScript Syntax
  • JavaScript Comments
  • JavaScript Variables
  • JavaScript Let
  • JavaScript Const
  • JavaScript Operators
  • JavaScript Assignment
  • JavaScript Data Types
  • JavaScript Functions
  • JavaScript Objects
  • JavaScript Events
  • JavaScript Strings
  • JavaScript String Methods
  • JavaScript Numbers
  • JavaScript Number Methods
  • JavaScript Arrays
  • JavaScript Array Const
  • JavaScript Array Methods
  • JavaScript Sorting Arrays
  • JavaScript Array Iteration
  • JavaScript Date Objects
  • JavaScript Date Formats
  • JavaScript Get Date Methods
  • JavaScript Set Date Methods
  • JavaScript Math Object
  • JavaScript Random
  • JavaScript Booleans
  • JavaScript Comparison And Logical Operators
  • JavaScript If Else And Else If
  • JavaScript Switch Statement
  • JavaScript For Loop
  • JavaScript Break And Continue
  • JavaScript Type Conversion
  • JavaScript Bitwise Operations
  • JavaScript Regular Expressions
  • JavaScript Errors
  • JavaScript Scope
  • JavaScript Hoisting
  • JavaScript Use Strict
  • The JavaScript This Keyword
  • JavaScript Arrow Function
  • JavaScript Classes
  • JavaScript JSON
  • JavaScript Debugging
  • JavaScript Style Guide
  • JavaScript Common Mistakes
  • JavaScript Performance
  • JavaScript Reserved Words
  • JavaScript Versions
  • JavaScript History
  • JavaScript Forms
  • JavaScript Validation API
  • JavaScript Objects
  • JavaScript Object Properties
  • JavaScript Function Definitions
  • JavaScript Function Parameters
  • JavaScript Function Invocation
  • JavaScript Closures
  • JavaScript Classes
  • Java Script Async
  • JavaScript HTML DOM
  • The Browser Object Model
  • JS Ajax
  • JavaScript JSON
  • JavaScript Web APIs
  • JS Vs JQuery
  • Home
  • Courses
  • JavaScript
  • JavaScript Use Strict

JavaScript Use Strict

Previous Next

The "use Strict" Directive

The "use strict" directive was new in ECMAScript version 5.

It is not a statement, but a literal expression, ignored by earlier versions of JavaScript.

The purpose of "use strict" is to indicate that the code should be executed in "strict mode".

With strict mode, you can not, for example, use undeclared variables.

All modern browsers support "use strict" except Internet Explorer 9 and lower:
 

The numbers in the table specify the first browser version that fully supports the directive.

You can use strict mode in all your programs. It helps you to write cleaner code, like preventing you from using undeclared variables.

"use strict" is just a string, so IE 9 will not throw an error even if it does not understand it.



Practice Excercise Practice now

Declaring Strict Mode

Strict mode is declared by adding "use strict"; to the beginning of a script or a function.

Declared at the beginning of a script, it has global scope (all code in the script will execute in strict mode):

Example

"use strict";
x = 3.14;       // This will cause an error because x is not declared
Try it now

Example

"use strict";
myFunction();

function myFunction() {
  y = 3.14;   // This will also cause an error because y is not declared
}
 
Try it now

Declared inside a function, it has local scope (only the code inside the function is in strict mode):

x = 3.14;       // This will not cause an error.
myFunction();

function myFunction() {
  "use strict";
  y = 3.14;   // This will cause an error
}
 
Try it now

The "use strict"; Syntax

The syntax, for declaring strict mode, was designed to be compatible with older versions of JavaScript.

Compiling a numeric literal (4 + 5;) or a string literal ("John Doe";) in a JavaScript program has no side effects. It simply compiles to a non existing variable and dies.

So "use strict"; only matters to new compilers that "understand" the meaning of it.



Practice Excercise Practice now

Why Strict Mode?

Strict mode makes it easier to write "secure" JavaScript.

Strict mode changes previously accepted "bad syntax" into real errors.

As an example, in normal JavaScript, mistyping a variable name creates a new global variable. In strict mode, this will throw an error, making it impossible to accidentally create a global variable.

In normal JavaScript, a developer will not receive any error feedback assigning values to non-writable properties.

In strict mode, any assignment to a non-writable property, a getter-only property, a non-existing property, a non-existing variable, or a non-existing object, will throw an error.


Not Allowed in Strict Mode

Using a variable, without declaring it, is not allowed:

"use strict";
x = 3.14;                // This will cause an error
Try it now

Objects are variables too.

Using an object, without declaring it, is not allowed:

"use strict";
x = {p1:10, p2:20};      // This will cause an error
Try it now

Deleting a variable (or object) is not allowed.

"use strict";
var x = 3.14;
delete x;                // This will cause an error
Try it now

Deleting a function is not allowed.

"use strict";
function x(p1, p2) {};
delete x;                // This will cause an error 
Try it now

Duplicating a parameter name is not allowed:

"use strict";
function x(p1, p1) {};   // This will cause an error
Try it now

Octal numeric literals are not allowed:

"use strict";
var x = 010;             // This will cause an error
Try it now

Octal escape characters are not allowed:

"use strict";
var x = "\010";            // This will cause an error
Try it now

Writing to a read-only property is not allowed:

"use strict";
var obj = {};
Object.defineProperty(obj, "x", {value:0, writable:false});

obj.x = 3.14;            // This will cause an error
Try it now

Writing to a get-only property is not allowed:

"use strict";
var obj = {get x() {return 0} };

obj.x = 3.14;            // This will cause an error
Try it now

Deleting an undeletable property is not allowed:

"use strict";
delete Object.prototype; // This will cause an error
Try it now

The word eval cannot be used as a variable:

"use strict";
var eval = 3.14;         // This will cause an error

The word arguments cannot be used as a variable:

"use strict";
var arguments = 3.14;    // This will cause an error

The with statement is not allowed:

"use strict";
with (Math){x = cos(2)}; // This will cause an error

For security reasons, eval() is not allowed to create variables in the scope from which it was called:

"use strict";
eval ("var x = 2");
alert (x);             // This will cause an error

The this keyword in functions behaves differently in strict mode.

The this keyword refers to the object that called the function.

If the object is not specified, functions in strict mode will return undefined and functions in normal mode will return the global object (window):

"use strict";
function myFunction() {
  alert(this); // will alert "undefined"
}
myFunction();

Future Proof!

Keywords reserved for future JavaScript versions can NOT be used as variable names in strict mode.

These are:

  • implements
  • interface
  • let
  • package
  • private
  • protected
  • public
  • static
  • yield
"use strict";
var public = 1500;      // This will cause an error

Watch Out!

The "use strict" directive is only recognized at the beginning of a script or a function.



Practice Excercise Practice now

Previous Next
COMPANY
  • About us
  • Careers
  • Contact Us
  • In Press
  • People
  • Companies List
Products
  • Features
  • Coding Assessments
  • Psychometric Assessment
  • Aptitude Assessments
  • Tech/Functional Assessments
  • Video Assessment
  • Fluency Assessment
  • Campus
 
  • Learning
  • Campus Recruitment
  • Lateral Recruitment
  • Enterprise
  • Education
  • K 12
  • Government
OTHERS
  • Blog
  • Terms of Services
  • Privacy Policy
  • Refund Policy
  • Mart Category
Partner
  • Partner Login
  • Partner Signup

Copyright © RVR Innovations LLP 2025 | All rights reserved - Mytat.co is the venture of RVR Innovations LLP