Missing use strict statement javascript ошибка

I am new to coding. I am receiving this error: «Missing «Use strict» statement» in my Javascript code.
Here is my code:

$(function(){
    $('.nav-toggle').on('click',function(){

        $('.main-nav').toggleClass('open');
    });
});

I do not know where to put 'use-strict';.
Where do I put the statement?

Barmar's user avatar

Barmar

731k53 gold badges488 silver badges607 bronze badges

asked Jan 14, 2016 at 22:54

Jyan's user avatar

5

As Barmar said, the use strict is optional, but if you had to place it somewhere the best place I would say you should put it is

$(function(){
    "use strict";
    $('.nav-toggle').on('click',function(){

        $('.main-nav').toggleClass('open');
    });
});

This is where you have to put it whether you use Jquery or Javascript

jfriend00's user avatar

jfriend00

677k94 gold badges967 silver badges964 bronze badges

answered Jan 14, 2016 at 22:59

JKer's user avatar

JKerJKer

884 bronze badges

4

(function($){
    "use strict";
    $('.nav-toggle').on('click',function(){

        $('.main-nav').toggleClass('open');
    });
})(jQuery);
.open{
  display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div>
  <button class="nav-toggle">Open</button>
  <div class="main-nav">
    <ul>
      <li>Home</li>
      <li>Contact</li>
    </ul>
  </div>
</div>

Assuming you want to achieve a responsive navbar effect I have added the relevant code to make this easier.

In the jQuery part, I’m creating an anonymous function and passing it the $ sign so you can use it safely without polluting the global scope. "use strict" use is not mandatory but if you want to use place it inside the anonymous function as I did.

answered Jan 14, 2016 at 23:06

Felipe Alarcon's user avatar

2

Asked
7 years, 6 months ago

Viewed
1k times

$(document).ready(function(){
  'use strict';
  $('#c2').hide('slow');
});
<html>
  <body>
    <div class="container">
      <div class="Header">
        <nav>
          <ul>
            <li><a href="#">Home Page</a></li>
            <li><a href="#">Kinematic Equations</a></li>
            <li><a href="#">Newtons Laws</a></li>
            <li><a href="#">Work and Force</a></li>
          </ul>
        </nav>
      </div>
    </div>

    <ul>
      <li id="one">Newtons 1st Law</li>
      <li id="two">Newtons 2nd Law</li>
      <li id="three">Newtons 3rd Law</li>
    </ul>

    <div class="content" id="c1"></div>
    <div class="content" id="c2"></div>
    <div class="content" id="c3"></div>
  </body>
</html>

When I try to run this code I do get the message «Missing ‘use strict’ statement». I am not sure what the problem could be.

FloPinguin's user avatar

FloPinguin

3511 gold badge5 silver badges16 bronze badges

asked Nov 9, 2015 at 5:31

Joseph Long's user avatar

6

You probably got this message from Dreamweaver. When you put 'use strict'; at the beginning of your script instead of putting it inside a $(document).ready(function(){ the error message should go away.

But there is probably a setting somewhere to turn off the prompt or error messaging for 'use strict';. It is not neccessary to use 'use strict';.

answered Oct 17, 2018 at 13:36

FloPinguin's user avatar

FloPinguinFloPinguin

3511 gold badge5 silver badges16 bronze badges

5

When do I get this error?

The «Missing ‘use strict’ statement» error is thrown when JSLint, JSHint and
ESLint encounter a function that does not contain the strict mode directive,
and none of whose ancestor scopes contain the strict mode directive
. JSHint
will only raise this warning if the strict option is set to true. Here’s an
example of a function that does not run in strict mode:

/*jshint strict: true */
function example() {
    return true;
}

Why do I get this error?

This error is raised to highlight a lack of convention. However, as
JavaScript engines move forward, this error will increasingly be helpful as it
should highlight areas of code that may not work as you expect them to, or may
even cause fatal JavaScript errors.

A "use strict" statement is an example of a directive, which can appear as
the first statement of a program or a function (ES5 §14.1):

A Directive Prologue is the longest sequence of ExpressionStatement
productions occurring as the initial SourceElement productions of a
Program or FunctionBody and where each ExpressionStatement in the
sequence consists entirely of a StringLiteral token followed a semicolon.
The semicolon may appear explicitly or may be inserted by automatic semicolon
insertion. A Directive Prologue may be an empty sequence.

The "use strict" directive can be used to force the engine to conform to a
strict subset of the language, as defined in ES5 Annex C. It has
become something of a convention to run all JavaScript code in strict mode, to
avoid falling into traps that are apparent in the non-strict language. See the
previous link or the corresponding MDN article for the details of the
differences in strict mode. You can fix this error by simply adding a "use
strict"
directive to the function, or to an ancestor function:

/*jshint strict: true */
function example() {
    "use strict";
    return true;
}

In JSHint 1.0.0 and above you have the ability to ignore any warning with a
special option syntax. This message is treated as an error by
JSHint which means you are unable to prevent it from being issued by ID.

In ESLint the rule that generates this warning is named strict. You can
disable it by setting it to 0, or enable it by setting it to 1.

When do I get this error?

The «Missing ‘use strict’ statement» error is thrown when JSLint, JSHint and
ESLint encounter a function that does not contain the strict mode directive,
and none of whose ancestor scopes contain the strict mode directive
. JSHint
will only raise this warning if the strict option is set to true. Here’s an
example of a function that does not run in strict mode:

/*jshint strict: true */
function example() {
    return true;
}

Why do I get this error?

This error is raised to highlight a lack of convention. However, as
JavaScript engines move forward, this error will increasingly be helpful as it
should highlight areas of code that may not work as you expect them to, or may
even cause fatal JavaScript errors.

A "use strict" statement is an example of a directive, which can appear as
the first statement of a program or a function (ES5 §14.1):

A Directive Prologue is the longest sequence of ExpressionStatement
productions occurring as the initial SourceElement productions of a
Program or FunctionBody and where each ExpressionStatement in the
sequence consists entirely of a StringLiteral token followed a semicolon.
The semicolon may appear explicitly or may be inserted by automatic semicolon
insertion. A Directive Prologue may be an empty sequence.

The "use strict" directive can be used to force the engine to conform to a
strict subset of the language, as defined in ES5 Annex C. It has
become something of a convention to run all JavaScript code in strict mode, to
avoid falling into traps that are apparent in the non-strict language. See the
previous link or the corresponding MDN article for the details of the
differences in strict mode. You can fix this error by simply adding a "use strict" directive to the function, or to an ancestor function:

/*jshint strict: true */
function example() {
    "use strict";
    return true;
}

In JSHint 1.0.0 and above you have the ability to ignore any warning with a
special option syntax. This message is treated as an error by
JSHint which means you are unable to prevent it from being issued by ID.

In ESLint the rule that generates this warning is named strict. You can
disable it by setting it to 0, or enable it by setting it to 1.

A missing "use strict" error is common when you use ESLint.
There are 2 possible fixes to it:

  • add the statement “use strict” in double quotes as a first line in your JS file
    "use strict"
    //the rest of your JS code goes below
    // AND must be in a strict mode
    
  • add a custom ESLint rule (as a comment!) to ignore the missing “use strict statement”
    /*eslint strict: ["error", "never"]*/
    
    //the rest of your JS code goes below
    //AND can be in a non-strict mode
    

You should try the first solution and if you’re not sure why it doesn’t work, then
you can disable the ESLint errors for missing strict mode.

Понравилась статья? Поделить с друзьями:
  • Missing texture detected blender ошибка
  • Missing system image android studio ошибка
  • Missing sql property delphi ошибка
  • Missing select keyword ошибка sql
  • Missing schema folder revit ошибка