Skip to content
Fix Code Error

What does “use strict” do in JavaScript, and what is the reasoning behind it?

March 13, 2021 by Code Error
Posted By: Anonymous

Recently, I ran some of my JavaScript code through Crockford’s JSLint, and it gave the following error:

Problem at line 1 character 1: Missing “use strict” statement.

Doing some searching, I realized that some people add "use strict"; into their JavaScript code. Once I added the statement, the error stopped appearing. Unfortunately, Google did not reveal much of the history behind this string statement. Certainly it must have something to do with how the JavaScript is interpreted by the browser, but I have no idea what the effect would be.

So what is "use strict"; all about, what does it imply, and is it still relevant?

Do any of the current browsers respond to the "use strict"; string or is it for future use?

Solution

This article about Javascript Strict Mode might interest you: John Resig – ECMAScript 5 Strict Mode, JSON, and More

To quote some interesting parts:

Strict Mode is a new feature in ECMAScript 5 that allows you to place a program, or a function, in a "strict" operating context. This strict context prevents certain actions from being taken and throws more exceptions.

And:

Strict mode helps out in a couple ways:

  • It catches some common coding bloopers, throwing exceptions.
  • It prevents, or throws errors, when relatively "unsafe" actions are taken (such as gaining access to the global object).
  • It disables features that are confusing or poorly thought out.

Also note you can apply "strict mode" to the whole file… Or you can use it only for a specific function (still quoting from John Resig’s article):

// Non-strict code...

(function(){
  "use strict";

  // Define your library strictly...
})();

// Non-strict code...

Which might be helpful if you have to mix old and new code 😉

So, I suppose it’s a bit like the "use strict" you can use in Perl (hence the name?): it helps you make fewer errors, by detecting more things that could lead to breakages.

Strict mode is now supported by all major browsers.

Inside native ECMAScript modules (with import and export statements) and ES6 classes, strict mode is always enabled and cannot be disabled.

Answered By: Anonymous

Related Articles

  • npm global path prefix
  • Should I use JSLint or JSHint JavaScript validation?
  • What are the undocumented features and limitations…
  • Maven2: Missing artifact but jars are in place
  • How to insert a new key value pair in array in php?
  • Ukkonen's suffix tree algorithm in plain English
  • What's the difference between eval, exec, and compile?
  • How do SO_REUSEADDR and SO_REUSEPORT differ?
  • How is the 'use strict' statement interpreted in Node.js?
  • How to filter a RecyclerView with a SearchView
  • What is your most productive shortcut with Vim?
  • What is a NullReferenceException, and how do I fix it?
  • Reference - What does this regex mean?
  • data.table vs dplyr: can one do something well the…
  • Uncaught Error: No define call in require.js
  • How to initialize an array's length in JavaScript?
  • Create a card with wave in Polymer
  • Backbone.js: TypeError: Object # has no method 'parse'
  • How to generate a random string of a fixed length in Go?
  • What is an optional value in Swift?
  • For-each over an array in JavaScript
  • How does PHP 'foreach' actually work?
  • Programmatically navigate using React router
  • How do I include certain conditions in SQL Count
  • The definitive guide to form-based website authentication
  • JSLint is suddenly reporting: Use the function form…
  • Programmatically navigate using react router V4
  • Google Forms file upload complete example
  • Backbone.js click event in a Modal Reveal
  • How to implement my own history stack in a single…
  • Is it possible to apply CSS to half of a character?
  • What is the scope of variables in JavaScript?
  • How to use Regular Expressions (Regex) in Microsoft…
  • Usage of __slots__?
  • How can I manually compile a svelte component down…
  • PHP parse/syntax errors; and how to solve them
  • Angular Karma - Component undefined
  • Why does C++ code for testing the Collatz conjecture…
  • Polymer dom-repeat not rendering changes with…
  • Identifying and solving…
  • How to paste yanked text into the Vim command line
  • Remove URL parameters without refreshing page
  • Clearing a polyline on a Polymer Google Map
  • Difference between variable declaration syntaxes in…
  • Smart way to truncate long strings
  • How to keep a clean browser history in a backbone.js app?
  • How to return grandchilds names for each person in array JS?
  • Use Reveal.js with Polymer
  • What's the best way of scraping data from a website?
  • What are the new features in C++17?
  • How to render web component in Polymer
  • CSS animation as a Google Map Marker (Polymer 1.0)?
  • Are PDO prepared statements sufficient to prevent…
  • What does "Fatal error: Unexpectedly found nil while…
  • Google Maps Android API v2 - Interactive InfoWindow…
  • How to prevent scrolling the whole page?
  • Using StringWriter for XML Serialization
  • XMLHttpRequest cannot load ✘✘✘ No…
  • How to get previous url in nextjs
  • What does this symbol mean in JavaScript?
  • How to create a search filter using Vue js from API Data?
  • How to use java.net.URLConnection to fire and handle…
  • ExpressJS How to structure an application?
  • Using Address Instead Of Longitude And Latitude With…
  • How do I get the Back Button to work with an…
  • How to set zoom level in google map
  • What does a "Cannot find symbol" or "Cannot resolve…
  • Git Using Remote Branch
  • Why do I have to "git push --set-upstream origin "?
  • Ember.js: rendering google chart in template (AKA…
  • Database development mistakes made by application developers
  • SQL query return data from multiple tables
  • Pass array and/or object data between Polymer elements
  • Using Javascript: How to create a 'Go Back' link…
  • How to use Servlets and Ajax?
  • How to find out client ID of component for ajax…
  • Polymer 1.x: Observers
  • Vue&TypeScript: how to avoid error TS2345 when…
  • Node.js Web Application examples/tutorials
  • How to make popup look at the centre of the screen?
  • How to pass the id value of an url with axios and…
  • How can I find the product GUID of an installed MSI setup?
  • npm install error in vue
  • Ember.js -- How do I target outlets in…
  • How to get UTF-8 working in Java webapps?
  • What is the difference between atomic / volatile /…
  • commandButton/commandLink/ajax action/listener…
  • How to transfer browser’s session state across pages?
  • .Net How to create a Google Spreadsheet in Google Drive?
  • How to handle scroll position on hashchange in…
  • Why do git fetch origin and git fetch : behave differently?
  • Why doesn't [01-12] range work as expected?
  • How do JavaScript closures work?
  • Google Maps: Set Center, Set Center Point and Set…
  • How do I return the response from an asynchronous call?
  • Which equals operator (== vs ===) should be used in…
  • What's the best way to get the last element of an…
  • How to detect if multiple keys are pressed at once…
  • Is this very likely to create a memory leak in Tomcat?
  • Add prefix to all img src in a array

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

Calling the base constructor in C#

Next Post:

if else statement in AngularJS templates

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

.net ajax android angular arrays aurelia backbone.js bash c++ css dataframe ember-data ember.js excel git html ios java javascript jquery json laravel linux list mysql next.js node.js pandas php polymer polymer-1.0 python python-3.x r reactjs regex sql sql-server string svelte typescript vue-component vue.js vuejs2 vuetify.js

  • you shouldn’t need to use z-index
  • No column in target database, but getting “The schema update is terminating because data loss might occur”
  • Angular – expected call-signature: ‘changePassword’ to have a typedeftslint(typedef)
  • trying to implement NativeAdFactory imports deprecated method by default in flutter java project
  • What should I use to get an attribute out of my foreign table in Laravel?
© 2022 Fix Code Error