Jyan
Jyan

Reputation: 53

Missing "Use strict" statement

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?

Upvotes: 2

Views: 20156

Answers (2)

Felipe Alarcon
Felipe Alarcon

Reputation: 956

(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.

Upvotes: 0

JKer
JKer

Reputation: 88

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

Upvotes: 3

Related Questions