Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the proper way to use jQuery in conjunction with 'use strict'?

If I have something like the following

"use strict";

$(document).ready(function () {

});

I get the warning

'$'is not defined
like image 785
deltanovember Avatar asked Apr 21 '12 21:04

deltanovember


People also ask

Why We Use Use strict in jQuery?

The "use strict" Directive The purpose of "use strict" is to indicate that the code should be executed in "strict mode". With strict mode, you can not, for example, use undeclared variables. The numbers in the table specify the first browser version that fully supports the directive.

What is correct way to run a JavaScript in strict mode?

Using Strict mode for a function: Likewise, to invoke strict mode for a function, put the exact statement “use strict”; (or 'use strict';) in the function's body before any other statements. Examples of using Strict mode: Example: In normal JavaScript, mistyping a variable name creates a new global variable.

Where should we place strict in JavaScript?

To do this, we add the statement "use strict" or 'use strict' inside the functions on the top of the body, before any other statements. It's applied to everything inside, including functions that are nested in the function that uses strict mode.

How can I use jQuery and JavaScript together?

jQuery code is implemented as part of JavaScript scripts. To add jQuery and JavaScript to your web pages, first add a <script> tag that loads the jQuery library, and then add your own <script> tags with your custom code.


1 Answers

( function ( $ ) {
    'use strict';
    $( document ).ready( function () {
        console.log( 'working!' )
    })
} ( jQuery ) )
like image 73
Florian Margaine Avatar answered Oct 17 '22 08:10

Florian Margaine