Sunday, May 6, 2012

Using "use strict" in JavaScript

Using Firefox, as of this writing, version 12.0. Here's a tiny part of using "use strict" syntax in JavaScript.

without "use strict",


var x = 17;
var evalX = eval("var x = 42; x");
alert(x === 17); // results to false
alert(x === 42); // results to true
alert(evalX === 42); // results to true




with "use strict",

var x = 17;
var evalX = eval("'use strict'; var x = 42; x");
alert(x === 17); // results to true
alert(x === 42); // results to false

alert(evalX === 42); // results to true






To know more about "use strict", read more on Mozilla's page at https://developer.mozilla.org/en/JavaScript/Strict_mode or another interesting blog to read at http://ejohn.org/blog/ecmascript-5-strict-mode-json-and-more/

A tabular view shows what browsers does support strict mode in JavaScript. Taken from http://caniuse.com/#feat=use-strict, as of this writing, I got this tabular view below,


No comments: