Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript: Can ECMAScript 5's Strict Mode ("use strict") be enabled using single quotes ('use strict')?

JavaScript doesn't care if your Strings are double-quoted "double" or single-quoted 'single'.

Every example of ECMAScript 5's strict mode has it enabled by "use strict" in double-quotes. Can I do the following (single-quotes):

alert(function(){   'use strict';   return !this; }()); 

This will return true if Strict mode is enabled, and false if it is not.

like image 597
Rudiger Avatar asked Mar 06 '11 23:03

Rudiger


People also ask

How do you enable strict mode in JavaScript?

You can enable the strict mode by declaring this in the top of your script/function. 'use strict'; When a JavaScript engine sees this directive, it will start to interpret the code in a special mode.

Is ES6 strict mode?

ES6 modules and classes are strict by default.

Is use strict a statement in JavaScript?

Using Strict mode for a function: Likewise, to invoke strict mode for a function, put the exact statement “use strict”; (or 'use strict';) in the function's body before any other statements. Examples of using Strict mode: Example: In normal JavaScript, mistyping a variable name creates a new global variable.

Where should we place strict in JavaScript?

We can also apply this to functions. To do this, we add the statement "use strict" or 'use strict' inside the functions on the top of the body, before any other statements. It's applied to everything inside, including functions that are nested in the function that uses strict mode.


2 Answers

For you, without using a browser that supports strict mode:

A Use Strict Directive is an ExpressionStatement in a Directive Prologue whose StringLiteral is either the exact character sequences "use strict" or 'use strict'. A Use Strict Directive may not contain an EscapeSequence or LineContinuation.

like image 124
Felix Kling Avatar answered Oct 18 '22 22:10

Felix Kling


http://ecma262-5.com/ELS5_HTML.htm#Section_14.1

A Use Strict Directive is an ExpressionStatement in a Directive Prologue whose StringLiteral is either the exact character sequences "use strict" or 'use strict'. A Use Strict Directive may not contain an EscapeSequence or LineContinuation.

like image 32
user113716 Avatar answered Oct 18 '22 23:10

user113716