Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nodejs need use 'strict use'?or what is the best practice of node's strict mode?

I'm a new to node.js and javascript.I checked code examples of node.js and it used use strict mode.

For example, a server.js:

'use strict';
//some server code

Also, I got to know that use strict is present at head of every js file. It confused me and so I want to know what is best practice in Nodejs to use strict mode?


Thank you all, My question is focus on the strict mode. In this mode, some code mistake can be reported. In back-end, the strict error reporter also run? And If I need use it, I should add it in every js file header? Or add it in main file(server.js or etc.) head? Or use some node.js self style?

like image 772
Edward Avatar asked Jun 04 '15 03:06

Edward


People also ask

Do I need use strict in Nodejs?

Nodejs is server-side javascript, so many coding practices are similar in both. So using strict mode is common. It ensures that you are not violating certain coding conventions like undeclared variables x=14; , use of specific variable names arguments,eval which are names of few global variables and functions.

Should I always use strict mode?

Turning on strict mode helps you find errors sooner, before they become bigger problems. And it forces you to write better code. Always use strict mode on your scripts.

What is the benefit of using use strict `?

Benefits of using “use strict” It changes previously accepted "bad syntax" into real errors. As an example, mistyping a variable name creates a new global variable. When using strict mode, this will throw an error. It leads to making it impossible to accidentally create a global variable.


2 Answers

Use it always. If nothing else, it ensures that your code is properly written, and cross browser compatible as it can be. It also will reveal mundane syntax errors that would otherwise go unfound, and lead to hours of unnecessary debugging.

like image 195
Jeffrey A. Gochin Avatar answered Oct 07 '22 10:10

Jeffrey A. Gochin


“use strict” is a behavior flag you can add to to first line of any JavaScript file or function. It causes errors when certain bad practices are use in your code, and disallows the use of certain functions, such as with.

References:

  1. Restrictions on Code in Strict Mode by Microsoft MSDN
  2. Node.js Best Practices
like image 30
Anand Sudhanaboina Avatar answered Oct 07 '22 10:10

Anand Sudhanaboina