Categories
Uncategorized

Understanding the ‘Use Strict’ in JavaScript

What is ‘Use Strict’?

The “use strict” is new feature in JavaScript 1.8.5 (ECMAScript version 5) that permits us to put a program/function in a “strict mode”. It allows to write “Secure” JavaScript.

 Strict mode prevents certain actions from being taken and throws more exceptions

Need of Strict Mode

   

Learning Use Strict

   It catches some common coding bloopers, throwing exceptions.
       It prevents, or throws errors, when relatively “unsafe” actions are taken            (such as gaining access to the global object).
       It disables features that are confusing or poorly thought out.

Working Flow of ‘Use Strict’

An ECMAScript Program syntactic unit may be processed using either unrestricted or strict mode syntax and semantics. When processed using strict mode we have three types of  ECMAScript codes:

  •  strict global code
  •  strict eval code
  •  strict function code


Note:-
 1.Global code is strict global code if it begins with a Directive Prologue that contains a Use Strict Directive 

 2.Eval code is strict eval code if it begins with a Directive Prologue that contains a Use Strict Directive or if the call to eval is a direct call to the eval function that is contained in strict mode code.

3.Function code that is part of a Function Declaration, Function Expression, or accessor Property Assignment is strict function code if its Function Declaration, Function Expression, or Property Assignment is contained in strict mode code or if the function code begins with a Directive Prologue that contains a Use Strict Directive. 

4.Function code that is supplied as the last argument to the built-in Function constructor is strict function code if the last argument is a String that when processed as a Function Body begins with a Directive Prologue that contains a Use Strict Directive.


Declaring Strict Mode

Pitch this at the beginning of a JavaScript file to enable it for the whole script (Global scope):

Use Strict Command
Use Strict Command

Or toss it within a function to turn on strict mode only within that context (Function scope).

Use Strict inside a function
Use Strict inside a function


Syntax
         The structure/syntax, for declaring strict mode, was designed to be backward-compatible with older versions of JavaScript.
All of the “features” that were in earlier versions of JavaScript that were “bad syntaxes” are simply disabled (or throw errors) in strict mode. 


Changes when we use Strict Mode
         Any attempt to use a variable without declaring is not admitted.
Eg: foo=”bar” will throw an error

Earlier it would assign the value to the foo property of the global object (e.g.window.foo)

Error in Use Strict
Error in Use Strict

Duplicate property in an object literal will cause an error.

Duplicate Property Error in Use Strict
Duplicate Property will generate Error

 Variable or function cannot be deleted

Deleting Function or variable in Use Strict
Delete Error in Use Strict 

 Duplicate parameters are not allowed

Duplicate Parameter Error
Duplicate Parameter Error

 Deleting an undeletable property is not allowed

Deleting in Use Strict
Deleting Error 

 Any attempt to use the name ‘eval’ is strictly prohibited – as is the ability to assign the eval function to a variable or a property of an object.

Use of Eval as variable Name
Use of Eval as variable Name 
Browser Support

Browser
Version
Internet Explorer
10
Firefox from version
4
Chrome from version
13
Safari from version
5.1
Opera from version
12

Leave a Reply

Your email address will not be published. Required fields are marked *