QNimate

  • CoursesVideos
  • WP PremiumPlugins
  • DemosLab
  • Home
  • QIdea
  • QTrack
Home Carbon Ads Javascript “use strict” Tutorial with Examples

Javascript “use strict” Tutorial with Examples

strict-javascript

In this post we will have a look at one of the most important Javascript feature called as “strict mode”. We normally execute Javascript code in normal mode which allows many things which are actually of wrong syntax and unsecure. But when we use strict mode, Javascript code is executed without ignoring wrong syntax and also make the code more secure..


“use strict” directive

To execute our code in strict mode we need to use the “use strict” directive. Its just a expression. This directive is supported by JavaScript 1.8.5(ECMAScript 5) or above.

Using “use strict” directive

There are two ways of using “use strict” directive. We can use it as function declaration or global declaration.

When we use “use strict” as global declaration, all the javascript code in the page is executed in strict mode.

For example:

"use strict"

function print(val){
    console.log(val);
}

When we use “use strict” as function declaration, all the code inside the function is executed as strict mode. All other code outside the function is executed as normal mode.

For example:

function print(val){
    "use strict"
    console.log(val);
}

function print_twice(val){
    console.log(val);
    console.log(val);
}


Things that are not allowed in strict mode are:

Use of undeclared variable

In strict mode we cannot use an undeclared variable.

"use strict"
a = 12; //this throws exception in strict mode. In non-strict mode a new property for window object is created.

delete operator

Delete operator is used to delete user defined object properties and array elements. If we try to delete anything other than user defined object properties or array elements we get an exception.

"use strict"

var a = 78;
window.b = 34;

function print(){}
window.c = function(){}

delete a; //throws exception in strict mode. In non strict mode it returns false and program continues. 'a' is treated as a global variable not as an property of window object in both modes, so property operations are not allowed in variable a.
delete window.a; //throws exception in strict mode. In non strict mode it returns false and program continues.
delete window.b; //In both modes property b is deleted.
delete Math.PI; //In strict mode it returns false as PI is pre defined. In strict mode exception is thrown.

delete Object.prototype; //deleting an undeletable property in non-strict mode returns false. But in strict mode exception is thrown.

To learn more about delete operator click here. Just remember that wherever delete returns false in non-strict mode it throws exception in strict mode.

Multiple same declaration of property

In strict mode you cannot declare same object properties more than once. Otherwise it will throw exception.

var myObject = {property1:1, property2:2, property1:1}; //throws an exception.

If you declare global variables or functions more than once than no exception is thrown. Its only applicable for object properties.

Duplicate parameter names

In strict mode you cannot declare same parameter name more than once.

function print(value, value) {} //throws exception.

Octal numeric literals and escape characters are not allowed

Octal numeric literals and escape characters are not allowed in strict mode.

var testStrict = 010;  // throws exception
var testStrict = \010; // throws exception.

read-only and get-only Property

Writing to read-only and get-only properties of an object throws exception.

var testObjone = {};
Object.defineProperties(testObjone,"x",{value:0,writable:false});
testObjone.x = 3.14; // exception throws.

var testObjtwo = {get x() {return 0} };
testObjtwo.x = 3.14; // exception thrown.

“eval” and “arguments”

We cannot use eval and arguments strings as variable, property or object names in strict mode.

var eval = 12; //In non-strict mode eval is overwritten. Exception thrown in strict mode.
var arguments = 12;//Same behavior as above.

“with” statement

Use of “with” is not allowed in strict mode.

with (Math){x = sin(2)}; // throws an exception.

To learn more about with statement click here.

Reserved keywords

In strict mode we cannot use some words which are reserved for future version of JavaScript.

Reserved keywords are:

  1. implements
  2. interface
  3. package
  4. private
  5. protected
  6. public
  7. static
  8. yield
"use strict"
 var private = 12; //in non-strict mode this is allowed. In strict mode this throws an exception.

“this” keyword

“this” keyword inside global functions is undefined in strict mode.

"use strict"
function checkforthis(){
console.log(this); //will print undefined.
}
checkforthis();

eval creating variables

eval function creates variables in the scope from which it was called. But in strict mode it doesn’t.

"use strict"
eval("var q = 12");
console.log(q); //throws exception. q is not defined.

Benefits of strict mode

  • Strict mode changes previously accepted “bad syntax” into real errors.
  • In 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 property of window variable.
  • In normal JavaScript, a developer will not receive any error feedback assigning values to non-writable properties. And also 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

Conclusion

We from above theory we can say that strict mode helps us to reduce typing mistakes, common programming errors and also makes our code compatible for future versions of browsers. If you like this post please “Like and Share”.

Apr 27, 2014Narayan Prusty
Customizing Right Click Menu in HTML PageHow Web Caching Works?
Comments: 1
  1. Liza
    6 years ago

    Thank you for sharing excellent informations. Your web-site is very cool. I’m impressed by the details that you’ve on this blog. It reveals how nicely you perceive this subject. Bookmarked this website page, will come back for extra articles. You, my pal, ROCK! I found simply the info I already searched all over the place and simply cou;dn&#8217lt come across. What an ideal website.

    ReplyCancel

Leave a Reply to Liza Cancel reply

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax

Narayan Prusty

I am a software engineer specialising in Blockchain, DevOps and Go/JavaScript. This is my personal blog where I write about things that I learn and feel interesting to share.

Image8 years ago 1 Comment Web Development
Share this
0
GooglePlus
0
Facebook
0
Twitter
0
Linkedin
  • “use strict” directive
  • Using “use strict” directive
  • Use of undeclared variable
  • delete operator
  • Multiple same declaration of property
  • Duplicate parameter names
  • Octal numeric literals and escape characters are not allowed
  • read-only and get-only Property
  • “eval” and “arguments”
  • “with” statement
  • Reserved keywords
  • “this” keyword
  • eval creating variables
  • Benefits of strict mode
Related Articles
  • Default Function Arguments Values in JavaScript
  • JavaScript Function Techniques You May Not Know
  • JavaScript “let” Keyword
  • Increase PHP Script Execution Time
  • JavaScript “yield” Keyword and “function*()” Syntax
Our Sponsor
My Books

2014 - 2015 © QNimate
All tutorials MIT license