How is the 'use strict' statement interpreted in Node.js? [duplicate]
Asked Answered
T

1

227

I have started to explore the Node.js and wrote many demo web application, to understand the flow of Node.js, Express.js, jade, etc..

But one thing I came across recently, is the statement "use strict" as the first line inside every function and every .js file.

How exactly is it is interpreted by Node.js?

Talanta answered 24/8, 2013 at 10:9 Comment(12)
this is not duplicate... I really donot know about JavaScript.. and if it is there in JavaScript. some differences will be there.. right? so please give some understandings how about node.jsTalanta
The files which you've seen probably have a .js extension as well. That's the language which Node runs (its JS interpreter being V8).Feces
Yes, and although Node.js is JavaScript, there's a subtle difference in how to use strict mode: In Node.js it's no problem to do enable it file-wide, outside of an immediately executed function, in the browser that's a no-go. So, at least IMHO no need for sarcasm.Unsustainable
@GoloRoden though I refrain from using it file-wide, MDN says it works as intended when setting 'use strict'; file-wide. Problems arise when you concatenate non-strict with strict files, or when you set use strict inside an HTML page's inline script tag (which Node does not have).Feces
Exactly. Nevertheless, there's that difference between web browsers and Node.js: In one scenario you should think about it, in the other you don't need to. (My comment regarding sarcasm was not related to you, btw)Unsustainable
As you already said: Problems may arise when you concatenate all your script files. Hence I'd consider it a no-go, and would - as rule of thumb - recommend that you always wrap your code in an immediately executed function (which is a good idea, anyway, for various other reasons). In Node.js, that's simply not neccessary.Unsustainable
@GoloRoden +1 for the IIFE. That's what I do for all my JS files and is also recommended by JS[H|L]int from what I remember.Feces
Yep, and as mentioned before: It's good practice for various other reasons, such as scoping, creating modules & co.Unsustainable
Yep, IIFEs for scoping are very useful in front-end JS otherwise any var/function declarations would be unnecessarily thrown in the global scope. Though Node.js doesn't have that problem, you have to manually export vars/functions through module.exports (or global shivers) so scoping isn't that much of an issue on Node. Well, too sleepy to keep up, night. :PFeces
I'm not sure this was a duplicate. This question was useful in helping me understand that node.js is interpreted using the same engine as the Chrome Browser. Without that knowledge, there's limited clarity on how "use strict" is applied.Apure
@AmolMKulkarni : *note that Node JS wraps our code (module) into function (function (exports, require, module, __filename, __dirname, process, global) { .. your module code }); and then it is processed by V8Batty
How is this a duplicate of that referenced question? People got a bit gung ho here.Oppress
S
213

"use strict";

Basically it enables the strict mode.

Strict Mode is a feature that allows you to place a program, or a function, in a "strict" operating context. In strict operating context, the method form binds this to the objects as before. The function form binds this to undefined, not the global set objects.

As per your comments you are telling some differences will be there. But it's your assumption. The Node.js code is nothing but your JavaScript code. All Node.js code are interpreted by the V8 JavaScript engine. The V8 JavaScript Engine is an open source JavaScript engine developed by Google for Chrome web browser.

So, there will be no major difference how "use strict"; is interpreted by the Chrome browser and Node.js.

Please read what is strict mode in JavaScript.

For more information:

  1. Strict mode
  2. ECMAScript 5 Strict mode support in browsers
  3. Strict mode is coming to town
  4. Compatibility table for strict mode
  5. Stack Overflow questions: what does 'use strict' do in JavaScript & what is the reasoning behind it


ECMAScript 6:

ECMAScript 6 Code & strict mode. Following is brief from the specification:

10.2.1 Strict Mode Code

An ECMAScript Script syntactic unit may be processed using either unrestricted or strict mode syntax and semantics. Code is interpreted as strict mode code in the following situations:

  • Global code is strict mode code if it begins with a Directive Prologue that contains a Use Strict Directive (see 14.1.1).
  • Module code is always strict mode code.
  • All parts of a ClassDeclaration or a ClassExpression are strict mode code.
  • Eval code is strict mode code if it begins with a Directive Prologue that contains a Use Strict Directive or if the call to eval is a direct eval (see 12.3.4.1) that is contained in strict mode code.
  • Function code is strict mode code if the associated FunctionDeclaration, FunctionExpression, GeneratorDeclaration, GeneratorExpression, MethodDefinition, or ArrowFunction is contained in strict mode code or if the code that produces the value of the function’s [[ECMAScriptCode]] internal slot begins with a Directive Prologue that contains a Use Strict Directive.
  • Function code that is supplied as the arguments to the built-in Function and Generator constructors is strict mode code if the last argument is a String that when processed is a FunctionBody that begins with a Directive Prologue that contains a Use Strict Directive.

Additionally if you are lost on what features are supported by your current version of Node.js, this node.green can help you (leverages from the same data as kangax).

Shriver answered 24/8, 2013 at 10:30 Comment(5)
First of all, Chrome is not the only browser on the planet. Just that Node.js and Chrome use V8, does not mean neccessarily that it works the same in all browsers. Second, there is a difference in the way, files (aka modules) are loaded. Third, scripts for browsers get usually concatenated for production use, and that's where problems may arise when you just say that both are the same environments. They're not. V8 is not the only important thing when it comes to executing Node.js files.Unsustainable
@GabrielLlamas: Thanks. @ Golo Roden: Yes, if commented properly. we can give more better answers.Shriver
@AmolMKulkarni : "Module code is always strict mode code" - it is not exactly true for Node. If you will not use 'use strict' in node v.6.10.2 following code will not throw error: var obj = {}; Object.preventExtensions(obj); obj.a=1;Batty
@fider: Though your question is not very clear to me. You can consider asking a new question or continue explaining in comment if its relevant to this question asked. Before that have a look at this link if you can find the answerShriver
I think Node.js not include full Ecmascript 6 Module (which support Strict Mode by default), see this useful article here (discuss usage of Strict mode into Javascript/Node.js) => tvernon.tech/blog/javascript-strict-modeTraitorous

© 2022 - 2024 — McMap. All rights reserved.